小菜鸟一枚,获取授权通知的时候没有考虑到用户拒绝后显示的次数问题,测试后才发现如果用户不接受或者没有点击底栏的不用再通知,就会在进入页面后一直显示,但是设置一次性通知就会导致用户拒绝后,后期无法再通知用户,因此便设置了一天提示一次。代码如下:
// 获取订阅通知权限
checkSubscribeMessage() {
let that = this;
//设置进入时间
uni.setStorage({
key: "show-time",
data: new Date().getTime(),
});
uni.getSetting({
withSubscriptions: true, // 这里设置为true,下面才会返回mainSwitch
success: function(res) {
if (res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关
if (res.subscriptionsSetting.itemSettings != null) {
// 用户同意的消息模板id
let moIdState = res.subscriptionsSetting.itemSettings[that.tmps_ids];
if (moIdState === 'accept') {
console.log('接受了消息推送');
} else if (moIdState === 'reject') {
console.log("拒绝消息推送");
} else if (moIdState === 'ban') {
console.log("已被后台封禁");
}
} else {
// 当用户没有点击 ‘总是保持以上选择,不再询问’ 按钮。那每次执到这都会拉起授权弹窗
uni.showModal({
title: '提示',
content: '请授权开通服务通知',
showCancel: true,
success: function(res1) {
if (res1.confirm) {
uni.requestSubscribeMessage({ // 调起消息订阅界面
tmplIds: that.tmps_ids,
success(res) {
console.log("订阅消息成功");
},
fail(er) {
console.log("订阅消息失败");
}
})
}
}
})
}
} else {
console.log('订阅消息未开启');
}
},
fail: function(error) {
console.log(error);
}
})
}
以上是获取授权通知,然后在onload处设置时间
uni.getStorage({
key: "show-time",
success: (res) => {
if (!res.data || new Date().getTime() - res.data > 1000 * 60 * 60 * 24) {
console.log("超时");
that.checkSubscribeMessage();
} else {
console.log("时间没到");
}
},
fail: (err) => {
console.log("本地无", err);
this.checkSubscribeMessage();
},
});