0

我想以编程方式找出用户是否启用了推送通知。

我正在使用这段代码:

 UIRemoteNotificationType status = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (status == UIRemoteNotificationTypeNone)
    {
        NSLog(@"User doesn't want to receive push-notifications");
    } 

如果我这样做:

  1. 当应用程序请求许可时,我按确定,然后从设置中将通知中心设置为关闭,将警报样式设置为无,我无法收到通知,但我没有看到 NSLog。

  2. 如果通知中心处于关闭状态并且警报样式不同于无,我看不到 NSLog,但我可以接收通知。

有人可以解释 1,2 的行为,我应该做什么检查?

4

1 回答 1

0
UIRemoteNotificationType notificationTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

notificationTypes has the integer code which will help us to find what are the remote notification enabled. For that, we need to understand the UIRemoteNotificationType,

typedef enum {
   UIRemoteNotificationTypeNone    = 0,
   UIRemoteNotificationTypeBadge   = 1 << 0,
   UIRemoteNotificationTypeSound   = 1 << 1,
   UIRemoteNotificationTypeAlert   = 1 << 2,
   UIRemoteNotificationTypeNewsstandContentAvailability = 1 << 3
} UIRemoteNotificationType; 

According to the Bitwise left shift calculation, following are the values,

  • UIRemoteNotificationTypeNone = 0
  • UIRemoteNotificationTypeBadge = 1
  • UIRemoteNotificationTypeSound = 2
  • UIRemoteNotificationTypeAlert = 4
  • UIRemoteNotificationTypeNewsstandContentAvailability = 8

Following code will help us to find what are the remote notification enabled for your app,

    if (notificationTypes == UIRemoteNotificationTypeNone) {
        // Do what ever you need to here when notifications are disabled
        NSLog(@"User doesn't want to receive push-notifications");
    } else if (notificationTypes == UIRemoteNotificationTypeBadge) {
        // Badge only
        NSLog(@"Badges Only");
    } else if (notificationTypes == UIRemoteNotificationTypeAlert) {
        // Alert only
        NSLog(@"Alerts only");
    } else if (notificationTypes == UIRemoteNotificationTypeSound) {
        // Sound only
        NSLog(@"Sound Only");
    }
    else if (notificationTypes == UIRemoteNotificationTypeNewsstandContentAvailability) {
        // NewsstandContentAvailability only
        NSLog(@"NewsstandContentAvailability Only");
    }else if (notificationTypes == 5)//Badge(1)+Alert(4)=5 {
        // Badge & Alert
        NSLog(@"Badges and Alert");
    } else if (notificationTypes == 3)//Badge(1)+Sound(2)=3 {
        // Badge & Sound
        NSLog(@"Badges and Sound");
    } else if (notificationTypes == 6)//Alert(4)+Sound(2)=6 {
        // Alert & Sound
        NSLog(@"Alert and Sound");
    } else if (notificationTypes == 7)//Badge(1)+Alert(4)+Sound(2)=7 {
        // Badge, Alert & Sound
        NSLog(@"Badge, Alert & Sound");
    }
    else if (notificationTypes == 10)//Sound(2)+NewsstandContentAvailability(8)=10 {
        // Sound & NewsstandContentAvailability
        NSLog(@"Sound & NewsstandContentAvailability");
    }

Note: I haven't write all possible if-else statements. Please add according to your need.

Hope this will help you.

于 2013-10-16T07:06:38.470 回答