I have made the Firebase Cloud Message Notification working even work through the Notification Hub from Azure. The time I can't receive the message is when I try to re-run the application.
Process:
1) Fresh install the application with Visual Studio IDE
2) Stop the debugger
3) Debug and run the application again through Visual Studio IDE
4) Send a test message through FCM Console
If I am not doing 3, I still can receive the message even if the application is in background
LoadApplication(new App());
FirebaseOptions options = new FirebaseOptions.Builder()
.SetApplicationId(GetString(Resource.String.fcmAppId))
.SetApiKey(GetString(Resource.String.fcmApiKey))
.SetGcmSenderId(GetString(Resource.String.fcmGCMSenderId))
.Build();
FirebaseApp.InitializeApp(Android.App.Application.Context, options);
?
public class MyFirebaseInstanceIdService : FirebaseInstanceIdService
{
const string TAG = "MyFirebaseInstanceIdService";
public override void OnTokenRefresh()
{
var refreshedToken = FirebaseInstanceId.Instance.Token;
Settings.NotificationToken = refreshedToken;
Android.Util.Log.Debug(TAG, "Refreshed token: " + refreshedToken);
SendRegistrationToServer(refreshedToken);
}
void SendRegistrationToServer(string token)
{
}
}
Initially I thought it has a different token but is the same.
Message from Azure Portal:
The token obtained from the token provider is wrong.
As the GCM docs said :
So only when the token is available, your app can receive message from
GCM
.How can an app get that token?
You could see the document :
Every time when you re-run the application in your device the token is changed, but as the document said,
OnTokenRefresh
is only called when the system determines that the tokens need to be refreshed, it is needed for key rotation and to handle Instance ID changes due to :You need to trigger
OnTokenRefresh
method. You should first uninstall the app from the device, then reinstall the app and open it, theOnTokenRefresh
will be triggered and the token will be updated and your app could receiveGCM
message again.