简体   繁体   中英

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).

You didn't mention about enabling Background Fetch. Have you enabled it in your App Capabilities?

and set this in your AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];

    return YES;
}

and this as your delegate method

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    // do your thing here. maybe calling a view controller class or stuff
}

But if you're not into background fetch, try this instead:

{
    aps = {
        "content-available" : 1,
        "sound" : ""
    };
}

Good luck!

Im not exactly sure about the issue, but maybe you can try to change respondsToSelector. Refer to the following:

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM