6

Is there no simple way of testing if the user has allowed local notifications? I noticed a warning in my console after I denied the sending of local notifications. When the relevant event occurred it said that the app tried to send a notifcation even though the user didn't allow it. I want to check if it's allowed before attempting to display a notification. See the comment in my condition, how do I do that?

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 get prompt the user for permission like so:

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

couldn't I use settings object?

EDIT: I think I have solved it. 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];
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
VeryPoliteNerd
  • 253
  • 2
  • 13

1 Answers1

9

Here's what I use for less specific situations:

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

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

Logan
  • 52,262
  • 20
  • 99
  • 128
  • I tried replacing my /* */-comment with **settings.types != UIUserNotificationTypeNone** . Sadly it didn't work. It still tries to send the notification. Also I find it confusing that something called _types_ contains a number value (enums yes but still). It sounds more like a collection. – VeryPoliteNerd Apr 12 '15 at 14:49
  • I've been using this code in production for a while now and it's always tested reliably. Are you sure there's not something else going on? – Logan Apr 12 '15 at 14:52
  • I only use that one function to send notifications. But I may have mistyped something. It does work now. At least on the simulator. – VeryPoliteNerd Apr 12 '15 at 15:14
  • This is not a good check. It will tell you that notifications are enabled in the case of `UIUserNotificationType.allZeros`. Better to use `settings.types.rawValue & UIUserNotificationType.Sound.rawValue != 0`. more: http://stackoverflow.com/questions/26051950/check-if-local-notifications-are-enabled-in-ios-8/26052687#26052687 – SimplGy Jun 19 '15 at 13:10
  • @SimplGy - `UIUserNotificationType.allZeros` would be equal to `UIUserNotificationType.None` as far as Objective-C is concerned, but, `allZeroes` doesn't exist in ObjC, so there's no issue. In swift, their raw values are also equal (pre 2.0). However, in Swift 2, `.allZeroes` doesn't exist. This answer remains perfectly valid. – Logan Jun 19 '15 at 13:17
  • @Logan this approach will not work for swift < v2.0. I think you must agree, your comment says as much. As a result, for swift users today, including me, it's a no-go. – SimplGy Jun 19 '15 at 13:25
  • @SimplGy - This is an ObjC question, your comment is not helpful. If you have a specific comment for something unrelated to the question, you should say that as opposed to a blanket dismissal of a perfectly valid answer. – Logan Jun 19 '15 at 13:27
  • @SimplGy - Just tested w/ Swift 1.2 and this still works absolutely fine when the value is set to `.allZeroes`. – Logan Jun 19 '15 at 14:04
  • for others like me have same problem checkin against `UIUserNotificationType.None` is not correct instead check with `[UIUserNotificationType.None]` – LHIOUI Jan 25 '16 at 13:36