angular - Firebase 消息服务工作人员发送通知
问题描述
在 Firebase Messaging 文档中,有一个示例说明如何通过 javscript 文件中注册的 serviceworker 向浏览器发送通知:
messaging.onBackgroundMessage((payload) => {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: '/firebase-logo.png'
};
self.registration.showNotification(notificationTitle,
notificationOptions);
});
但遗憾的是,没有任何例子可以为前台做同样的事情:
const messaging = firebase.messaging();
this.afMessaging.messages
.subscribe((message) => {
const title = 'TEST';
const options = {
body: message["data"].text,
icon: 'assets/icon.png',
badge: 'assets/badge.png',
actions: [
{
action: 'archive',
title: 'archive'
}
]
};
//let notification = new Notification(title, options);
navigator.serviceWorker.getRegistration().then(reg => reg.showNotification(title, options));
我以前用过,new Notification
但这不支持动作。所以我需要使用 serviceworker 的 showNotification,但是如何从打字稿代码中访问它。
使用当前的实现,我总是会收到错误消息:
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'showNotification' of undefined
TypeError: Cannot read property 'showNotification' of undefined
解决方案
推荐阅读
- javascript - 我如何知道数组对象中有多少具有不同日期或日期的数据
- javascript - 抽屉导航器内的堆栈导航器出现错误
- javascript - 如何从不等于给定 ID 的 firebase 集合中获取第一个文档 ID?
- c++ - 运行命令“rosbag play example.bag”时发生错误。找不到与插件 rosbag/NoEncryptor 对应的库
- microsoft-graph-api - 如何使用 Graph API 获取所有 Azure AD b2c 用户的个人资料图片
- php - php strtotime 函数没有给出输出
- firebase - 如何持久化 Firebase UserCredential 对象,因此可以使用 UserCredential.sendEmailVerification() 函数
- html - 形成动作=“/登录”?斜线是什么意思?
- python - pyproj 意外返回 nan 方位角(方位角)
- reactjs - 如何测量 useCallback 和 useMemo 性能