kreait / laravel-firebase

A Laravel package for the Firebase PHP Admin SDK

Home Page:https://github.com/kreait/firebase-php

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The registration token is not a valid FCM registration token

robertnicjoo opened this issue · comments

I've downloaded my adminsdk from firebase and add it to my env file like FIREBASE_CREDENTIALS="config/xxxx-xxxx-firebase-adminsdk-xxxx-xxxx.json" but when I'm trying to send notification it returns this error

[2021-09-24 11:48:42] local.ERROR: The registration token is not a valid FCM registration token {"userId":1,"exception":"[object] (Kreait\\Firebase\\Exception\\Messaging\\InvalidMessage(code: 0): The registration token is not a valid FCM registration token at /home/xxxxxxx/domains/xxxxxxx/vendor/kreait/firebase-php/src/Firebase/Exception/Messaging/InvalidMessage.php:22)

here is my code:

$uu = User::where('id', $request->input('senderId'))->first();
$deviceToken = $uu->app_token;
if(!empty($deviceToken)) {
    $factory = Firebase::auth();
    $notification = Notification::fromArray([
        'token' => $deviceToken, // token of receiver user device
        'title' => $uu->name,
        'body' => $message->note,
        'image' => null,
    ]);

    $config = ApnsConfig::fromArray([
        'headers' => [
            'apns-priority' => '10',
        ],
        'payload' => [
            'aps' => [
                'alert' => [
                    'title' => $uu->name,
                    'body' => $message->note,
                ],
                'badge' => 42,
                'sound' => 'default',
            ],
        ],
    ]);

    $messaging =  app('firebase.messaging');
    $dataToSend = [
        'to_userId' => $request->input('senderId') == $mess->chat->asReceiver->id ? $mess->chat->asSender->id : $mess->chat->asReceiver->id,
        'from_userId' => $request->input('senderId'),
        'chat_id' => $chat->id,
        'type' => 'privatechat',
        'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
    ];
    $message = CloudMessage::withTarget('token', 'rtc')
        ->withNotification($notification)
        ->withHighestPossiblePriority()
        ->withDefaultSounds()
        ->withApnsConfig($config)
        ->withData($dataToSend);
    $m = $messaging->send($message);
}

CloudMessage::withTarget('token', 'rtc')

Make sure that you use a valid registration token where you currently have rtc

CloudMessage::withTarget('token', 'rtc')

Make sure that you use a valid registration token where you currently have rtc

Thank you, can I have both token and topic at the same time?

For anyone else stumbling upon this: It's not possible to send a message to a token and a topic simultaneously, so you'll need two messages to achieve this, for example with:

$messaging = $factory->createMessaging();

$message = CloudMessage::new()
    ->withNotification(['title' => 'Title', 'body' => 'Body']);

$messaging->send($message->withChangedTarget('token', '...'));
$messaging->send($message->withChangedTarget('topic', '...'));