laravel-notification-channels / fcm

Firebase Cloud Messaging (FCM) notifications channel for Laravel
https://laravel-notification-channels.com/
MIT License
476 stars 123 forks source link

How do i properly configure for the web push with deeplink? #147

Closed sarozpradhan64 closed 11 months ago

sarozpradhan64 commented 1 year ago

https://github.com/laravel-notification-channels/fcm#usage

dwightwatson commented 1 year ago

Under the hood we use: https://github.com/kreait/laravel-firebase

You may want to look into their documentation to see how to configure for your needs.

gbaggaley commented 8 months ago

For anyone else trying to get this to work, try this in your notification:

$notification_content = [
    'title' => 'Notification Title',
    'body' => 'Notification body',
    'image' => 'URL to the icon for the notification' ,
];

return (new FcmMessage(notification: new FcmNotification(
    ...$notification_content
)))->custom([
    'webpush' => [
        'fcm_options' => [
            'link' => route('welcome'),
        ]
    ]
]);

Then in your firebase service worker, add the following:

messaging.onBackgroundMessage((payload) => {
    // Customize notification here

    const notificationTitle = payload.notification.title;
    const notificationOptions = {
        body: payload.notification.body,
        icon: payload.notification.image ?? "/icon-256x256.png",
        click_action: payload.data.link,
    };

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