我有一个Xamarin表单iOS应用程序,可与Firebase Cloud Messaging一起使用,以使用Xamarin.Firebase.iOS.CloudMessaging v3.1.2进行推送通知
我可以在DidReceiveRegistrationToken
方法中正确获取FCM令牌,然后调用REST端点https://fcm.googleapis.com/fcm/send
,通知将按预期发送。
问题是,每当有未处理的异常或当前线程被手动终止且我的应用崩溃时,FCM令牌将被注销,并且相同的API调用将导致"error": "NotRegistered"
响应。
到那时,我所能做的就是卸载并重新安装该应用程序以获取新的FCM令牌,然后一切恢复正常。
这是我的AppDelegate.cs中主要的Firebase设置代码
public void RegisterFirebaseNotifications()
{
UNUserNotificationCenter.Current.Delegate = this;
var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
{
if (error != null)
{
Log.Error("Error occurred getting authorization from user app");
Log.Error(error.Description);
}
});
UIApplication.SharedApplication.RegisterForRemoteNotifications();
Messaging.SharedInstance.Delegate = this;
// To connect with FCM. FCM manages the connection, closing it
// when your app goes into the background and reopening it
// whenever the app is foregrounded.
Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
}
#region IMessagingDelegate
[Export("messaging:didReceiveRegistrationToken:")]
public void DidReceiveRegistrationToken(Messaging messaging, string fcmToken)
{
Utils.Settings.FirebaseNotificationToken = fcmToken;
}
[Export("messaging:didReceiveMessage:")]
public void DidReceiveMessage(Messaging messaging, RemoteMessage remoteMessage)
{
HandleMessage(remoteMessage.AppData);
LogInformation(nameof(DidReceiveMessage), remoteMessage.AppData);
}
#endregion