<script>
	export default {
		onLaunch: () => {
			// 1 判断手机权限 , 如果没有权限就去设置
			//#ifdef APP-PLUS
			checkNotificationAuthorized();
			//#endif

			// 2 获取客户端推送标识信息 cid , 必须要获取到cid后才能接收推送信息
			uni.getPushClientId({
				success: (res) => {
					// 将获取到的cid存起来，方便其它页面从缓存中获取
					uni.setStorageSync('cid',res.cid)
					console.log('客户端推送标识:', res.cid)
				}
			})

			// 3 启动监听推送消息事件
			uni.onPushMessage(res => {
				const { type, data } = res
				console.log('"click"-从系统推送服务点击消息启动应用事件；', res);
				if (type == 'click') {
					console.log('"click"-从系统推送服务点击消息启动应用事件；', res);
					if (!data?.payload?.url) {
						uni.reLaunch({
							url: '/pages/xiaoxi/index'
						});
					} else {
						setTimeout(() => {
							uni.navigateTo({
								url: data.payload.url
							});
						}, 1000);
					}
				}
				if (type == 'receive') {
					console.log('"receive"-应用从推送服务器接收到推送消息事件', res);
				}
			});
		}
	}

	// 检查app是否开启了通知权限 安卓苹果通用
	function checkNotificationAuthorized() {
		const notificationAuthorized = uni.getAppAuthorizeSetting().notificationAuthorized
		if (notificationAuthorized !== 'authorized') {
			uni.showModal({
				title: '通知权限',
				content: '您还没有开启通知权限，无法接受到消息通知，请前往设置！',
				confirmText: '去设置',
				showCancel: false,
				success:(res) => {
					if (res.confirm) uni.openAppAuthorizeSetting()
				}
			});
		}
	}
</script>
