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

被刻印的时光 ゝ 提交于 2019-12-12 04:16:16

问题


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


回答1:


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!




回答2:


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

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



回答3:


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



来源:https://stackoverflow.com/questions/34017393/silent-push-notification-doesnt-work-on-ios7-in-background-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!