How to Register a device for Push notification and get a Token

Vuyiswa Maseko 351 Reputation points
2022-05-07T22:16:26.66+00:00

i trap the token creation and store it in the db

[Service(DirectBootAware = true, Exported = false, Enabled = true)]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class CustomFirebaseMessagingService : FirebaseMessagingService
{
    public override void OnNewToken(string token)
    {
        try
        {
            base.OnNewToken(token);
            GenericMethods.PUSH_TOKEN = token;
            Task.Run(() => GenericMethods.StorePushToken(token));
        }
        catch (Exception ex)
        {
            Crashes.TrackError(ex);
        }
    }

and i have subscribed to the change on the App.cs

    CrossFirebasePushNotification.Current.OnTokenRefresh += Current_OnTokenRefresh;

for some reason some users who register on my app dont have token saved and i dont get an error . Their device are in the supported range of android and non Huawei . I want to manually register the device and get a token , how can i do that programmatically ?

Thanks

Developer technologies .NET Xamarin
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2022-05-09T02:35:43.55+00:00

    Hello,

    for some reason some users who register on my app dont have token saved and i dont get an error.

    Your code seems no problem, Please check the method GenericMethods.StorePushToken, it would be the reason the error occurs.

    manually register the device and get a token

    It's no way to set the device token for Firebase Cloud Messaging by yourself. It would actually be an antipattern to set a user's ID, due to the same user may end up using the app on different devices.

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.