【发布时间】:2018-06-01 09:25:51
【问题描述】:
我正在尝试在我的 angularJS 项目中设置 firebase 以使用后台通知和应用程序通知。
我做了什么:
- 通过
npm install --save firebase安装firebase - 将带有发件人 ID 的 manifest.json 添加到根目录
- 将此脚本放入
<head>:<script src="node_modules/firebase/firebase-app.js"></script><script src="node_modules/firebase/firebase-messaging.js"></script>
-
在
<body>打开后添加此脚本:<script> var config = { apiKey: "AIzaSyDAVuWKUL-aPoGasdEi-EMR7uFN1gtgk0s", authDomain: "testproject-d71.firebaseapp.com", databaseURL: "https://testproject-12d71.firebaseio.com", projectId: "testproject-12d71", storageBucket: "", messagingSenderId: "123128116401" }; firebase.initializeApp(config); </script> -
在
<body>末尾添加这个脚本<script src="js/configFcm.js"></script>
这是:
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = 'Title';
const notificationOptions = {
body: 'body.',
icon: '/firebase-logo.png'
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
这就是我所做的一切,我没有在应用通知中配置,只有后台通知。我在控制台中遇到错误:
未捕获的代码: “消息传递/仅在 sw 中可用”
消息: “消息传递:此方法在服务工作者上下文中可用(消息传递/only-available-in-sw)。”
堆栈: “FirebaseError:消息传递:此方法在服务工作者上下文中可用。(消息传递/仅在 sw 中可用)
什么是服务工作者?它只是客户端吗?我正在从 index.html 执行这个脚本,所以我很困惑。
【问题讨论】:
标签: angularjs firebase notifications firebase-notifications