Vis*_*hnu 2 firebase google-cloud-messaging service-worker firebase-cloud-messaging
我一直在尝试在前端自定义通知消息,即如果未设置字段发送通知,我正在尝试添加它。
importScripts('https://www.gstatic.com/firebasejs/5.0.4/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/5.0.4/firebase-messaging.js');
var config = {
apiKey: "x",
authDomain: "y",
databaseURL: "z",
projectId: "a",
storageBucket: "b",
messagingSenderId: "1"
};
firebase.initializeApp(config);
const messaging = firebase.messaging();
console.log('came here');
console.log(messaging.bgMessageHandler);
console.log(messaging.setBackgroundMessageHandler,'dsafdsadasfd')
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
var notificationTitle = 'Background Message Title';
var notificationOptions = {
body: 'Background Message body.',
icon: '/firebase-logo.png'
};
console.log(notificationOptions)
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
console.log(messaging.bgMessageHandler);
Run Code Online (Sandbox Code Playgroud)
在执行上面的代码时[firebase-messaging-sw.js] Received background message ', payload,即使我收到通知,我也没有收到 的控制台。
为什么setBackgroundMessageHandler不工作?
小智 5
当应用程序在后台运行时,您发送消息的 json 请求中似乎有问题。
注意:如果您在 HTTP 或 XMPP 发送请求中设置通知字段,则这些值优先于服务工作线程中指定的任何值。
https://firebase.google.com/docs/cloud-messaging/js/topic-messaging
因此,以下格式不会调用后台处理程序:
{
to: "e-DLMv........._DiL",
notification: {
body: "Backgound-Message"
}
}Run Code Online (Sandbox Code Playgroud)
发送带有内部数据通知的消息(它将起作用):
{
to: "e-DLMv........._DiL",
data: {
notification: {
body: "Backgound-Message"
}
}
}Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4676 次 |
| 最近记录: |