始终启用iOS 8+远程通知功能

时间:2015-09-07 15:39:15

标签: ios

对于iOS> = 8,仅

在我的AppDelegate中,我按如下方式注册用户通知:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"didFinishLaunchingWithOptions called");

    // iOS >= 8.0

    // register to receive user notifications
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    ...
}

完成后,我按如下方式注册远程通知:

- (void)application:(UIApplication *)application  didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    NSLog(@"didRegisterUserNotificationSettings called");

    //register to receive remote notifications
    [application registerForRemoteNotifications];
}

完成后,我会检查该应用是否已注册

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken called");

    BOOL pushNotificationOnOrOff = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]; // always returns TRUE
}

但是,即使用户已明确设置应用的远程通知功能(通过首次安装应用后显示的通知权限提醒,或通过应用设置),应用也始终指示已启用推送通知。

我已将应用的背景模式/远程通知设置为TRUE。我一直在使用开发证书编译的实际设备(通过USB电缆连接)进行调试。

帮助,我几个小时都在争吵。

1 个答案:

答案 0 :(得分:5)

这似乎是一个错误,我也在iPhone 6,iOS 8.1.2上发现了相同的行为。

即使用户拒绝推送通知权限(通过提醒视图),也可以通过[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]手动停用通知,

TRUE始终返回Settings.app > Notifications

经过一些研究后,我发现如果您为该应用启用了Background App Refresh,则[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]将始终返回TRUE

Background App Refresh设置为FALSE时,[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]会返回正确的值。

作为一种变通方法,您可以评估[[UIApplication sharedApplication] currentUserNotificationSettings].types以确定是否允许推送通知。

typedef NS_OPTIONS(NSUInteger, UIUserNotificationType) {
    UIUserNotificationTypeNone    = 0,      // the application may not present any UI upon a notification being received
    UIUserNotificationTypeBadge   = 1 << 0, // the application may badge its icon upon a notification being received
    UIUserNotificationTypeSound   = 1 << 1, // the application may play a sound upon a notification being received
    UIUserNotificationTypeAlert   = 1 << 2, // the application may display an alert upon a notification being received
} NS_ENUM_AVAILABLE_IOS(8_0);

希望这有帮助。