Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid FCM registration token #31

Closed
nicolasvahidzein opened this issue Apr 2, 2020 · 19 comments
Closed

Invalid FCM registration token #31

nicolasvahidzein opened this issue Apr 2, 2020 · 19 comments

Comments

@nicolasvahidzein
Copy link

Hello, thank you for your great package.

i retrieve my token properly from flutter and i confirm it is correct by sending a test message on firebase console. I get it on my device properly.

What could be my error?

[2020-04-02 01:47:21] local.ERROR: The registration token is not a valid FCM registration token {"userId":1,"exception":"[object] (Kreait\Firebase\Exception\Messaging\InvalidMessage(code: 400): The registration token is not a valid FCM registration token at D:\WebServer\ZeinTekWebServices\liveroot\merlin-core\vendor\kreait\firebase-php\src\Firebase\Exception\Messaging\InvalidMessage.php:43)
[stacktrace]

Thank you

                 $deviceToken = 'cO_GzDW6jaE:APA91bEBnhoMPRWg6A_tGnGE-gJxgEsOoaZKjHSf4AlPRQozdfCWoJqxUOWnEn37rPUAZ49ZGDp0Sp-VuSME8mjdBkCFvhsCr-tIKyUL8Ro4u45AyhR4AwQZta_3vR_9DPOieTGekCJc';


		$title = 'Package is being delivered';
		$body = 'Your order 9847GH8474 is on it\'s way to you.';
		$imageUrl = 'http://lorempixel.com/400/200/';
		
		$notification = Notification::fromArray([
			'title' => $title,
			'body' => $body,
			// 'image' => $imageUrl,
		]);
		
		$notification = Notification::create($title, $body);
		
		$data = [
			'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
			'action' => 'new_notification',
			'notification_id' => '888888888',
			'notification_uid' => '1111-2222-3333-4444',
			'notification_foreignUID' => '5555-6666-7777-8888',
			'notification_foreignUID_type' => 'user',
			'notification_title' => $title,
			'notification_message' => $body,
			'notification_asset_type' => 'image',
			'notification_image' => 'http://f48aa427.ngrok.io/storage/images_products/860a6ac4-bd66-4c3b-b07f-450f7cdcc007_2020-01-20_15-15-21_0001.jpg',
			'notification_icon' => null,
			'notification_account_type' => 'client',
			'notification_read_status' => 0,
			'notification_status' => 'active',
			'notification_created_at' => '2020-03-29 02:09:00',
			'notification_updated_at' => '2020-03-29 02:09:00',
		];
		
		
		$message = CloudMessage::withTarget('token', $deviceToken)
		->withNotification($notification) // optional
		->withData($data);// optional
		
		$message = CloudMessage::fromArray([
			'token' => $deviceToken,
			'notification' => [/* Notification data as array */], // optional
			'data' => [/* data array */], // optional
		]);
		
		$messaging = app('firebase.messaging');
		
		$messaging->send($message);
		
		
		
		
@nicolasvahidzein
Copy link
Author

Nevermind, i had not saved my file, it was an old token. Sorry for the bother and thanks again!!!

@jeromegamez
Copy link
Member

Sometimes one has to just write it down to see it. I love Rubber Duck debugging and am glad you figured it out! 🥳 Anytime again 😊

@nicolasvahidzein
Copy link
Author

@jeromegamez Thank you Jerome. What is rubber duck?

@nicolasvahidzein
Copy link
Author

@jeromegamez LOL i should be ashamed, i have a whale, dolphin, octopus and i yet i still failed!!!

@jeromegamez
Copy link
Member

In software engineering, rubber duck debugging is a method of debugging code. The name is a reference to a story in the book The Pragmatic Programmer in which a programmer would carry around a rubber duck and debug their code by forcing themselves to explain it, line-by-line, to the duck.[1] Many other terms exist for this technique, often involving different (usually) inanimate objects.

Many programmers have had the experience of explaining a problem to someone else, possibly even to someone who knows nothing about programming, and then hitting upon the solution in the process of explaining the problem.

image

https://en.wikipedia.org/wiki/Rubber_duck_debugging

😅

@nicolasvahidzein
Copy link
Author

Thanks @jeromegamez i never knew it had a name, i do this often by just talking to myself like a crazy person but actually talking to someone like i did yesterday with my mentor Jonathan White it helped a lot.

@RahulDey12
Copy link

I am also facing this issue

@jeromegamez
Copy link
Member

@RahulDey12 Without more info, the token you‘re using is on of these:

  • not in valid format
  • registered with another project
  • expired/refreshed = not registered with your project anymore

Please make sure that you‘re using a current registration token of a device that‘s registered with your project.

@RahulDey12
Copy link

How can I handle refresh token

@nicolasvahidzein
Copy link
Author

check your token @RahulDey12 i made the same mistake.

@nicolasvahidzein
Copy link
Author

@RahulDey12 do it on your device or frontend, it's not something you do in your backend.

@jeromegamez
Copy link
Member

jeromegamez commented Apr 12, 2020

From the error docs at https://firebase.google.com/docs/cloud-messaging/send-message#admin

A previously valid registration token can be unregistered for a variety of reasons, including:

The client app unregistered itself from FCM.

The client app was automatically unregistered.
This can happen if the user uninstalls the application or, on iOS, if the APNS Feedback Service reported the APNS token as invalid.

The registration token expired. For example, Google might decide to refresh registration tokens or the APNS token may have expired for iOS devices.

The client app was updated, but the new version is not configured to receive messages.

For all these cases, remove this registration token and stop using it to send messages.

@RahulDey12
Copy link

Ok so i ran a cron job which deletes all invalid token on midnight from my database

@nicolasvahidzein
Copy link
Author

@RahulDey12 can you share how you delete invalid tokens? How do you even know a token is invalid?

@jeromegamez
Copy link
Member

@nicolasvahidzein Depending on how you are sending the messages solutions could look like this:

When sending to a single token

use Kreait\Firebase\Exception\Messaging\InvalidArgument;
use Kreait\Firebase\Exception\Messaging\NotFound;

try {
    $messaging->send($message, $token);
} catch (NotFound $e) {
    // $token is not registered to the project (any more)
    // Handle the token (e.g. delete it in a local database)
} catch (InvalidArgument $e) {
    // $token is not a *valid* registration token, meaning
    // the format is invalid, OR the message was invalid
}

When sending to multiple tokens

$report = $messaging->sendMulticast($message, $tokens);

foreach ($report->unknownTokens() as $unknownToken) {
    // Handle unknown token
}

foreach ($report->invalidTokens() as $invalidToken) {
    // Handle invalid tokens
}

@RahulDey12
Copy link

@nicolasvahidzein It is very simple to check the Invalid token. Checkout this link https://developers.google.com/instance-id/reference/server

But @jeromegamez 's method is more efficient I think.

@nicolasvahidzein
Copy link
Author

Thank you very much Jerome @jeromegamez. You as well @RahulDey12 .

@rahmat-ihsan
Copy link

I use Kreait\Firebase\Exception\Messaging\InvalidMessage instead of InvalidArgument because the error message said InvalidMessage, not InvalidArgument.
thank you btw.

@nicolasvahidzein Depending on how you are sending the messages solutions could look like this:

When sending to a single token

use Kreait\Firebase\Exception\Messaging\InvalidArgument;
use Kreait\Firebase\Exception\Messaging\NotFound;

try {
    $messaging->send($message, $token);
} catch (NotFound $e) {
    // $token is not registered to the project (any more)
    // Handle the token (e.g. delete it in a local database)
} catch (InvalidArgument $e) {
    // $token is not a *valid* registration token, meaning
    // the format is invalid, OR the message was invalid
}

When sending to multiple tokens

$report = $messaging->sendMulticast($message, $tokens);

foreach ($report->unknownTokens() as $unknownToken) {
    // Handle unknown token
}

foreach ($report->invalidTokens() as $invalidToken) {
    // Handle invalid tokens
}

@jeromegamez
Copy link
Member

@rahmat-ihsan This issue is over two years old, with a new major release in between. Please refer to the current documentation at https://firebase-php.readthedocs.io/en/stable/cloud-messaging.html#validating-messages

If you find an error in the documentation somewhere, feel free to create a PR, thanks! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants