This question is specific to iOS 10 APNS changes.
This is the flow of my app:
I am only requesting for push notification (step 4.) when the user logs in. So until a user logs out I will not able to re-request for the push.
Is there any neat and clear solution to this so that we can support iOS 10 changes while still supporting iOS 8 or 9?
Turn on notifications for Android devicesTap More on the bottom navigation bar and select Settings. Tap Turn on notifications. Tap Notifications. Tap Show notifications.
Push notifications are messages that pop up on a user's mobile phone or desktop device via their chosen web browser. These little banners slide into view — whether or not your app or website is open.
Go to Settings and tap Notifications. Select an app under Notification Style. Under Alerts, choose the alert style that you want. If you turn on Allow Notifications, choose when you want the notifications delivered—immediately or in the scheduled notification summary.
UIUserNotificationSettings was deprecated back in iOS8. If you want to access the general status of your apps settings, check out UNUserNotifications, the new framework. My understanding is that it treats push and local as one thing. When you register notifications, you can then call to register push. But for the local permissions -- badging and so on, you still need to request user permission. That is, your device can accept push notifications without user permission in order to received data updates, but you can only show notifications via the center with permissions. Here's how to see what permissions have been granted.
Import the framework into your class
@import UserNotifications;
Query the settings
- (void)_queryNotificationsStatus
{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings){
//1. Query the authorization status of the UNNotificationSettings object
switch (settings.authorizationStatus) {
case UNAuthorizationStatusAuthorized:
NSLog(@"Status Authorized");
break;
case UNAuthorizationStatusDenied:
NSLog(@"Status Denied");
break;
case UNAuthorizationStatusNotDetermined:
NSLog(@"Undetermined");
break;
default:
break;
}
//2. To learn the status of specific settings, query them directly
NSLog(@"Checking Badge settings");
if (settings.badgeSetting == UNAuthorizationStatusAuthorized)
NSLog(@"Yeah. We can badge this puppy!");
else
NSLog(@"Not authorized");
}];
}
You can use getNotificationSettingsWithCompletionHandler whenever your app enters in forground.
-(void) IsNotifictaionEnabled :(void (^)(BOOL isActive))handler {
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
if (settings.alertSetting == UNNotificationSettingEnabled) {
handler(YES);
} else {
handler(NO);
}
}];
}
///////////
Following is the original answer, but currentUserNotificationSettings is deprecated now.
you can use currentUserNotificationSettings whenever your app enters in foreground.
UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (grantedSettings.types == UIUserNotificationTypeNone) {
NSLog(@"No permiossion granted");
}
else if (grantedSettings.types & UIUserNotificationTypeSound & UIUserNotificationTypeAlert ){
NSLog(@"Sound and alert permissions ");
}
else if (grantedSettings.types & UIUserNotificationTypeAlert){
NSLog(@"Alert Permission Granted");
}
If you want to check if the status has changed from the previous one, You can keep the previous value of currentUserNotificationSettings
to some variable and compare it with current value overtime in applicationWillEnterForeground
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With