提交 0cbff6a5 编写于 作者: fxy060608's avatar fxy060608

fix(push): remove on push message

上级 fe76e4b6
...@@ -133,22 +133,6 @@ function initPushNotification() { ...@@ -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 { ...@@ -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
} }
...@@ -31,22 +31,6 @@ function initPushNotification() { ...@@ -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({ ...@@ -58,11 +42,4 @@ uni.invokePushCallback({
Promise.resolve().then(() => { Promise.resolve().then(() => {
initPushNotification(); initPushNotification();
plus.push.setAutoNotification && plus.push.setAutoNotification(false); plus.push.setAutoNotification && plus.push.setAutoNotification(false);
plus.push.addEventListener('receive', (result) => {
// @ts-expect-error
uni.invokePushCallback({
type: 'pushMsg',
message: result,
});
});
}); });
...@@ -23,7 +23,7 @@ export function invokePushCallback ( ...@@ -23,7 +23,7 @@ export function invokePushCallback (
) { ) {
if (args.type === 'enabled') { if (args.type === 'enabled') {
enabled = true enabled = true
if (__PLATFORM__ === 'app') { if (__PLATFORM__ === 'app-plus') {
offline = args.offline offline = args.offline
} }
} else if (args.type === 'clientId') { } else if (args.type === 'clientId') {
...@@ -76,7 +76,7 @@ export function getPushClientId (args) { ...@@ -76,7 +76,7 @@ export function getPushClientId (args) {
const hasComplete = isFn(complete) const hasComplete = isFn(complete)
// App 端且启用离线时,使用 getClientInfoAsync 来调用 // App 端且启用离线时,使用 getClientInfoAsync 来调用
if (__PLATFORM__ === 'app' && offline) { if (__PLATFORM__ === 'app-plus' && offline) {
plus.push.getClientInfoAsync( plus.push.getClientInfoAsync(
(info) => { (info) => {
const res = { const res = {
...@@ -126,8 +126,25 @@ export function getPushClientId (args) { ...@@ -126,8 +126,25 @@ export function getPushClientId (args) {
} }
const onPushMessageCallbacks = [] const onPushMessageCallbacks = []
let listening = false
// 不使用 defineOnApi 实现,是因为 defineOnApi 依赖 UniServiceJSBridge ,该对象目前在小程序上未提供,故简单实现 // 不使用 defineOnApi 实现,是因为 defineOnApi 依赖 UniServiceJSBridge ,该对象目前在小程序上未提供,故简单实现
export const onPushMessage = (fn) => { 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) { if (onPushMessageCallbacks.indexOf(fn) === -1) {
onPushMessageCallbacks.push(fn) onPushMessageCallbacks.push(fn)
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册