在xamarin IOS中处理推送通知时,使用DidReceiveRemoteNotification处理程序,它可以在前台和后台随机工作。
有时我会收到通知,有时却不会。
我们正在使用Xamarin.IOS 13.4。
这是我们的AppDeletage.cs代码:
[Register("AppDelegate")]
public class AppDelegate : FormsApplicationDelegate,IUNUserNotificationCenterDelegate,IMessagingDelegate
{
public override bool Finishedlaunching(UIApplication app,NSDictionary options)
{
RegistrarNotificacionesRemotas();
LoadApplication(new App());
App.ConfigNotificationEvents();
return base.Finishedlaunching(app,options);
}
public override void RegisteredForRemoteNotifications(UIApplication application,NSData devicetoken)
{
App.SendRegistrationToServer(devicetoken.ToString(),(int)Plataforma.iOS);
}
public override void ReceivedRemoteNotification(UIApplication application,NSDictionary userInfo)
{
}
public override void DidReceiveRemoteNotification(UIApplication application,NSDictionary userInfo,action<UIBackgroundFetchResult> completionHandler)
{
}
private void RegistrarNotificacionesRemotas()
{
Firebase.Core.App.Configure();
// Register your app for remote notifications.
if (UIDevice.CurrentDevice.CheckSystemVersion(10,0))
{
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.Current.Delegate = this;
var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
UNUserNotificationCenter.Current.RequestAuthorization(authOptions,(granted,error) => {
Console.WriteLine(granted);
});
}
else
{
// iOS 9 or before
var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes,null);
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
}
UIApplication.SharedApplication.RegisterForRemoteNotifications();
Messaging.SharedInstance.Delegate = this;
Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
}
}
}
在前台,一切都很好,但是在后台永远不会触发方法ReceivedRemoteNotification或DidReceiveRemoteNotification