J.M*_*ael 5 javascript reactjs service-worker
我想使用服务人员显示推送通知。在开发人员工具 -> 应用程序 -> 服务人员中,我有测试文本"a": "test message",按下推送并收到以下错误:
serviceWorker.js:48 Uncaught (in promise) TypeError: Failed to execute
'showNotification' on 'ServiceWorkerRegistration': No notification
permission has been granted for this origin.
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?当您单击按钮时,是否会弹出通知?
对于 Push 消息,您需要设置两个 API :Notification API和Push API。首先,要征得客户的同意才能发出通知。除非得到认可,否则什么都行不通。因此,在您的 index.js/App.js 中放置以下代码片段:-
function askForNPerm() {
Notification.requestPermission(function(result) {
console.log("User choice", result);
if (result !== "granted") {
console.log("No notification permission granted!");
} else {
configurePushSub();// Write your custom function that pushes your message
}
});
}
Run Code Online (Sandbox Code Playgroud)
一旦获得许可,就可以调用要显示消息的推送功能。
| 归档时间: |
|
| 查看次数: |
5045 次 |
| 最近记录: |