Get Apple's remote push notifications even after app terminates - ios

I am developing an iOS app which receives apple's remote push notifications at a particular time interval. The notifications are received successfully when app is active as well as when app is in background. But when app is terminated from app switcher/dock, notifications are not received until the app is started again.
I have tried following code to keep app alive in background. This same code is implemented in applicationWillTerminate: method, but it did not work.
__block UIBackgroundTaskIdentifier bgTask ;
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
[app enabledRemoteNotificationTypes];
bgTask = UIBackgroundTaskInvalid;
}];
What is the perfect way of receiving notifications even after app is terminated?

If you actively stop the app in the app switcher it stops receiving any push notifications.
This is an intended behaviour as stated by Apple engineers in the developer forums.

Related

iOS background fetch before termination

I have a situation where I must call a web service before app termination but not when enters the background, because this should be done only once which when the app get terminated.
I tried the background execution thing but I believe it won't work once the app get terminated but only if the app in the background.
Here is my latest try inside applicationWillTerminate:
__block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithName:#"MyTask" expirationHandler:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
// Call web service here
[application endBackgroundTask:bgTask];
bgTask =UIBackgroundTaskInvalid;
});
You can't do that, and honestly, why would you?
This is an ill-posed problem anyway. What should happen if the battery dies? What if the app crashes? What if the app goes to background and then the OS terminates it when memory pressure increases? What if you lose internet access and the app is terminated?
The closest thing you can do is to implement some sort of heartbeat service which your app regularly pings. If the pinging stops, your app has either terminated or network connectivity was lost.

App terminate how to run a service in ios

If it possible to run a service after app was terminated. Please, help me verify following code:
UIApplication* app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
Your app will generally never see willTerminate, because the system generally only terminates your app once it's already suspended (in the background). Once your app is suspended, it gets no further chance to act(*), so there's no callback for that.
The didEnterBackground delegate message or notification should be considered your last chance to clean things up or save state before possible termination.
(*) Okay, your app can do stuff if it's in one of the supported background execution modes, like audio, VoIP, or navigation, but in that case it either hasn't been suspended yet or it's been un-suspended with an entry point specific to that background mode.

Voip Pushkit notification will not re-launch the app if it was force-quitted and device was rebooted

I cannot get voip pushkit notifications to re-launch the app if the user has force-quitted the app (by swiping it up in the multi-tasking interface) AND if the device was rebooted.
However, I can get the voip pushkit notifications to work in the following scenarios:
The app was force-quitted then the pushkit notification arrives. The app will be relaunched immediately. Standard push notifications are not capable of waking the app in such scenario.
The app was in the background / suspended and the device is rebooted. Thanks to Voip mode, the app will be relaunched on device reboot (I can see the process in Xcode Activity Monitor). There is a trick needed here to get the pushkit notification to be properly processed which is described in http://blog.biokoda.com/post/114315188985/ios-and-pushkit in these terms "Before initializing PushKit start a background task. Finish this task when PushKit token is received"
Somehow when combining these two (device reboot AND app force-quit) then pushkit notifications doesn't seem to relaunch the app. Also when looking at the device logs in Xcode I get no logs from apsd saying the notification was processed by the system.
Here is my code:
#implementation AppDelegate
{
UIBackgroundTaskIdentifier bgTask;
}
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (true) {
;
}
});
// Initialize pushkit
PKPushRegistry *pushRegistry =
[[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
pushRegistry.delegate = self;
pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
return YES;
}
- (void)pushRegistry:(PKPushRegistry *)registry
didUpdatePushCredentials:(PKPushCredentials *)credentials
forType:(NSString *)type{
UIApplication* app = [UIApplication sharedApplication];
[app endBackgroundTask:bgTask];
// ... more code to read the token ...
}
- (void)pushRegistry:(PKPushRegistry *)registry
didReceiveIncomingPushWithPayload:(PKPushPayload *)payload
forType:(NSString *)type {
// ... logging to check if notification is received ...
}
Also I have "Voice over IP" and "Remote notifications" enabled in Background modes.
I know other apps like Whatsapp are capable of being relaunched in this scenario, so I don't understand what I am doing wrong.
On a related note, it doesn't help to do the following 1) Force quit 2) Send a pushkit notification - which will be received 3) Reboot. The app will not be relaunched and a new push notification will still not relaunch it.
After I tested the app with an AdHoc provisioning profile (and installed it from iTunes), the Voip push notifications served through prod gateway.push.apple.com instead of gateway.sandbox.push.apple.com started waking up the force-quitted app after reboot.
The os is apparently handling development and production in a different manner.
Looking further into the APSD logs, I found that when using a development provisioning profile the following is printed out:
: XXXX-XX-XX XX:XX:XX +0300 apsd[97]:
These enabled topics have been removed {(
"YOUR_BUNLE_IDENTIFIER"
)}
This does not happen when using an adhoc provisioning profile.

Pubnub connection stops when application goes into background state iOS 8

Title says it all.
I've looked at this question and also here on the pubnub forums (same question, just different suggestion).
The core of the issue is that as soon as the application suspends, pubnub connectivity is queued and not sent until the app goes back to foreground. It seems to be a reasonable thing to do to send a notification saying that you're going in the background on your channel but it doesn't work.
From my readings I understand that pubnub uses websockets and that it is not allowed in background mode. Even tried to enable VOIP as a background mode with no luck but Location updates bg mode works. However, using this will have my app rejected as I don't use location services.
When running this code
- (void)applicationDidEnterBackground:(UIApplication *)application {
[PubNub sendMessage:#"Hello from PubNub iOS!" toChannel:self.myChannel;
}
I get this log entry from pubnub (so at least I know the command is ran):
Looks like the client suspended"; Fix suggestion="Make sure that your application is configured to run persistently in background
I have been killing myself over this for a day. One of these days where you start doing something that you think is pretty simple, a 15min thing and it turns into a day of frustration ... You know what I mean :)
I was actually able to send the messages I needed when the app was about to enter Background. And without enabling any of the background modes.
I took advantage of the background finite task as explained is this tutorial.
- (void)applicationWillResignActive:(UIApplication *)application {
[PubNub updateClientState:#"MyID" state:#{#"appState":#"Background",#"userNickname":#"MyNickname"} forObject:[PNChannel channelWithName:#"MyChannel"]];
self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(#"Background handler called. Not running background tasks anymore.");
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
self.backgroundTask = UIBackgroundTaskInvalid;
}];
}
And implementing the stop background when coming back online
- (void)applicationDidBecomeActive:(UIApplication *)application {
if (self.backgroundTask != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
self.backgroundTask = UIBackgroundTaskInvalid;
NSLog(#"Task invalidated");
}
}

How to keep a block of code alive in background?

I have searched for this topic bu mostly i face the restrictions of Apple. I need to once per minute control my server and ifthere is a change, fire a local notification. What i need is, how to keep timer(NSTimer) alive in background(or when the device lock is activated..) Any idea please. Thanks
You could do your logic in the server part and if there are changes send a Push Notification.
You need to reconsider the design of your app. You can't guarantee that your app will never be killed when the OS goes out hunting for memory to free up. What happens in that scenario? Push Notifications are your best bet here. First of all, you don't need to be polling your server every 60 seconds; you just fire a notification when the content you're interested in changes on the server. Secondly, the notification will be received even if your app isn't running.
The other issue is that you have to tell Apple, via your info.plist, which background modes your app supports. This is really for apps that run music or VoIP in the background. Polling a web server is not one of those supported modes. With push notifications, you also get some delegate methods you can use to handle the information passed in through the notification when the app enters the foreground.
My app continuously runs in the background with following piece of code.....
-(void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask = 0;
backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(backgroundTask) userInfo:nil repeats:YES];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
}
Now do whatever you want in the backgroundTask method.

Resources