开发者

Registering for Push Notification

I have disabled Push notification from my device settings application (Inside my app icon under settings) and when I call following piece of code none of my delegate call back gets called.

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

application:didRegi开发者_开发问答sterForRemoteNotificationsWithDeviceToken:
application:didFailToRegisterForRemoteNotificationsWithError:

Is there any way to know before registering for Push what all notification types have been switched on? In my application, I am proceeding further once I receive the device token in didRegisterForRemoteNotificationsWithDeviceToken call back. Now, if user do not select any one of them I cannot proceed further so wanted to give an alternate path also.


You can use

UIRemoteNotificationType notificationTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

And then check the returned bit mask for what is and isn't enabled

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     
}

You can read more in the docs here


I realize this is getting fairly old, but there is a much better way to check which bits are set in a bitmask. First of all, if you only want to check if at least one bit is set, check if the whole bitmask is not zero.

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

And if you want to check for specific bits being set, logically AND whichever bit you want to check with the bitmask.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜