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" } }
}
Sparm
source share