laravel-notification-channels / fcm

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

How to send Data Message notification type? #76

Closed sncbtz closed 2 years ago

sncbtz commented 3 years ago

Is there a way to set the notification type as mentioned in the FCM docs?

I'm having problems implementing with React Native (expo managed projec) and found a little piece of advice in the expo docs:

Notification messages are only handled (and displayed) by the Firebase library, meaning they won't necessarily wake the app, and expo-notifications will not be made aware that your app has received any notification. (source)

warhsn commented 3 years ago

@soyjesus did you manage to find a way to send expo-notifications? We're also i need of sending data only notifications.

sudoanand commented 3 years ago

I'm building Finni.app and we are using the following Notification class to send data-only messages.

<?php

namespace App\Notifications;

.........

class AppAlertDashboardReady extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [FcmChannel::class];
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
                //
            ];
    }

    public function toFcm($notifiable)
    {
        $message = "You can view your financial report now";

        return FcmMessage::create()
          ->setNotification(null)
          ->setData([
            'type' => 'testtext',
            'title' => 'Your Finni Dashboard is Ready',
            'body' => $message
          ])
          ->setAndroid(
              AndroidConfig::create()
                  ->setFcmOptions(
                      AndroidFcmOptions::create()->setAnalyticsLabel(
                          'analytics'
                      )
                  )                  
          );
    }
}
dwightwatson commented 2 years ago

Happy to look over any PR that wants to take a stab at implementing this.