无法识别用户是否可以在IO中接收我的通知

时间:2013-10-15 22:37:25

标签: ios push-notification apple-push-notifications

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

我正在使用此代码:

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

如果我这样做:

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

  2. 如果通知中心为OFF并且Alert Style与None不同,我看不到NSLog,但我可以收到通知。

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

1 个答案:

答案 0 :(得分:0)

UIRemoteNotificationType notificationTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

notificationTypes具有整数代码,可帮助我们找到启用的远程通知。为此,我们需要了解UIRemoteNotificationType

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

根据Bitwise left shift计算,以下是值

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

以下代码可帮助我们找到为您的应用启用的远程通知,

    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");
    }

注意:我没有编写所有可能的if-else语句。请根据您的需要添加。

希望这会对你有所帮助。