Search code examples
iosiphoneuilocalnotification

UILocalNotification fired but not showing, or showing but not visible in notification center, or firing like a charm


I've been struggling for the past few days on the local notifications on my app.

Basically the goal is to pop a notification when the user approches an address.

here is the code:

NSMutableArray *notifications = [@[] mutableCopy];
for (CCAddress *address in results) {
    CCCategory *category = [address.categories.allObjects firstObject];
    NSDictionary *userInfo = @{@"addressId" : address.identifier};
    UILocalNotification *localNotification = [UILocalNotification new];

    if (category == nil)
        localNotification.alertBody = [NSString stringWithFormat:@"Vous êtes proche de %@", address.name];
    else
        localNotification.alertBody = [NSString stringWithFormat:@"Vous êtes proche de %@, %@", address.name, category.name];
    localNotification.alertAction = @"Linotte";
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.userInfo = userInfo;

    [notifications addObject:localNotification];

    address.lastnotif = [NSDate date];
}
[managedObjectContext saveToPersistentStore:NULL];

[UIApplication sharedApplication].scheduledLocalNotifications = notifications;

The result is actually totally random, but there is something I know for sure: the geofencing works well, as you can see I set the date of the notification in lastNotif, so I know when they are fired.

Sometimes I see the notification pop, but doesn't stay in the notification center, but most times nothing happens, even if I see by the date that It actually fired, and sometimes everything goes fine.

I tried many things, like using presentLocalNotificationNow, setting a fireDate with a 1 second delay between each, and other things I don't even remember...

So, obviously there is something I missed in the documentation, but what ?

thanks.

PS: the app is in background or off when it happens, I'm aware of didReceiveLocalNotification.

PS2: I actually don't know if those that I don't see at all actually fired, because they don't show up in the notification center, so maybe they fired but I have absolutely no way to see them if I don't have my phone's screen in sight when they do.

EDIT: So, I've been doing some tests around my house, phone closed, screen locked. The real syndrom is that when a notification pops, it only turns the screen on, and the phone vibrates (I was sound off), then nothing...


Solution

  • Ok, so as intended, it was kind of stupid.

    Before the code I posted I had:

    if ([results count] == 0) {
        [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
        return;
    }
    

    But, when you set applicationIconBadgeNumber to 0, it removes all notifications from notification center !

    The documentation says nothing about this (https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html).