javascript - 在 chrome 中显示推送消息
问题描述
我想使用服务人员显示推送通知。在开发人员工具 -> 应用程序 -> 服务工作者中,我有测试文本"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.
我怎样才能解决这个问题?当您单击按钮时,是否会弹出通知?
解决方案
对于推送消息,您需要两个 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
}
});
}
一旦您获得许可,然后调用您的推送函数,该函数具有要显示的消息。
推荐阅读
- linux - IPv6 示例程序在 connect() 上失败
- android - 从活动传递到片段时为空包
- python - 当我在 Django 中运行我的服务器时,我得到模板不存在
- racket - 在球拍中编写嵌套的while循环
- c++ - 用于装箱和拆箱的 C++ 类
- javascript - 为什么此代码跳过添加到列表对象?
- c - 这段代码有什么问题?它给出错误 - 需要作为增量操作数的左值
- authentication - 如何使用 apollo graphql 处理授权标头?
- visual-studio-code - 如何在 vscode 中更改设置输入背景颜色?
- php - 查询:具有可定制变量(如颜色、尺寸)的产品数量