8

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.

  • Am I doing something wrong?
  • Is it even possible to execute an API request in application:didReceiveRemoteNotification: when the app is in the background and the user hasn't interacted with the notification?**

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.

Kara
  • 6,115
  • 16
  • 50
  • 57
ArtSabintsev
  • 5,170
  • 10
  • 41
  • 71

4 Answers4

16

Check the following:

  • Notification payload includes "content-available"
{"alert":"",
"badge":"0",
"content-available":"1",
"sound":""}
Alex L
  • 8,419
  • 6
  • 43
  • 51
  • That worked! I had the `content-available` flag turned on in the AppDelegate, but wasn't pushing that flag in my JSON APNs. Thanks, Alex! That fixes the problem. – ArtSabintsev Oct 16 '13 at 14:02
  • Hah, odd. It worked only one time. Will look into it and get back to you – ArtSabintsev Oct 16 '13 at 14:06
  • Nevermind, got it working! It was just a delay in receiving the data from parse. – ArtSabintsev Oct 16 '13 at 14:10
  • According to the iOS developer library "content-available" is for silent push notifications: "The aps dictionary can also contain the content-available property. The content-available property with a value of 1 lets the remote notification act as a “silent” notification. When a silent notification arrives, iOS wakes up your app in the background so that you can get new data from your server or do background information processing. Users aren’t told about the new or changed information that results from a silent notification, but they can find out about it the next time they open your app." – Simon Jul 28 '15 at 07:03
0

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.

RyanR
  • 7,728
  • 1
  • 25
  • 39
  • I guess I understood `not running` as totally shut down, not `inactive` or `background` mode. I'll try it out momentarily and mark your answer correct if I get it working. – ArtSabintsev Oct 15 '13 at 21:04
  • It's still not working. I am now attempting to send the location data from both `application:didFinishLaunchingWithOptions:` and `application:didReceiveRemoteNotification:`. If the application is in the background (e.g., I pushed the home button), no data is sent, UNLESS I interact with the notification. If I interact with the notification by clicking on it, the `application:didReceiveRemoteNotification:` method is entered. What am I doing wrong? Do I fundamentally misunderstand how background notifications should work? – ArtSabintsev Oct 15 '13 at 21:28
  • I think my app is actually in the `suspended` state, and not the `background` state. I think that's what may be causing the issue. – ArtSabintsev Oct 15 '13 at 21:41
  • Did you set the remote-notification flag in the info.plist? – RyanR Oct 16 '13 at 00:27
  • Yes, I did. I also have been using the new `application:didReceiveRemoteNotification:fetchCompletionHandler:` method, which supersedes the other delegate method `application:didReceiveRemoteNotification:`. Still, no dice. – ArtSabintsev Oct 16 '13 at 01:54
0

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

}
munfai
  • 83
  • 9
0

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)