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:
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.
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.
Can someone explain the behavior of 1,2 and what check should I do?
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,
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.