I am implementing the push notifications in my xamarin.forms app using azure notification hub following this documentation from Microsoft. The implementation works fine and I am able to receive notifications on both android and ios devices. I was testing some use cases and came across the issue with token retrieval in Android if for some reason the app crashes before the device registers with Azure notification hub. Below is my firebase service code.
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class FirebaseService : FirebaseMessagingService
{
readonly AndroidHelper androidHelper = new AndroidHelper();
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
androidHelper.SendLocalNotification(message.Data);
}
//Send pns handle for device installation with Azure Notification Hub.
public override void OnNewToken(string token)
{
PushNotifications.PNSHandle = token;
// APP CRASH OCCURS
}
}
Whenever the token is generated, I store it in a static variable and use it for the device registration later.
But since OnNewToken() only runs when the app is launched first time after install. If for some reason let's say I get a crash right after I assign the token to my static variable as mentioned in the comment above inside OnNewToken() and I restart the app, I don't get that token again.
Can anyone please help with token retrieval or regeneration again on every app launch. After searching I came across this post from xamarin forums but not able to get what I am looking for.