Search code examples
iosios7notificationspush-notification

Silent push notification doesn't work on iOS7 in background mode


I have this weird situation, my app support iOS7 and above. what it does, it's enabled Remote notifications by using silent notifications.

I know that iOS7 and iOS8 above has different logic to handle notification. I did this :

if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
        {
            [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

            [[UIApplication sharedApplication] registerForRemoteNotifications];
        }
        else {
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
             (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
        }

here's notification receives

    {
        aps =     {
            "content-available" = 1;
        };

    }

So what it does is, app receive silent notification, and then set localNotification, see below :

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    notification.soundName = UILocalNotificationDefaultSoundName;

    notification.alertBody = @"testing";
    notification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication]  scheduleLocalNotification:notification];

All it works in iOS8 and iOS9 in background mode and foreground mode. When app is in foreground, it will trigger didReceiveLocalNotification.

But when I was testing in iOS7, it doesn't work if the app is in background mode. I'm trying to figure out how this happen, while other OS working fine. I did test while app is open, it did receive push notification, and didReceiveLocalNotification get triggered. but when goes to background, nothing happen (no local push notification).


Solution

  • As pointed in this tread, aps need to include priority in order to work in iOS7.

     aps =     {
            badge = 7;
            "content-available" = 1;
            priority = 5;
        }; 
    

    check this : https://stackoverflow.com/a/23917593/554740