diff --git a/packages/uni-api/src/service/plugin/push.ts b/packages/uni-api/src/service/plugin/push.ts index 26b385d03fa4afa968b777b815bb713bbd4210df..911ebcb356fe45d939262bf589c1159ef4f0f067 100644 --- a/packages/uni-api/src/service/plugin/push.ts +++ b/packages/uni-api/src/service/plugin/push.ts @@ -2,6 +2,7 @@ import { defineAsyncApi } from '../../helpers/api' interface OnPushEnabledCallback { type: 'enabled' + offline: boolean } interface OnPushClientIdCallback { @@ -28,6 +29,7 @@ interface OnPushClickCallback { let cid: string | undefined let cidErrMsg: string | undefined let enabled: boolean | undefined +let offline: boolean | undefined function normalizePushMessage(message: unknown) { try { @@ -49,6 +51,9 @@ export function invokePushCallback( ) { if (args.type === 'enabled') { enabled = true + if (__PLATFORM__ === 'app') { + offline = args.offline + } } else if (args.type === 'clientId') { cid = args.cid cidErrMsg = args.errMsg @@ -89,6 +94,18 @@ const API_GET_PUSH_CLIENT_ID = 'getPushClientId' export const getPushClientId = defineAsyncApi( API_GET_PUSH_CLIENT_ID, (_, { resolve, reject }) => { + // App 端且启用离线时,使用 getClientInfoAsync 来调用 + if (__PLATFORM__ === 'app' && offline) { + plus.push.getClientInfoAsync( + (info) => { + resolve({ cid: info.clientid }) + }, + (res) => { + reject(res.code + ': ' + res.message) + } + ) + return + } Promise.resolve().then(() => { if (typeof enabled === 'undefined') { enabled = false diff --git a/packages/uni-app-plus/dist/uni.runtime.esm.js b/packages/uni-app-plus/dist/uni.runtime.esm.js index 5c05142f9d85d43f56eacfef8fb964dcd49a92bf..df93084d4f77c3accfe79cbf4a2294cdf4d9b3a6 100644 --- a/packages/uni-app-plus/dist/uni.runtime.esm.js +++ b/packages/uni-app-plus/dist/uni.runtime.esm.js @@ -11075,6 +11075,7 @@ const getLaunchOptionsSync = defineSyncApi(API_GET_LAUNCH_OPTIONS_SYNC, () => { let cid; let cidErrMsg; let enabled; +let offline; function normalizePushMessage(message) { try { return JSON.parse(message); @@ -11089,6 +11090,9 @@ function normalizePushMessage(message) { function invokePushCallback(args) { if (args.type === 'enabled') { enabled = true; + { + offline = args.offline; + } } else if (args.type === 'clientId') { cid = args.cid; @@ -11127,6 +11131,15 @@ function invokeGetPushCidCallbacks(cid, errMsg) { } const API_GET_PUSH_CLIENT_ID = 'getPushClientId'; const getPushClientId = defineAsyncApi(API_GET_PUSH_CLIENT_ID, (_, { resolve, reject }) => { + // App 端且启用离线时,使用 getClientInfoAsync 来调用 + if (offline) { + plus.push.getClientInfoAsync((info) => { + resolve({ cid: info.clientid }); + }, (res) => { + reject(res.code + ': ' + res.message); + }); + return; + } Promise.resolve().then(() => { if (typeof enabled === 'undefined') { enabled = false; diff --git a/packages/uni-push/dist/uni-push.plus.es.js b/packages/uni-push/dist/uni-push.plus.es.js index 4b2a5c93f911c289b99191c7f6b98ed1dd848700..e30c14a76f5c4d78de5d5004a34f958897a42484 100644 --- a/packages/uni-push/dist/uni-push.plus.es.js +++ b/packages/uni-push/dist/uni-push.plus.es.js @@ -1,25 +1,10 @@ // @ts-expect-error uni.invokePushCallback({ type: 'enabled', + offline: true, }); Promise.resolve().then(() => { plus.push.setAutoNotification && plus.push.setAutoNotification(false); - plus.push.getClientInfoAsync((info) => { - if (info.clientid) { - // @ts-expect-error - uni.invokePushCallback({ - type: 'clientId', - cid: info.clientid, - }); - } - }, (res) => { - // @ts-expect-error - uni.invokePushCallback({ - type: 'clientId', - cid: '', - errMsg: res.code + ': ' + res.message, - }); - }); plus.push.addEventListener('click', (result) => { // @ts-expect-error uni.invokePushCallback({ diff --git a/packages/uni-push/src/plus.ts b/packages/uni-push/src/plus.ts index 98f9355fef02c15c69385b6394cef60a844b32bd..e7ddee0bb3076bf2b323d5cf485c8cbc2a83b77d 100644 --- a/packages/uni-push/src/plus.ts +++ b/packages/uni-push/src/plus.ts @@ -1,29 +1,10 @@ // @ts-expect-error uni.invokePushCallback({ type: 'enabled', + offline: true, }) Promise.resolve().then(() => { plus.push.setAutoNotification && plus.push.setAutoNotification(false) - plus.push.getClientInfoAsync( - (info) => { - if (info.clientid) { - // @ts-expect-error - uni.invokePushCallback({ - type: 'clientId', - cid: info.clientid, - }) - } - }, - (res) => { - // @ts-expect-error - uni.invokePushCallback({ - type: 'clientId', - cid: '', - errMsg: res.code + ': ' + res.message, - }) - } - ) - plus.push.addEventListener('click', (result) => { // @ts-expect-error uni.invokePushCallback({