FCM - Android Xamarin NotRegistered error when sending message after re-debugging the App

后端 未结 2 812
故里飘歌
故里飘歌 2021-01-07 00:47

I am developing an App in Xamarin Android, for notifications I am using FCM the Pre-Release package: https://www.nuget.org/packages/Xamarin.Firebase.Messaging/

Now e

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-07 01:16

    i'm manualy trying to initialize like this

     var options = new FirebaseOptions.Builder()
                 .SetApplicationId("YOURAPPID")
                 .SetApiKey("YOURAPIKEY")
                 //.SetDatabaseUrl(Keys.Firebase.Database_Url) //i'M not using it
                 .SetGcmSenderId("YOURSENDERID")
        //.SetStorageBucket(Keys.Firebase.StorageBucket)//i'M not using it
        .Build();
            try
            {   //try to initilize firebase app to get token
                FirebaseApp.InitializeApp(Forms.Context, options);//initializeto get token
            }
            catch
            {   //if app already initialized it will throw exception, so get the previous active token and send to your server-database etc
                var instanceId = FirebaseInstanceId.Instance;
                var token = instanceId.Token;
                Service.MyFirebaseMessagingService.RegisterForAndroid(token); //this method sends the token to my server app, you have to write your own
            }
    

    So when user opens the app, I'm trying to reinitialize the Firebase app. If it is already initalized it will throw an exception :) I'm taking the token there so it gives me the active registered token. If app is not initialized everything will work on smoothly and so your OnTokenRefresh method will be fired as expected. Hope this helps you.

提交回复
热议问题