FirebaseMessagingException: The registration token is not a valid FCM registration token

Vuyiswa Maseko 351 Reputation points
2022-04-12T21:06:13.47+00:00

Good Day All

i have a Xamarin App , i want to implement Push Notification. i use FirebaseAdmin to send this notifications to a specific user.

When the app loads for the first time i trap the token and store it in the database and in my app.cs i also subscribed to the notification refresh event so i trap the changes of the device token as it changes.

CrossFirebasePushNotification.Current.OnTokenRefresh += Current_OnTokenRefresh;

and in this event i go and update the token on the db.

i created a webapi that can be called by the app to send push notification , i hardcoded the token by copying it from the db table and my app is on release mode on the emulator

on the web api i called it via Fiddler which a different project and debugged it to check if there is any other issues , everything gets executed correctly with no issues except the last line

string response = FirebaseMessaging.DefaultInstance.SendAsync(message).Result;

and it gaves me an error

"FirebaseMessagingException: The registration token is not a valid FCM registration token"

and the example code that tries to sent the Push notification is as below

try
            {
                PUSH_MESSAGE model = new PUSH_MESSAGE();
                model.SENDER_HANDLE = "vuyiswamb";
                model.RECEIVER_TOKEN = "e-itrrcdtoodv7rkx6g2yy:apa91bfu1hczrzpaubya9umv3d3guzt_dcz8zhfgbxaltse4fbamcv2kklf6ekdmxhg_g9inwamja8-6vfzfrlqkpm8mi956ncfcsa-jrsmp50qsbblehgiz9ezo7ih3h1aavrodl_jn";
                model.SENDER_USER_ID = 1;
                model.TITLE = "Hello Girl";
                model.MESSAGE_BODY = "How this is working";
                model.SENDER_USER_TOKEN = new Guid("84819756-A839-45A0-ACB3-08BCABA013BB");

                if (model.SENDER_USER_TOKEN != new Guid("00000000-0000-0000-0000-000000000000"))
                {
                    FirebaseApp.Create(new AppOptions()
                    {
                        Credential = GoogleCredential.FromFile(System.Web.Hosting.HostingEnvironment.MapPath(@"~/private_key.json"))
                    });

                    var registrationToken = model.RECEIVER_TOKEN;

                    var message = new Message()
                    {
                        Data = new Dictionary<string, string>()
                    {
                        { "SENDER_USER", model.SENDER_USER_ID.ToString() },
                    }, 
                        Token =registrationToken,
                        //Topic = "TrovaPush",
                        Notification = new Notification()
                        {
                            Title = model.TITLE,
                            Body = model.MESSAGE_BODY,
                            ImageUrl = model.ICON_URL
                        }
                    };

                    // Send a message to the device corresponding to the provided 
                    string response = FirebaseMessaging.DefaultInstance.SendAsync(message).Result;

                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

The Token is the latest i have in the database

Thanks

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,312 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 37,506 Reputation points Microsoft Vendor
    2022-04-13T02:56:47.967+00:00

    Hello,

    When sending to an invalid registration token, you'll should receive error:InvalidRegistration.

    Check the format of the registration token you pass to the server. Make sure it matches the registration token the client app receives from registering with Firebase Notifications. Do not truncate or add additional characters.

    You need to check whether the token changed during your application running.

    You can refer to Registration with FCM and Set up a Firebase Cloud Messaging client app on Android to make sure that your steps are in compliance with the official specification.

    If you want to test your api, you can refer to Downstream HTTP messages and search dry_run.

    This parameter, when set to true, allows developers to test a request without actually sending a message.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful