diff --git a/packages/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.es.js b/packages/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.es.js index eba97fae86fd2fb6555ef6d9b356faedc81ba033..49e871a318755660d6a4cf074d062f398fd96249 100644 --- a/packages/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.es.js +++ b/packages/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.es.js @@ -133,22 +133,6 @@ function initPushNotification() { }, }); }); - plus.push.addEventListener('click', (result) => { - // @ts-expect-error - uni.invokePushCallback({ - type: 'click', - message: result, - }); - }); - uni.onPushMessage((res) => { - if (res.type === 'receive' && - res.data && - res.data.force_notification) { - // 创建通知栏 - uni.createPushMessage(res.data); - res.stopped = true; - } - }); } } @@ -207,4 +191,16 @@ else { }); }, }); + // 仅在 jssdk 中监听 + // #ifdef APP + uni.onPushMessage((res) => { + if (res.type === 'receive' && + res.data && + res.data.force_notification) { + // 创建通知栏 + uni.createPushMessage(res.data); + res.stopped = true; + } + }); + // #endif } diff --git a/packages/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.plus.es.js b/packages/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.plus.es.js index f28302f21454bb2045c30c9a81242ee587f21f74..9269d971447acf024bb2d922b8ea41272a3f8913 100644 --- a/packages/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.plus.es.js +++ b/packages/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.plus.es.js @@ -31,22 +31,6 @@ function initPushNotification() { }, }); }); - plus.push.addEventListener('click', (result) => { - // @ts-expect-error - uni.invokePushCallback({ - type: 'click', - message: result, - }); - }); - uni.onPushMessage((res) => { - if (res.type === 'receive' && - res.data && - res.data.force_notification) { - // 创建通知栏 - uni.createPushMessage(res.data); - res.stopped = true; - } - }); } } @@ -58,11 +42,4 @@ uni.invokePushCallback({ Promise.resolve().then(() => { initPushNotification(); plus.push.setAutoNotification && plus.push.setAutoNotification(false); - plus.push.addEventListener('receive', (result) => { - // @ts-expect-error - uni.invokePushCallback({ - type: 'pushMsg', - message: result, - }); - }); }); diff --git a/src/core/service/api/plugin/push.js b/src/core/service/api/plugin/push.js index 7bbdbe056063e8b4966d1ca10321da3e14582d8c..fa3c84ae936360eeb3f0f55302149ac3aab3691c 100644 --- a/src/core/service/api/plugin/push.js +++ b/src/core/service/api/plugin/push.js @@ -23,7 +23,7 @@ export function invokePushCallback ( ) { if (args.type === 'enabled') { enabled = true - if (__PLATFORM__ === 'app') { + if (__PLATFORM__ === 'app-plus') { offline = args.offline } } else if (args.type === 'clientId') { @@ -76,7 +76,7 @@ export function getPushClientId (args) { const hasComplete = isFn(complete) // App 端且启用离线时,使用 getClientInfoAsync 来调用 - if (__PLATFORM__ === 'app' && offline) { + if (__PLATFORM__ === 'app-plus' && offline) { plus.push.getClientInfoAsync( (info) => { const res = { @@ -126,8 +126,25 @@ export function getPushClientId (args) { } const onPushMessageCallbacks = [] +let listening = false // 不使用 defineOnApi 实现,是因为 defineOnApi 依赖 UniServiceJSBridge ,该对象目前在小程序上未提供,故简单实现 export const onPushMessage = (fn) => { + // 不能程序启动时就监听,因为离线事件,仅触发一次,框架监听后,无法转发给还没开始监听的开发者 + if (__PLATFORM__ === 'app' && !listening) { + listening = true + plus.push.addEventListener('click', (result) => { + invokePushCallback({ + type: 'click', + message: result + }) + }) + plus.push.addEventListener('receive', (result) => { + invokePushCallback({ + type: 'pushMsg', + message: result + }) + }) + } if (onPushMessageCallbacks.indexOf(fn) === -1) { onPushMessageCallbacks.push(fn) }