Check if the user is allowed local notifications or not. iOS 8. Obj-C

Is there a simple way to test if the user has allowed local notifications? I noticed a warning in the console after I refused to send local notifications. When the corresponding event occurred, he said that the application tried to send a notification, even if the user did not allow it. I want to check if this is allowed before trying to display a notification. See Comment in my state, how can I do this?

My code is:

app = [UIApplication sharedApplication]; -(void)showBackgroundNotification:(NSString *) message { //check if app is in background and check if local notifications are allowed. if (app.applicationState == UIApplicationStateBackground /*&& LocalNotificationsAreAllowed*/){ UILocalNotification *note = [[UILocalNotification alloc]init]; note.alertBody = message; note.fireDate = [NSDate dateWithTimeIntervalSinceNow:0.0]; [app scheduleLocalNotification :note]; } } 

I ask the user for this permission:

 UIUserNotificationSettings *settings; if ([app respondsToSelector:@selector(registerUserNotificationSettings:)]) { settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotification TypeSound) categories:nil]; [app registerUserNotificationSettings:settings]; } 

Can't I use the settings object?

EDIT: I think I decided. This seems to work.

 -(void)showBackgroundNotification:(NSString *) message { if (app.applicationState == UIApplicationStateBackground && [app currentUserNotificationSettings].types != UIUserNotificationTypeNone){ UILocalNotification *note = [[UILocalNotification alloc]init]; note.alertBody = message; note.fireDate = [NSDate dateWithTimeIntervalSinceNow:0.0]; [app scheduleLocalNotification :note]; } } 
+6
source share
1 answer

Here is what I use for less specific situations:

 + (BOOL)notificationsEnabled { UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings]; return settings.types != UIUserNotificationTypeNone; } 

I usually store a set of these types of methods in the notification manager.

+9
source

Source: https://habr.com/ru/post/985014/


All Articles