IOS 远程通知在 IOS 8 的后台不起作用

本文关键字:IOS 后台 不起作用 通知 程通知 | 更新日期: 2024-11-08 23:14:15

我一直在使用Xamarin.IOS,最近我遇到了当Iphone/Ipad在后台时尝试接收远程通知有效负载的问题。IOS 7设备在后台或地面时似乎可以工作。但是,对于IOS 8设备,只有在触摸警报横幅时才会调用DidReceiveRemoteNotification。我需要能够在未触及警报横幅且应用程序处于后台时获取有效负载。为什么远程通知在 IOS 7 的后台工作,而在 IOS 8 上不起作用。我可能做错了什么?

if (UIDevice.CurrentDevice.SystemVersion [0] >= '8')
    {
        UIUserNotificationType types = UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert;
        UIUserNotificationSettings settings = UIUserNotificationSettings.GetSettingsForTypes (types, null);
        UIApplication.SharedApplication.RegisterUserNotificationSettings (settings);
    }
    else
    {
        UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge |                                                      
        UIRemoteNotificationType.Sound;
        UIApplication.SharedApplication.RegisterForRemoteNotificationTypes (notificationTypes);
    } 

IOS 远程通知在 IOS 8 的后台不起作用

如果版本是iOS 8或更高版本,则需要调用RegisterForRemoteNotifications方法,如下所示:

if (UIDevice.CurrentDevice.SystemVersion [0] >= '8')
{
    UIUserNotificationType types = UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert;
    UIUserNotificationSettings settings = UIUserNotificationSettings.GetSettingsForTypes (types, null);
    UIApplication.SharedApplication.RegisterUserNotificationSettings (settings);
    UIApplication.SharedApplication.RegisterForRemoteNotifications();
}