iOS:用于判断是否启用推送通知的编程方式

时间:2014-08-05 15:04:49

标签: ios push-notification

我为APNS启用了appID的iPhone开发应用程序。在我的applicationDidBecomeActive事件中,我通过执行以下操作来检查是否启用了推送通知:

- (void)applicationDidBecomeActive:(UIApplication *)application {

    NSLog(@"applicationDidBecomeActive");

    //Check if remote notifications are enabled for this app, if not proceed with performSetupAndWebService calls
    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

    if(types == UIRemoteNotificationTypeNone)
    {
        //User has push notifications DISABLED for this application.
        NSLog(@"PUSH NOTIFICATIONS DISABLED");

        [self performSetupAndWebServiceCalls];
    }
    else
    {
        //User has push notifications ENABLED for this application.
        NSLog(@"PUSH NOTIFICATIONS ENABLED");

        NSLog(@"Registering for push notifications...");
        [[UIApplication sharedApplication]
         registerForRemoteNotificationTypes:(UIRemoteNotificationType)(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert)];
    }
}

似乎无论我做什么,我的代码总是告诉我禁用推送通知。我的应用图标位于Notification Center中的Settings,并且已包含在内。我尝试关闭特定类型的通知并将其重新打开,但types == UIRemoteNotificationTypeNone始终为真。

我错过了一些明显的东西吗?不好的是,这似乎也影响了我们的生产应用程序的用户,但不是所有这些。非常不一致且非常令人沮丧。

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

当通知被禁用并且您尝试注册它们时,您将获得以下UIApplicationDelegate方法:

application:didFailToRegisterForRemoteNotificationsWithError:

答案 1 :(得分:0)

- (BOOL) getpushNotificationstatus
 {
 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
 {
 return ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]); 
} 
else 
{
 UIRemoteNotificationType types = [[UIApplicationy sharedApplication] enabledRemoteNotificationTypes]; return (types & UIRemoteNotificationTypeAlert); 
}
}

它也可以在iOS8中使用