开发者

Reading Notification flag in "Settings" application inside my iPhone application

I am enabling push notification for my application. How can we read the flags for notification in "Settings" app when my app is Running. For some reasons, I need to know whether a particular notification (alert, sound, badge) is 开发者_如何转开发set to ON/OFF.

Please guide.


Try evoking this method [[UIApplication sharedApplication] enabledRemoteNotificationTypes]

It will return a UIRemoteNotificationType which you can work with to determine what is available.

UIRemoteNotificationType status = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

Now, status can be looked at as an int using NSLog(@"status = ", status);, to which we can determine exactly what is on. But to do this we need to understand UIRemoteNotificationType.

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

Without going into much detail, what you basically need to walk away from this knowing is that ...

  • If badges are on, add 1
  • If sound is on, add 2
  • If alerts are on, add 4
  • If Newsstand Content is available, add 8 (I'm not going to worry about this guy)

Let's say you want to know if badges/sound/alerts are all on. The UIRemoteNotificationType (status if you are playing along) should come out to be 7.

Now, lets work backwards. Lets say that status == 5. There is only one configuration of settings that can give us this value, and that is if badges and alerts are on (badges add 1, alerts add 4, total is 5) and sound is off.

What if status == 6? Again, there is only one configuration of settings that will return this number, and that is if alerts and sound are both on, while badges are off.

Using IF statements, we can do something like

If (status == 5)
{
    NSLog(@"User has sound alerts disabled");
    [self fireThatSpecialMethod];
}

To run a set block of code, or fire a particular method for when sound is disabled, but everything else is on. Anyways, hope this response is helpful to people!


Note that as of iOS 8 the method you are looking for to determine if remote notifications are registered is this:

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]

You may determine what kinds of notifications the user currently has enabled using the following method

[[UIApplication sharedApplication] currentUserNotificationSettings]

This returns a UIUserNotificationSettings object with all of the info you need.

Documentation Links:

isRegisteredForRemoteNotifications

currentUserNotificationSettings

UIUserNotificationSettings

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜