2011-06-29 143 views
5

我已禁用从我的设备设置应用程序(在我的应用程序图标下的设置下)推送通知,当我打电话下面的一段代码时,我的委托回调没有被调用。注册推送通知

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound]; 

application:didRegisterForRemoteNotificationsWithDeviceToken: 
application:didFailToRegisterForRemoteNotificationsWithError: 

有没有办法知道所有通知类型已切换上什么推送注册过吗?在我的应用程序中,一旦我收到didRegisterForRemoteNotificationsWithDeviceToken回调中的设备令牌,我就会继续前进。现在,如果用户没有选择其中的任何一个,我不能进一步进行,因此想要给出替代路径。

回答

10

您可以使用

UIRemoteNotificationType notificationTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; 

,然后检查是什么,没有启用返回位掩码

if (notificationTypes == UIRemoteNotificationTypeNone) { 
    // Do what ever you need to here when notifications are disabled 
} else if (notificationTypes == UIRemoteNotificationTypeBadge) { 
    // Badge only 
} else if (notificationTypes == UIRemoteNotificationTypeAlert) { 
    // Alert only 
} else if (notificationTypes == UIRemoteNotificationTypeSound) { 
    // Sound only 
} else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)) { 
    // Badge & Alert 
} else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)) { 
    // Badge & Sound   
} else if (notificationTypes == (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)) { 
    // Alert & Sound 
} else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)) { 
    // Badge, Alert & Sound  
} 

你可以阅读更多的the docs here

+0

谢谢。这可以工作,但不会单独读取各个标志。如果只有一个标志启用(声音/警报/徽章),我们可以读取它,但如果启用了多个标志,我们就没有价值。任何线索如何处理这一点。我想读不同的所有这些标志。 – Abhinav

+0

由于返回的值是一个位掩码,因此您需要手动测试每种可能的组合。我已经更新了上面的答案以证明这一点。 –

+1

bdmontz答案是比较位掩模时的正确方法。上面的例子不必要的复杂。 – Emil

9

我意识到这是变得相当老,但有一个更好的方法来检查位掩码中设置了哪些位。首先,如果您只想检查是否至少设置了一个位,请检查整个位掩码是否不为零。

if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes] != 0) { 
    //at least one bit is set 
} 

如果你想检查被设置的具体位,逻辑和无论你想检查位掩码的位。

UIRemoteNotificationType enabledTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; 
if (enabledTypes & UIRemoteNotificationTypeBadge) { 
    //UIRemoteNotificationTypeBadge is set in the bitmask 
} 
+0

它在iOS 7上总是给我UIRemoteNotificationTypeNone。任何理由? –