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]; } }
source share