فهرست منبع

更新权限问题

shish 2 ماه پیش
والد
کامیت
d6dd95ebb7
6فایلهای تغییر یافته به همراه192 افزوده شده و 6 حذف شده
  1. 4 0
      ghsApp/src/App.vue
  2. 2 1
      ghsApp/src/manifest.json
  3. 90 2
      ghsApp/src/utils/appDevice.js
  4. 4 0
      hdApp/src/App.vue
  5. 2 1
      hdApp/src/manifest.json
  6. 90 2
      hdApp/src/utils/appDevice.js

+ 4 - 0
ghsApp/src/App.vue

@@ -114,6 +114,10 @@ export default {
 				name:"读取外部存储空间",
 				explain:"为了修改个人头像和上传花材图片, 我们需要申请您设备的相机和存储权限",
 			},
+			"POST_NOTIFICATIONS":{
+				name:"通知",
+				explain:"为了及时接收订单等重要消息,我们需要申请通知权限",
+			},
 		}
 		// 唤起权限会触发onHide,所以listenerFunc须在onShow生命周期调用
 		permissionListener && permissionListener.listenerFunc(permissionEnums)

+ 2 - 1
ghsApp/src/manifest.json

@@ -48,11 +48,12 @@
                     "request" : "none",
                     "prompt" : "为保证您正常和安全地使用,需要获取设备识别码(部分手机提示为获取手机号码)使用权限,请允许。"
                 },
-                "targetSdkVersion" : 30,
+                "targetSdkVersion" : 33,
                 "minSdkVersion" : 21
             },
             /* ios打包配置 */
             "ios" : {
+                "pushRegisterMode" : "manual",
                 "urlschemewhitelist" : [ "baidumap", "iosamap" ],
                 "privacyDescription" : {
                     "NSPhotoLibraryUsageDescription" : "该应用需要读取您的相册,以便支持您修改头像、门店图片或花材图片",

+ 90 - 2
ghsApp/src/utils/appDevice.js

@@ -1,4 +1,5 @@
 import { replaceClientId } from '@/api/admin'
+import permision from '@/utils/wa-permission_1.1/permission.js'
 
 function isInvalidOaid(OAID) {
 	if (!OAID) {
@@ -8,6 +9,34 @@ function isInvalidOaid(OAID) {
 	return /^[0|-]+$/.test(OAID)
 }
 
+function isAndroid13Plus() {
+	// #ifdef APP-PLUS
+	try {
+		const Build = plus.android.importClass('android.os.Build')
+		return Build.VERSION.SDK_INT >= 33
+	} catch (e) {
+		return false
+	}
+	// #endif
+	return false
+}
+
+function areAndroidNotificationsEnabled() {
+	// #ifdef APP-PLUS
+	try {
+		const main = plus.android.runtimeMainActivity()
+		let NotificationManagerCompat = plus.android.importClass('androidx.core.app.NotificationManagerCompat')
+		if (!NotificationManagerCompat) {
+			NotificationManagerCompat = plus.android.importClass('android.support.v4.app.NotificationManagerCompat')
+		}
+		return NotificationManagerCompat.from(main).areNotificationsEnabled()
+	} catch (e) {
+		return false
+	}
+	// #endif
+	return true
+}
+
 function getIosDeviceId(onComplete) {
 	// #ifdef APP-PLUS
 	const keychain = uni.requireNativePlugin('Univalsoft-DeviceId')
@@ -84,6 +113,58 @@ function androidGetOAID(onComplete) {
 	// #endif
 }
 
+/**
+ * 登录成功后、同步推送前,由用户确认再申请通知权限(Android 13+)
+ */
+export function requestAppNotificationPermission(onComplete) {
+	// #ifdef APP-PLUS
+	if (plus.os.name === 'iOS') {
+		if (onComplete) {
+			onComplete()
+		}
+		return
+	}
+	if (!isAndroid13Plus() || areAndroidNotificationsEnabled()) {
+		if (onComplete) {
+			onComplete()
+		}
+		return
+	}
+	uni.showModal({
+		title: '提示',
+		content: '开启通知权限后,可及时接收订单等重要消息',
+		confirmText: '开启',
+		cancelText: '暂不',
+		success(res) {
+			const done = () => {
+				if (onComplete) {
+					onComplete()
+				}
+			}
+			if (!res.confirm) {
+				done()
+				return
+			}
+			permision.requestAndroidPermission('android.permission.POST_NOTIFICATIONS').then(() => {
+				done()
+			}).catch(() => {
+				done()
+			})
+		},
+		fail() {
+			if (onComplete) {
+				onComplete()
+			}
+		}
+	})
+	// #endif
+	// #ifndef APP-PLUS
+	if (onComplete) {
+		onComplete()
+	}
+	// #endif
+}
+
 /**
  * 登录成功后获取设备标识(仅 App 端)
  */
@@ -125,12 +206,19 @@ export function syncAppPushClientId(auto = 0) {
 }
 
 /**
- * 用户登录成功后:获取设备 ID 同步推送 clientId
+ * 用户登录成功后:获取设备 ID → 用户确认后申请通知权限 → 同步推送 clientId
  * @param {{ auto?: number }} options auto=1 表示静默恢复登录,auto=0 表示用户主动登录
  */
 export function afterAppLoginSuccess(options = {}) {
 	const auto = options.auto !== undefined ? options.auto : 0
 	setupAppDeviceId(() => {
-		syncAppPushClientId(auto)
+		if (auto === 1) {
+			// 冷启动自动恢复登录:不弹通知授权,仅尝试同步 clientId
+			syncAppPushClientId(auto)
+			return
+		}
+		requestAppNotificationPermission(() => {
+			syncAppPushClientId(auto)
+		})
 	})
 }

+ 4 - 0
hdApp/src/App.vue

@@ -145,6 +145,10 @@ export default {
 				name:"读取外部存储空间",
 				explain:"为了修改个人头像和上传花材图片, 我们需要申请您设备的相机和存储权限",
 			},
+			"POST_NOTIFICATIONS":{
+				name:"通知",
+				explain:"为了及时接收订单等重要消息,我们需要申请通知权限",
+			},
 		}
 		// 唤起权限会触发onHide,所以listenerFunc须在onShow生命周期调用
 		permissionListener && permissionListener.listenerFunc(permissionEnums)

+ 2 - 1
hdApp/src/manifest.json

@@ -50,11 +50,12 @@
                     "request" : "none",
                     "prompt" : "为保证您正常和安全地使用,需要获取设备识别码(部分手机提示为获取手机号码)使用权限,请允许。"
                 },
-                "targetSdkVersion" : 30,
+                "targetSdkVersion" : 33,
                 "minSdkVersion" : 21
             },
             /* ios打包配置 */
             "ios" : {
+                "pushRegisterMode" : "manual",
                 "urlschemewhitelist" : [ "baidumap", "iosamap" ],
                 "privacyDescription" : {
                     "NSPhotoLibraryUsageDescription" : "该应用需要读取您的相册,以便支持您修改头像、门店图片或花材图片",

+ 90 - 2
hdApp/src/utils/appDevice.js

@@ -1,4 +1,5 @@
 import { replaceClientId } from '@/api/admin'
+import permision from '@/utils/wa-permission_1.1/permission.js'
 
 function isInvalidOaid(OAID) {
 	if (!OAID) {
@@ -8,6 +9,34 @@ function isInvalidOaid(OAID) {
 	return /^[0|-]+$/.test(OAID)
 }
 
+function isAndroid13Plus() {
+	// #ifdef APP-PLUS
+	try {
+		const Build = plus.android.importClass('android.os.Build')
+		return Build.VERSION.SDK_INT >= 33
+	} catch (e) {
+		return false
+	}
+	// #endif
+	return false
+}
+
+function areAndroidNotificationsEnabled() {
+	// #ifdef APP-PLUS
+	try {
+		const main = plus.android.runtimeMainActivity()
+		let NotificationManagerCompat = plus.android.importClass('androidx.core.app.NotificationManagerCompat')
+		if (!NotificationManagerCompat) {
+			NotificationManagerCompat = plus.android.importClass('android.support.v4.app.NotificationManagerCompat')
+		}
+		return NotificationManagerCompat.from(main).areNotificationsEnabled()
+	} catch (e) {
+		return false
+	}
+	// #endif
+	return true
+}
+
 function getIosDeviceId(onComplete) {
 	// #ifdef APP-PLUS
 	const keychain = uni.requireNativePlugin('Univalsoft-DeviceId')
@@ -84,6 +113,58 @@ function androidGetOAID(onComplete) {
 	// #endif
 }
 
+/**
+ * 登录成功后、同步推送前,由用户确认再申请通知权限(Android 13+)
+ */
+export function requestAppNotificationPermission(onComplete) {
+	// #ifdef APP-PLUS
+	if (plus.os.name === 'iOS') {
+		if (onComplete) {
+			onComplete()
+		}
+		return
+	}
+	if (!isAndroid13Plus() || areAndroidNotificationsEnabled()) {
+		if (onComplete) {
+			onComplete()
+		}
+		return
+	}
+	uni.showModal({
+		title: '提示',
+		content: '开启通知权限后,可及时接收订单等重要消息',
+		confirmText: '开启',
+		cancelText: '暂不',
+		success(res) {
+			const done = () => {
+				if (onComplete) {
+					onComplete()
+				}
+			}
+			if (!res.confirm) {
+				done()
+				return
+			}
+			permision.requestAndroidPermission('android.permission.POST_NOTIFICATIONS').then(() => {
+				done()
+			}).catch(() => {
+				done()
+			})
+		},
+		fail() {
+			if (onComplete) {
+				onComplete()
+			}
+		}
+	})
+	// #endif
+	// #ifndef APP-PLUS
+	if (onComplete) {
+		onComplete()
+	}
+	// #endif
+}
+
 /**
  * 登录成功后获取设备标识(仅 App 端)
  */
@@ -125,12 +206,19 @@ export function syncAppPushClientId(auto = 0) {
 }
 
 /**
- * 用户登录成功后:获取设备 ID 同步推送 clientId
+ * 用户登录成功后:获取设备 ID → 用户确认后申请通知权限 → 同步推送 clientId
  * @param {{ auto?: number }} options auto=1 表示静默恢复登录,auto=0 表示用户主动登录
  */
 export function afterAppLoginSuccess(options = {}) {
 	const auto = options.auto !== undefined ? options.auto : 0
 	setupAppDeviceId(() => {
-		syncAppPushClientId(auto)
+		if (auto === 1) {
+			// 冷启动自动恢复登录:不弹通知授权,仅尝试同步 clientId
+			syncAppPushClientId(auto)
+			return
+		}
+		requestAppNotificationPermission(() => {
+			syncAppPushClientId(auto)
+		})
 	})
 }