mo-ah-dawood / fcm_config

10 stars 12 forks source link

[HELP] Change web notification icon #13

Closed ReniDelonzek closed 3 years ago

ReniDelonzek commented 3 years ago

I didn't find anything in the documentation, and looking at the code it doesn't seem to be implemented, but I'd like to confirm this/request the feature

mo-ah-dawood commented 3 years ago

displayNotificationWith( ) has property called web it takes WebNotificationDetails you can use it to display with icon default is /icons/icon-192.png

but if you need to change the incomming notification from fcm you have to change it in service worker see this

ReniDelonzek commented 3 years ago

I followed the code indicated, but my app icon is still not displayed in the notification, am I missing something?

const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function (payload) {
    console.log('[firebase-messaging-sw.js] Received background message ', payload);
    // Customize notification here
    const notificationOptions = {
        body: payload.body,
        icon: 'icons/Icon-192.png'
    };

    return self.registration.showNotification(payload.notificationTitle,
        notificationOptions);
});
ReniDelonzek commented 3 years ago
Captura de Tela 2021-08-22 às 12 56 41
ReniDelonzek commented 3 years ago

Found the problem, the indicated documentation is out of date, the method is now onBackgroundMessage

...
const messaging = firebase.messaging();
messaging.onBackgroundMessage(function (payload) {
    const notificationOptions = {
        body: payload.notification.body,
        icon: '/icons/Icon-192.png'
    };
    return self.registration.showNotification(payload.notification.title,
        notificationOptions);
});

That worked for me. Thanks!