Can Apple's silent push notifications launch my app in the background?
Asked Answered
R

2

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.

Repp answered 15/12, 2015 at 13:53 Comment(0)
P
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.

Prado answered 15/12, 2015 at 13:57 Comment(6)
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.Repp
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.Prado
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.Krugersdorp
@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?Repp
@Prado thanks for the suggestions I'll surely try to debug that with xcode debugger and hope it'll workRepp
By the look of this existing answer, 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.Krugersdorp
S
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.

Sloshy answered 18/12, 2015 at 8:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.