注册推送通知

时间:2011-06-29 21:32:17

标签: iphone objective-c ios cocoa-touch push-notification

我已禁用设备设置应用程序中的推送通知(设置中的我的应用程序图标内),当我调用以下代码时,我的代理回调都没有被调用。

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

application:didRegisterForRemoteNotificationsWithDeviceToken:
application:didFailToRegisterForRemoteNotificationsWithError:

在注册Push之前有什么方法可以知道所有通知类型是否已经打开?在我的应用程序中,一旦我在didRegisterForRemoteNotificationsWithDeviceToken回调中收到设备令牌,我就会继续前进。现在,如果用户没有选择其中任何一个,我就无法继续前进,所以也希望给出一个替代路径。

2 个答案:

答案 0 :(得分: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

中阅读更多内容

答案 1 :(得分: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
}