<script>
	const jpushModule = uni.requireNativePlugin('JG-JPush')
	export default {
		onLaunch: function() {
			console.log('App Launch')
			if (uni.getSystemInfoSync().platform == "ios") {
				// 请求定位权限
				let locationServicesEnabled = jpushModule.locationServicesEnabled()
				let locationAuthorizationStatus = jpushModule.getLocationAuthorizationStatus()
				console.log('locationAuthorizationStatus', locationAuthorizationStatus)
				if (locationServicesEnabled == true && locationAuthorizationStatus < 3) {
					jpushModule.requestLocationAuthorization((result) => {
						console.log('定位权限', result.status)
					})
				}
				jpushModule.requestNotificationAuthorization((result) => {
					let status = result.status
					if (status < 2) {
						uni.showToast({
							icon: 'none',
							title: '您还没有打开通知权限',
							duration: 3000
						})
					}
				})
			}


			// 初始化sdk
			jpushModule.initJPushService();


			// 开启 debug 模式，默认是关闭
			jpushModule.setLoggerEnable(true);

			// 连接状态回调 true已连接  false未连接
			jpushModule.addConnectEventListener(result => {
				console.log(result)
				let connectEnable = result.connectEnable
				uni.$emit('connectStatusChange', connectEnable)
			});


			// 取得应用程序对应的 RegistrationID。 只有当应用程序成功注册到 JPush 的服务器时才返回对应的值，否则返回空字符串
			// jpushModule.getRegistrationID(result => {
			// 	console.log(result, "注册ID.....")
			// 	// if (result && result.registerID) {
			// 	// 	this.globalData.text = result.registerID
			// 	// 	uni.$emit('getRegisterID', result.registerID)
			// 	// }
			// 	this.globalData.text = result.registerID
			// 	uni.$emit('getRegisterID', result.registerID)

			// })


			// 通知事件回调
			jpushModule.addNotificationListener(result => {
				console.log(result)
				if (result.notificationEventType == "notificationOpened") {
					//点击窗口通知栏推送的消息 跳转指定页面
					uni.navigateTo({
						url: '/pages/home/warningCenter/warningCenter',
						complete(res) {
							console.log(res);
						}
					})
				}

			});
			// 自定义消息事件回调
			jpushModule.addCustomMessageListener(result => {
				let type = result.type
				let messageType = result.messageType
				let content = result.content
				uni.showToast({
					icon: 'none',
					title: JSON.stringify(result),
					duration: 3000
				})
			})
			// 本地通知事件回调
			jpushModule.addLocalNotificationListener(result => {
				let messageID = result.messageID
				let title = result.title
				let content = result.content
				let extras = result.extras
				uni.showToast({
					icon: 'none',
					title: JSON.stringify(result),
					duration: 3000
				})
			})
			// 地理围栏事件回调
			jpushModule.addGeofenceListener(result => {
				let code = result.code
				let type = result.type
				let geofenceId = result.geofenceId
				let userInfo = result.userInfo
				uni.showToast({
					icon: 'none',
					title: '触发地理围栏',
					duration: 3000
				})
			})

			jpushModule.setIsAllowedInMessagePop(true)
			jpushModule.pullInMessage(result => {
				let code = result.code
				console.log(code)
			})
			// 应用内消息回调
			jpushModule.addInMessageListener(result => {
				let eventType = result.eventType
				let messageType = result.messageType
				let content = result.content
				console.log('inMessageListener', eventType, messageType, content)

				uni.showToast({
					icon: 'none',
					title: JSON.stringify(result),
					duration: 3000
				})
			})
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>