Firebase cloud messaging: setBackgroundMessageHandler not called

I prototype browser push notifications using FCM. I just copied the sample code from quickstart ( https://github.com/firebase/quickstart-js/tree/master/messaging ). Messages are received and displayed as they should be. But when I try to change the message in the Service Worker (messaging.setBackgroundMessageHandler), nothing happens. The service employee is called, and if I implement an event listener in this service worker for push notifications, it catches this event. But the setBackgroundMessageHandler method is never called. I am trying to do this on Chrome 54.

Any ideas what I need to do to set up the message in the service worker?

Many thanks!

+7
firebase firebase-cloud-messaging service-worker
source share
3 answers

For those experiencing the same problem, here is the answer: https://github.com/firebase/quickstart-js/issues/71

short summary: do not include the notification element in your json post.

+23
source share

When you try to send a push message, do you do it while your application is in focus or not? Because the documentation says that setBackgroundMessageHandler is called only when the web application is closed or not in the focus of the browser.

Based on sample code from quickstart ( https://github.com/firebase/quickstart-js/tree/master/messaging ).

If your application is in focus: the push message is received via messaging.onMessage () on index.html
If your application does not have focus: the push message is received through setBackgroundMessageHandler () in the worker file.

+1
source share

This is the solution that worked for me in webapp. It displays a notification with a title and body text along with the image and handles the user's click.

firebase-messages-sw.js

importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-app.js'); importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-messaging.js'); // Initialize Firebase var config = { apiKey: "YOUR_API_KEY", authDomain: "YOUR_AUTH_DOMAIN", databaseURL: "YOUR_DB_URL", projectId: "YOUR_PROJ_ID", storageBucket: "YOUR_STORAGE_BUCKET", messagingSenderId: "YOUR_SENDER_ID" }; firebase.initializeApp(config); const messaging = firebase.messaging(); messaging.setBackgroundMessageHandler(function (payload) { console.log('Handling background message ', payload); return self.registration.showNotification(payload.data.title, { body: payload.data.body, icon: payload.data.icon, tag: payload.data.tag, data: payload.data.link }); }); self.addEventListener('notificationclick', function(event) { event.notification.close(); event.waitUntil(self.clients.openWindow(event.notification.data)); }); 

JSON Message

 { "message": { "token": "YOUR_TARGET_APP_TOKEN", "data": { "title": "FCM Message", "body": "This is an FCM Message", "icon": "https://shortcut-test2.s3.amazonaws.com/uploads/role_image/attachment/10461/thumb_image.jpg", "link": "https://yourapp.com/somewhere" } } 

}

0
source share

All Articles