5

According to Apple's documentation I can register for silent notification by adding "content-available" = 1 key-value in aps payload dictionary. I want my app to wake up in background when a silent notification arrives. I set App downloads content in response to push notifications value to Required background modes in my info.plist

This is my payload dictionary

{"aps":
      {
       "alert":"Notification alert","badge":1,"sound":"default","content-available":1
      }
}

I am getting callbacks in -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler while my app is in background. But my question is can we get callback to this or any other method when our application is in killed state?

I don't want my app user to see the notification but I want my app to perform certain task by waking it up in background through silent notifications.

Any suggestions would be appreciated.

Vijay Sanghavi
  • 340
  • 1
  • 5
  • 23

2 Answers2

6

When the device receives a push message with content-available set, your app gets launched in the background by Apple. Users won't be aware of it. From the docs:

content-available: Provide this key with a value of 1 to indicate that new content is available. Including this key and value means that when your app is launched in the background or resumed, -application:didReceiveRemoteNotification:fetchCompletionHandler: is called.

Also from docs

didReceiveRemoteNotification: However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.

mfaani
  • 33,269
  • 19
  • 164
  • 293
TwoStraws
  • 12,862
  • 3
  • 57
  • 71
  • You are right but the problem is if my application is in killed state and a silent notification arrives than my app is not launching in background. – Vijay Sanghavi Dec 15 '15 at 14:04
  • Are you quite sure? This can be hard to debug. You might find it easiest (if your Xcode debug-fu isn't 100%) to write a log to a file when your app is launched so that you can see the app is activating even if you aren't attached to it with the Xcode debugger. The technology definitely works, but [there are some occasional quirks](http://stackoverflow.com/questions/19239737/silent-push-notification-in-ios-7-does-not-work). – TwoStraws Dec 15 '15 at 14:12
  • 1
    If you app has been manually terminated by the user, this is often taken as a hint by the OS that the user *really* doesn't want it running. Certainly several other "backgroundy" processes in iOS (e.g. background file transfer with NSURLSession) specifically *don't* start up your app like they normally would if it's been killed from the app switcher. I don't know about this particular case, but it wouldn't surprise me to find that a manually-terminated app wouldn't wake on push notifications until it had been manually re-started by the user. – Matt Gibson Dec 15 '15 at 14:13
  • 1
    @MattGibson I am just doing as you stated. I am terminating my app from the app switcher, what I actually want is to maintain a socket connection in background even if user kill the application and that is the reason I am trying to use silent notifications in my. Its like if set silent notification for a interval of time than without knowing the user I can wake my app in background. Is this feasible? or I am going in wrong direction? or any other solution for my problem? – Vijay Sanghavi Dec 16 '15 at 06:45
  • @TwoStraws thanks for the suggestions I'll surely try to debug that with xcode debugger and hope it'll work – Vijay Sanghavi Dec 16 '15 at 06:59
  • 4
    By the look of [this existing answer](http://stackoverflow.com/a/22938319/300836), I'm right to think that if an app has been killed from the app switcher, it won't be started when receiving a notification. This seems like sensible, user-friendly behaviour to me: if I explicitly kill your app, I want it to stay dead. – Matt Gibson Dec 16 '15 at 09:49
5

I have faced a similar kind of scenario, I wrote this code to debug if my application is waking up on silent notification or not.

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *str = [defaults objectForKey:@"key"];
    if (str  == nil) {
        str = @"didReceiveRemoteNotification";
    }else{
        str = [NSString stringWithFormat:@"%@, didReceiveRemoteNotification",str];
    }
    [defaults setObject:str forKey:@"key"];
    [defaults synchronize];
}

This code works like, if you app wakes up than you'll get callback in this method and the method name will be written in NSUserDefaults. So when you debug your app manually you can see the value of str string variable, if there is string didReceiveRemoteNotification than you'll get to know that you application has woke up.

Note: This works only in background mode for me. I get value when I did not force close(terminate manually) my app but when I force close my app from app switcher I'll not get any value.

I hope that works.