laravel-notification-channels / fcm

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

notify returns null #203

Closed neokofg closed 1 month ago

neokofg commented 1 month ago

Hello, i'm trying to send message to client:

dd(User::first()->notify(new \App\Notifications\CRM\AlmostAuthorized()));

it doesn't send anything and returns this:

null // vendor\psy\psysh\src\ExecutionLoopClosure.php(52) : eval()'d code:1

my Notification

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

    public function toFcm($notifiable): FcmMessage
    {
        return (new FcmMessage(notification: new FcmNotification(
            title: 'some title',
            body: 'some body',
            image: 'https://via.placeholder.com/640x480.png/00bb77?text=test'
        )))
        ->data(['type' => 'notification'])
        ->custom([
            'android' => [
                'notification' => [
                    'color' => '#0A0A0A',
                ],
                'fcm_options' => [
                    'analytics_label' => 'analytics',
                ],
            ],
            'apns' => [
                'fcm_options' => [
                    'analytics_label' => 'analytics_ios',
                ],
            ],
        ]);
    }

    /**
     * Get the array representation of the notification.
     *
     * @return array<string, mixed>
     */
    public function toArray(object $notifiable): array
    {
        return [
            //
        ];
    }
}

my user have routeNotificationForFcm, and its normally returns device token:

public function routeNotificationForFcm(): array|string
{
    return $this->devices()->pluck('token')->toArray()[0];
}

public function devices(): MorphMany
{
    return $this->morphMany(Device::class, 'userable');
}

i connected FIREBASE_CREDENTIALS normally

//.env
FIREBASE_CREDENTIALS=storage/firebase.json
// storage/firebase.json
{
    "type": "service_account",
    "project_id": "projectid",
    "private_key_id": "privatekeyid",
    "private_key": "bigprivatekey",
    "client_email": "fooemail",
    "client_id": "someid",
    "auth_uri": "url",
    "token_uri": "tokenurl",
    "auth_provider_x509_cert_url": "someurl",
    "client_x509_cert_url": "someurl",
    "universe_domain": "somedomain.com"
}
dwightwatson commented 1 month ago

A few things to note here:

  1. The notify method has no return value, so that is expected.
  2. Your route method could actually be an issue if the user has no tokens and you're trying to get the first element. You would be better off just returning the tokens.
public function routeNotificationForFcm(): array
{
    return $this->devices()->pluck('token')->toArray();
}

To debug this you may want to add an event listener on the NotificationFailed event or just add some dds in the channel to see what responses your app is receiving.

neokofg commented 1 month ago

Ty, it was curl problem