2

I am using iOS7 and I am trying to determine if I can get the JSON payload in the following situation.

  • I have background mode "remote-notifications" enabled
  • The push notification is received while the app is terminated
  • The app is launched manually from the icon not from the notification center

When I launch the app from the icon itself after the notification has been received I do not get the push in the launch options from

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

and the following method does not get called either when app is manually launched from the icon

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

  • See my answer. Most likely you either don't have 1.) background remote notificaitons enabled in your info.plist file OR 2.) the push notification that you are sending to the application doesn't contain 'content-available': 1. – Sandy D. Feb 23 '15 at 19:20

1 Answers1

1

I finally figured out how you can get this!

As of iOS 7 you can get it!

Basically, you need to configure your application for background remote notifications.

So, in your info.plist file: For required backgrounds - set it to app downloads content from push notifications.

In the AppDelegate.m file, you need to implement this method:

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

See this for how to implement that: didReceiveRemoteNotification: fetchCompletionHandler: open from icon vs push notification

For your push notifications, you must have 'content-available': 1, as part of the push notification. This is what tells the application that there is new content before displaying the alert.

See this page for more information on background remote notifications: http://developer.xamarin.com/guides/cross-platform/application_fundamentals/backgrounding/part_3_ios_backgrounding_techniques/updating_an_application_in_the_background/

Community
  • 1
  • 1
Sandy D.
  • 3,166
  • 1
  • 20
  • 31
  • Don't think this solution answers my question. I don't think its even possible. Does this method allow you to receive the payload after the app is terminated and the app is opened from the app icon? – Walter Martin Vargas-Pena Mar 26 '15 at 01:08
  • If you mean that you killed the app, then no -- you will not be able to get the data. The only way you can get the data for that scenario is by querying your server for the data (if you saved it somewhere). This solution handles if your app is open and running in the background -- they get an alert-- a user chooses the app icon instead of the banner alert. Then, it will get the data and update any view controllers before the user sees it. – Sandy D. Mar 26 '15 at 13:45
  • So there is really no way of telling that there was a notification while the app wasn't running? – Frank Rupprecht Sep 01 '15 at 15:50
  • @FrankSchlegel If a user kills the application by going to the task manager, then no you won't be able to know. The user has to re-open the application in order for background notifications to work. However, if your application requires the information -- then you should store the push alert in a database or somewhere on your server -- and then have the application request the information once the user re-opens the app. Now if the application is just running in the background (meaning the user didn't close out the app in the task manager), then yes you can get the notification. – Sandy D. Sep 02 '15 at 03:28