ArtSabintsev
ArtSabintsev

Reputation: 5200

Apple Push Notification in Background Issue

I'm using parse.com as my APNs provider for a test app I'm building. I've confirmed that push notifications are working as I have been able to successfully receive alerts when the app is in the foreground. Also, I have the remote-notification value for the UIBackgroundModes key specified in my plist.

In my app, I'm wanting to send a user's current location data back to my app-specific parse.com database when a push notification is received. I don't actually care about the notification payload itself, as the notification is just a means to getting a small piece of info. The app is constantly collecting data in the background and storing it in a persistent NSDictionary.

I've put the location sending code in the application:didReceiveRemoteNotification: method. If my app is in the foreground when I receive a notification, the method gets called. If my app is in the background, the method isn't called.

EDIT: The problem persists even when I use application:didReceiveRemoteNotification:fetchCompletionHandler:.

Note: My NSDictionary full of location data isn't empty. Also, I am not attempting to do any UI manipulation in the background - just trying to perform an API request.

Upvotes: 8

Views: 16105

Answers (4)

Sergey Filippov
Sergey Filippov

Reputation: 29

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)

Upvotes: 0

munfai
munfai

Reputation: 83

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

}

Upvotes: 0

Alex L
Alex L

Reputation: 8449

Check the following:

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

Upvotes: 16

RyanR
RyanR

Reputation: 7758

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.

Upvotes: 0

Related Questions