Apple Push Notification in Background Issue

15,248

Check the following:

  • Notification payload includes "content-available"
{"alert":"",
"badge":"0",
"content-available":"1",
"sound":""}
Share:
15,248
ArtSabintsev
Author by

ArtSabintsev

iOS Senior Engineering Manager at Capital One

Updated on June 26, 2022

Comments

  • ArtSabintsev
    ArtSabintsev almost 2 years

    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.

  • ArtSabintsev
    ArtSabintsev over 10 years
    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
    ArtSabintsev over 10 years
    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
    ArtSabintsev over 10 years
    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.
  • RyanR
    RyanR over 10 years
    Did you set the remote-notification flag in the info.plist?
  • ArtSabintsev
    ArtSabintsev over 10 years
    Yes, I did. I also have been using the new application:didReceiveRemoteNotification:fetchCompletionHand‌​ler: method, which supersedes the other delegate method application:didReceiveRemoteNotification:. Still, no dice.
  • ArtSabintsev
    ArtSabintsev over 10 years
    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
    ArtSabintsev over 10 years
    Hah, odd. It worked only one time. Will look into it and get back to you
  • ArtSabintsev
    ArtSabintsev over 10 years
    Nevermind, got it working! It was just a delay in receiving the data from parse.
  • Simon
    Simon almost 9 years
    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."