setbackgroundmessagehandler не показывает сообщение в Chrome - PullRequest
0 голосов
/ 22 сентября 2019

importScripts ('https://www.gstatic.com/firebasejs/6.3.4/firebase-app.js'); importScripts (' https://www.gstatic.com/firebasejs/6.3.4/firebase-messaging.js');

firebase.initializeApp({
apiKey: "xxxxxxxxxxxx",
authDomain: "xxxxxxxxxxxx",
databaseURL: "https://xxxxxxxxxxxx",
projectId: "xxxxxxxxxxxx",
storageBucket: "",
messagingSenderId: "xxxxxxxxxxxx",
appId: "xxxxxxxxxxxx"
})

/*
Retrieve an instance of Firebase Messaging so that it can handle background messages.
*/
const messaging = firebase.messaging()
messaging.setBackgroundMessageHandler(function (payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
const notification = JSON.parse(payload.data.notification);
// Customize notification here
const notificationTitle = notification.title;
const notificationOptions = {
body: notification.body,
icon: notification.icon
};

return self.registration.showNotification(notificationTitle,
    notificationOptions);
});

Полезная нагрузка:

{
"data":{
"notification": {
"title": "Firebase",
"body": "test",
"click_action": "http://xxxxx",
"icon": "icon.png"
}}
,
"to": "/topics/all"
}

при выполнении отладчика, данные полезной нагрузки поступают в метод setBackgroundMessageHandler,при достижении self.registration.showNotification не отображается окно всплывающего сообщения и консоль не помечает ошибку, версия рабочего стола chrome 77.0.3865.90 (сборка официальная) (64 бита) В mozilla, если отображается всплывающее сообщение

что может происходить?

Можно ли разместить диспетчер тегов Google для захвата щелчков во всплывающем окне? Для фонового процесса

с уважением

1 Ответ

0 голосов
/ 25 сентября 2019

вы пытались инициализировать службу обмена сообщениями, как это?

// init FCM 
const initFCM = async messaging => {
  try {
    const permission = await Notification.requestPermission()
    permission === 'granted' ? console.log(messaging) : console.log('Failed!')
    // console.log("Successful")
  } catch (error) {
    console.log('Error', error)
  }
}

//use on load
document.addEventListener('DOMContentLoaded', () => {
  const messaging = firebase.messaging()
  messaging.usePublicVapidKey('GET_THIS_FROM_FIREBASE_CONSOLE')
  initFCM(messaging)
})

Всегда видеть: messaging.onMessage() и messaging.onTokenRefresh()

https://firebase.google.com/docs/cloud-messaging/js/client

...