当前位置:   article > 正文

uniapp桌面角标修改封装(含消息推送)_uniapp 使用mqtt 进行角标替换

uniapp 使用mqtt 进行角标替换
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);
				});
			}
		})
	},



};

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

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);
		},
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

main.js

import push from 'common/js/push.js'
Vue.prototype.$push = push;
  • 1
  • 2
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/833229
推荐阅读
相关标签
  

闽ICP备14008679号