firebase 设置后台消息处理程序
firebase set background message handler
我一直在尝试在前端自定义通知消息,即如果未设置发送通知的字段,我正在尝试添加它。
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);
执行上述代码时,我没有收到 [firebase-messaging-sw.js] Received background message ', payload
的控制台,即使我收到通知。
为什么 setBackgroundMessageHandler
不工作?
您在后台 运行 时发送消息的 json 请求似乎有问题。
注意:如果您在 HTTP 或 XMPP 发送请求中设置通知字段,则这些值优先于服务工作者中指定的任何值。
https://firebase.google.com/docs/cloud-messaging/js/topic-messaging
所以,以下格式不会调用后台处理程序:
{
to: "e-DLMv........._DiL",
notification: {
body: "Backgound-Message"
}
}
在数据中发送带有通知的消息(它会起作用):
{
to: "e-DLMv........._DiL",
data: {
notification: {
body: "Backgound-Message"
}
}
}
我一直在尝试在前端自定义通知消息,即如果未设置发送通知的字段,我正在尝试添加它。
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);
执行上述代码时,我没有收到 [firebase-messaging-sw.js] Received background message ', payload
的控制台,即使我收到通知。
为什么 setBackgroundMessageHandler
不工作?
您在后台 运行 时发送消息的 json 请求似乎有问题。
注意:如果您在 HTTP 或 XMPP 发送请求中设置通知字段,则这些值优先于服务工作者中指定的任何值。
https://firebase.google.com/docs/cloud-messaging/js/topic-messaging
所以,以下格式不会调用后台处理程序:
{
to: "e-DLMv........._DiL",
notification: {
body: "Backgound-Message"
}
}
在数据中发送带有通知的消息(它会起作用):
{
to: "e-DLMv........._DiL",
data: {
notification: {
body: "Backgound-Message"
}
}
}