赞
踩
module.exports = { getAllMessage() { return plus.push.getAllMessage(); }, changeBadge(number) { const messages = this.getAllMessage(); plus.runtime.setBadgeNumber(number || messages.length); }, /** * @param {string} content 消息内容 * @param {Object} payload 消息承载的数据 * @param {Object} options 消息配置 * @param {Boolean} isChange 创建消息后是否更新BadgeNumber * @example * this.$push.create("点击查看消息内容>>", {url: "/pages/xxxx/xxx"}, { delay: 3, title: "您有3条新的消息", }); */ create(content, payload = {}, options, isChange) { plus.push.createMessage(...arguments); const delay = options.delay ? (~~options.delay * 1000) : 0; setTimeout(() => { isChange && this.changeBadge(); }, delay) }, /** * @param {number} message * @description * - 传入message时,删除消息列表中的message; * - 不传参数:则全部清空 */ remove(message, isChange) { message ? plus.push.remove(message) : plus.push.clear(); isChange && this.changeBadge(); }, /** * @description 需要在App.vue中onLaunch中调用 */ init() { plus.push.setAutoNotification(true); // setAutoNotification 默认就是true plus.push.addEventListener("click", (message) => { let { url } = message.payload; if (url) { uni.navigateTo({ url, success: () => {}, // 可以根据需求删除消息 this.remove(message); complete: () => {} // 可以根据需求删除消息 this.remove(message); }); } }) }, };
APP.vue
onLaunch: function() {
this.$push.init();
plus.push.addEventListener("click", res => {
this.clickPush(res.payload);
}, false);
plus.push.addEventListener("receive", res => {
this.receivePush(res.payload);
}, false);
},
main.js
import push from 'common/js/push.js'
Vue.prototype.$push = push;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。