简体   繁体   English

无法确定用户是否可以在IO中收到我的通知

[英]cannot identify if user can receive my notification or not in IOs

I want to find out programmatically if user has enabled push notifications or not. 我想以编程方式找出用户是否启用了推送通知。

I am using this code: 我正在使用此代码:

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

If I do this: 如果我这样做:

  1. when the app asks for permission, I press ok, and then from the settings set the Notification center to OFF and the Alert Style to None, I cannot receive the notification but I do not see the NSLog. 当应用程序请求许可时,我按OK,然后从设置中将“通知中心”设置为“关闭”,并将“警报样式”设置为“无”,我无法收到通知,但看不到NSLog。

  2. if notification center is left to OFF and Alert Style is different than None, I do not see the NSLog, but I can receive notifications. 如果通知中心保持关闭状态,并且“警报样式”与“无”不同,则看不到NSLog,但是我可以接收通知。

Can someone explain the behavior of 1,2 and what check should I do? 有人可以解释1,2的行为,我应该做什么检查?

UIRemoteNotificationType notificationTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

notificationTypes has the integer code which will help us to find what are the remote notification enabled. notificationTypes具有整数代码,它将帮助我们查找启用了什么远程通知。 For that, we need to understand the UIRemoteNotificationType , 为此,我们需要了解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 UIRemoteNotificationTypeNone = 0
  • UIRemoteNotificationTypeBadge = 1 UIRemoteNotificationTypeBadge = 1
  • UIRemoteNotificationTypeSound = 2 UIRemoteNotificationTypeSound = 2
  • UIRemoteNotificationTypeAlert = 4 UIRemoteNotificationTypeAlert = 4
  • UIRemoteNotificationTypeNewsstandContentAvailability = 8 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. 注意:我还没有编写所有可能的if-else语句。 Please add according to your need. 请根据您的需要添加。

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM