HMDagher's avatar

Push Notification (android/ios) using LaravelNotificationChannels/FCM

I imported kreait/laravel-firebase package and generated the service-account JSON. i am trying to send a notification to android/ios using the Fcm channel provided by this package laravel-notification-channels/fcm:~2.0

I also added the firebase credentials to the .env file FIREBASE_CREDENTIALS=service-account.json

I added the following function to the notifiable model (users).

public function routeNotificationForFcm()
    {
        return $this->fcm_token;
    }

I ran the command make:notification followreq and this is the code:

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use NotificationChannels\Fcm\FcmChannel;
use NotificationChannels\Fcm\FcmMessage;
use NotificationChannels\Fcm\Resources\AndroidConfig;
use NotificationChannels\Fcm\Resources\AndroidFcmOptions;
use NotificationChannels\Fcm\Resources\AndroidNotification;
use NotificationChannels\Fcm\Resources\ApnsConfig;
use NotificationChannels\Fcm\Resources\ApnsFcmOptions;

class FollowReq extends Notification
{
    use Queueable;

    public function __construct()
    {
        //
    }

    public function via($notifiable): array
    {
        return [FcmChannel::class];
    }


    public function toFcm($notifiable)
    {
        return FcmMessage::create()
            ->setData(['data1' => 'value', 'data2' => 'value2'])
            ->setNotification(\NotificationChannels\Fcm\Resources\Notification::create()
                ->setTitle('Follow Request')
                ->setBody('Your account has been activated.')
                ->setImage('https://tikers.tech/img/core-img/favicon.ico'))
            ->setAndroid(
                AndroidConfig::create()
                    ->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
                    ->setNotification(AndroidNotification::create()->setColor('#0A0A0A'))
            )->setApns(
                ApnsConfig::create()
                    ->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios')));
    }

    // optional method when using kreait/laravel-firebase:^3.0, this method can be omitted, defaults to the default project
    public function fcmProject($notifiable, $message)
    {
        // $message is what is returned by `toFcm`
        return 'app'; // name of the firebase project to use
    }

}

I tried to send notification using this route (for testing purposes)

Route::get('/noti', function () {
    $user=\App\Models\User::select()->where('name','name')->first();
    $user->notify(new \App\Notifications\FollowReq());
});

but it is not working, can anyone guide me on how to use this fcm channel

0 likes
2 replies
nick1697's avatar

I'm having the same problem. The foFcm() method is not invoked

armanist's avatar

have the same issue here, any update on this topic?

Please or to participate in this conversation.