Apple Push Notification in Background Issue
Asked Answered
S

4

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.

Stoicism answered 15/10, 2013 at 20:54 Comment(0)
L
16

Check the following:

  • Notification payload includes "content-available"
{"alert":"",
"badge":"0",
"content-available":"1",
"sound":""}
Lush answered 16/10, 2013 at 12:29 Comment(4)
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.Stoicism
Hah, odd. It worked only one time. Will look into it and get back to youStoicism
Nevermind, got it working! It was just a delay in receiving the data from parse.Stoicism
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."Truncated
P
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.

Playpen answered 15/10, 2013 at 21:0 Comment(5)
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.Stoicism
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?Stoicism
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.Stoicism
Did you set the remote-notification flag in the info.plist?Playpen
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.Stoicism
P
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);

}
Plucky answered 18/6, 2014 at 6:22 Comment(0)
B
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)

Bifoliolate answered 28/10, 2016 at 3:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.