A rookie, when obtaining the authorization notification, it did not take into account the number of times the user refused to display it. After testing, it was found that if the user does not accept or click the bottom bar, no further notification will be displayed after entering the page, but the setting A one-time notification will cause the user to refuse, and the user cannot be notified later, so a reminder is set once a day. code show as below:
// Obtain the subscription notification permission
checkSubscribeMessage() { let that = this;
//Set the entry time
uni.setStorage({ key: "show-time", data: new Date().getTime(), }); uni.getSetting({ withSubscriptions: true, // set to true here, the following will It will return mainSwitch success: function(res) { if (res.subscriptionsSetting.mainSwitch) { // The user has turned on the main switch of the subscription message if (res.subscriptionsSetting.itemSettings != null) { // The message template id agreed by the user let moIdState = res.subscriptionsSetting.itemSettings[that.tmps_ids]; if (moIdState === 'accept') {
console.log('Accept message push');
} else if (moIdState === 'reject') { console.log("Reject message push"); } else if (moIdState === 'ban') { console .log("Banned by the background"); } } else { // When the user does not click the 'Always keep the above selection, never ask' button. Then every time you get here, the authorization pop-up window will be pulled up uni.showModal({ title: 'Prompt', content: 'Please authorize to activate the service notification',
} , _ _ _ _ _ fail(er) { console.log("Failed to subscribe to message"); } })
}
}
})
}
} else { console.log('Subscription message is not enabled'); } }, fail: function(error) { console.log(error); } }) }
The above is to get the authorization notification, and then set the time at onload
uni.getStorage({ key: "show-time", success: (res) => { if (!res.data || new Date().getTime() - res.data > 1000 * 60 * 60 * 24) { console.log("Timeout"); that.checkSubscribeMessage(); } else { console.log("Timeout"); } }, fail: (err) => { console.log("None locally", err); this. checkSubscribeMessage(); }, });