|
|
@@ -187,12 +187,51 @@ export function setupAppDeviceId(onComplete) {
|
|
|
// #endif
|
|
|
}
|
|
|
|
|
|
+let pushMessageListenerRegistered = false
|
|
|
+
|
|
|
+/**
|
|
|
+ * 登录成功后再注册推送消息监听,避免冷启动触发推送通道初始化
|
|
|
+ */
|
|
|
+export function setupAppPushMessageListener() {
|
|
|
+ // #ifdef APP-PLUS
|
|
|
+ if (pushMessageListenerRegistered) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ pushMessageListenerRegistered = true
|
|
|
+ uni.onPushMessage((res) => {
|
|
|
+ console.log('收到推送消息:', res)
|
|
|
+ const message = res.data
|
|
|
+ const pagePath = message.payload && message.payload.page
|
|
|
+ if (!pagePath) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ let url = pagePath.startsWith('/') ? pagePath : `/${pagePath}`
|
|
|
+ if (message.params || (message.payload && message.payload.params)) {
|
|
|
+ const params = message.params || message.payload.params
|
|
|
+ const queryString = Object.keys(params).map(key => `${key}=${params[key]}`).join('&')
|
|
|
+ url = `${url}?${queryString}`
|
|
|
+ }
|
|
|
+ console.log('推送消息跳转页面:', url)
|
|
|
+ uni.navigateTo({
|
|
|
+ url: url,
|
|
|
+ fail: (err) => {
|
|
|
+ console.error('页面跳转失败:', err)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ plus.runtime.setBadgeNumber(0)
|
|
|
+ })
|
|
|
+ // #endif
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* 登录成功后同步推送 clientId(仅 App 端)
|
|
|
*/
|
|
|
export function syncAppPushClientId(auto = 0) {
|
|
|
// #ifdef APP-PLUS
|
|
|
try {
|
|
|
+ if (plus.os.name !== 'iOS' && isAndroid13Plus() && !areAndroidNotificationsEnabled()) {
|
|
|
+ return
|
|
|
+ }
|
|
|
const clientInfo = plus.push.getClientInfo()
|
|
|
const currentClientId = clientInfo && clientInfo.clientid ? clientInfo.clientid : ''
|
|
|
const deviceId = uni.getStorageSync('deviceId')
|
|
|
@@ -205,20 +244,25 @@ export function syncAppPushClientId(auto = 0) {
|
|
|
// #endif
|
|
|
}
|
|
|
|
|
|
+function finishAppPushSetup(auto) {
|
|
|
+ setupAppPushMessageListener()
|
|
|
+ syncAppPushClientId(auto)
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
- * 用户登录成功后:获取设备 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(() => {
|
|
|
if (auto === 1) {
|
|
|
- // 冷启动自动恢复登录:不弹通知授权,仅尝试同步 clientId
|
|
|
- syncAppPushClientId(auto)
|
|
|
+ // 冷启动自动恢复登录:不弹通知授权,仅注册监听并尝试同步 clientId
|
|
|
+ finishAppPushSetup(auto)
|
|
|
return
|
|
|
}
|
|
|
requestAppNotificationPermission(() => {
|
|
|
- syncAppPushClientId(auto)
|
|
|
+ finishAppPushSetup(auto)
|
|
|
})
|
|
|
})
|
|
|
}
|