Apple Push Notification in Background Issue

限于喜欢 提交于 2019-11-27 21:36:39

Check the following:

  • Notification payload includes "content-available"
{"alert":"",
"badge":"0",
"content-available":"1",
"sound":""}

The documentation for UIApplicationDelegate -application:didReceiveRemoteNotification is pretty clear on this method:

If the app is not running when a push notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that push notification. Instead, your implementation of the application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method needs to get the push notification payload data and respond appropriately.

You need to handle notifications in both methods to be able to respond when your app is in the background and foreground.

you can try to use this instead of

application:didReceiveRemoteNotification

method, since you need to fetch your push in background mode, thus, this would works when the app is in background mode. However, you might need to add in custom notification or UIAlertView when app is in foreground to display your message. Hope it helps and it's not too late.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{

    NSLog(@"Remote Notification Received: %@", userInfo);

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody = @"message to be displayed";
    notification.applicationIconBadgeNumber = 1;


    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
    completionHandler(UIBackgroundFetchResultNewData);

}

Alex L's answer almost worked for me.

Just make sure the value of content-available is 1 (number), not a string "1":

{
  "alert":"",
  "content-available":1,
  "sound":""
}

badge parameter is optional.

(Using Parse Server for APNs)

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