jaa*_*lah 3 javascript google-chrome firebase-cloud-messaging
我正在使用FCM发送网络通知.我收到此警告并单击通知不会给出通常的结果(打开通知URL).这是Service-Worker代码
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js');
firebase.initializeApp({
'messagingSenderId': '<my senderid>'
});
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
self.addEventListener('notificationclick', function(event) {
event.notification.close();
var promise = new Promise(function(resolve) {
setTimeout(resolve, 1000);
}).then(function() {
return clients.openWindow(payload.data.locator);
});
event.waitUntil(promise);
});
var notificationTitle = payload.data.title;
var notificationOptions = {
body: payload.data.body,
icon: payload.data.icon
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});Run Code Online (Sandbox Code Playgroud)
firebase-messaging-sw.js:108 Event handler of 'notificationclick' event must be added on the initial evaluation of worker script.
Run Code Online (Sandbox Code Playgroud)
这是警告信息,108引用此行
self.addEventListener('notificationclick', function(event) {
这是在Chrome版本55.在Firefox上,没有任何错误,它运行完全正常.提前致谢.
Kas*_*ady 13
你应该搬家
self.addEventListener
Run Code Online (Sandbox Code Playgroud)
功能之外
messaging.setBackgroundMessageHandler
Run Code Online (Sandbox Code Playgroud)
这样您只需添加一次EventListener.
您的完整代码应如下所示
const messaging = firebase.messaging();
self.addEventListener('notificationclick', function(event) {
event.notification.close();
var promise = new Promise(function(resolve) {
setTimeout(resolve, 1000);
}).then(function() {
return clients.openWindow(event.data.locator);
});
event.waitUntil(promise);
});
messaging.setBackgroundMessageHandler(function(payload) {
var notificationTitle = payload.data.title;
var notificationOptions = {
body: payload.data.body,
icon: payload.data.icon,
locator:payload.data.locator
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3059 次 |
| 最近记录: |