Pubnub connection stops when application goes into background state iOS 8 - ios

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");
}
}

Related

The "correct" way to handle remote iOS push notifications in react-native?

I'm having problems getting push notifications to work adequately in react-native 0.38 iOS. Specifically there seems to be an issue around handling push notifications after the OS has terminated the app.
It seems that when your app is in a background state, iOS will kill it at some point (presumably in order to free up memory for other processes). And from my experiments it appears that iOS will terminate an app from the background state quite liberally - it's a very common thing. However, if the OS does terminate your app from the background state, it will also revive it should a push notification arrive so long as that notification contains the important content-available=1 flag, AND you've allowed Remote Notifications in background modes for your app.
As of RN0.38, if you receive a push notification that has the content-available=1 flag, you MUST call the finish method which in turn invoke the fetchCompletionHandler. If you don't invoke the completionHandler, the OS will penalise your app for taking up resources and will slow down (or stop) notification delivery - which it does (again quite liberally). I believe you have around 30 seconds of wall clock time to call the fetchCompletionHandler, after which the OS will put you on its naughty list and your app will stop working properly.
So, from the terminated state I can see a push notification arriving, and I can see the OS starting my app. In my AppDelegate I have this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
NSLog(#"push notification when app is not open");
}
...
And I see the message "push notification when app is not open" in the logs. All Good. It works fine when the app is in the foreground and when it's in the background, but not so when terminated.
The next thing to happen is that -application:didReceiveRemoteNotification:fetchCompletionHandler get's called:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(#"push-notification with content body => %#", notification);
if(application.applicationState == UIApplicationStateInactive) {
NSLog(#"push-notification app:Inactive");
} else if (application.applicationState == UIApplicationStateBackground) {
NSLog(#"push-notification app:Background");
} else {
NSLog(#"push-notification app:Active");
}
[RCTPushNotificationManager didReceiveRemoteNotification:notification fetchCompletionHandler:completionHandler];
}
And in the logs, as expected I see "push-notification app:Background". But, immediately after this line in the logs I see the following warning:
Nov 28 10:53:17 Tests-iPhone myapp[1139] <Notice>: push-notification app:Background
Nov 28 10:53:17 Tests-iPhone myapp(UIKit)[1139] <Notice>: Warning: Application delegate received call to -application:didReceiveRemoteNotification:fetchCompletionHandler: but the completion handler was never called.
From this it looks to me as if the app is not given any time at all to process the incoming push notification. The warning that the completionHandler was not called happens immediately and my JS code doesn't get a chance to run. I do not see this message when the app is running in the foreground or background.
I suppose my question is - how do you get a react-native app to gracefully handle an iOS push notification that arrives after the OS has terminated the app?
From the apple docs I gather that an approach to this is to start a background task as soon as a notification arrives. I don't see anywhere in the RN code base where a background task is started, and I'm now actually wondering how push notifications work when the app is in the background without starting a background task.
As an experiment I tried the following, it's a dirty hack, but my app started behaving better in this situation:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(#"push-notification with content body => %#", notification);
if(application.applicationState == UIApplicationStateInactive) {
NSLog(#"push-notification app:Inactive");
} else if (application.applicationState == UIApplicationStateBackground) {
NSLog(#"push-notification app:Background");
} else {
NSLog(#"push-notification app:Active");
}
[RCTPushNotificationManager didReceiveRemoteNotification:notification fetchCompletionHandler:completionHandler];
//start hack
self.pnBgTask = [application beginBackgroundTaskWithName:#"MyPNTask" expirationHandler:^{
[application endBackgroundTask:self.pnBgTask];
self.pnBgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[NSThread sleepForTimeInterval:25.0f];//dodgy hack to give it a chance to complete the 'real' bg-task
[application endBackgroundTask:self.pnBgTask];
self.pnBgTask = UIBackgroundTaskInvalid;
});
//end hack
}
With this change my app appears to work much better when running from xCode, but I haven't tried it out in real deployed app yet.
Thanks for any ideas.
By the way, in order to test this you either need to get iOS to terminate you app (which as far as I can see is close to impossible to do), or - either simply stop it form inside Xcode or change the launch settings to use "Wait for executable to be launched", then send your push notification and the app will start from a terminated state.

How to call Background Fetch iOS with multiple intervals

In my iOS application i want to make an api call for every 4 hours.
As of my research i found that it can be achieved by background task or Local notification or Push Notification.
As of my knowledge, By Notifications (Either Local or Push both) user have to interact with application.
I want to make an api call without user interaction, even app is in background or foreground or active.
I didn't get exact sample code for background fetch which works with intervals.
Some one please guide me in this issue.
Thank you in Advance.
You can set the interval here or specified a minimum fetch interval (The smallest fetch interval supported by the system) like this example:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
return YES;
}
For 4 hours:
NSTimeInterval fourHours = 14400;
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:fourHours];

iOS PjSip - background working

Recently I have to implement background working app with voip. For that I use PJSip.
Right now I have done registering and handling calls working perfectly when app is running.
When app goes to the background, by first 10 minuts works fine -> new incomming calls are captured and are send as local notifications - so this is fine. After 10 minutes sip stops working and incomming calls doesn't arrives as well.
Could you help me with this?
I have check "bacground working - VoiP"
I have done keep alive in pjsip from siphon2 example application.
I have also:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[PjSipService service] setIsInBackground:YES];
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
int KEEP_ALIVE_INTERVAL = 600;
[self performSelectorOnMainThread:#selector(keepAlive) withObject:nil waitUntilDone:YES];
NSLog(#"startingKeepAliveTimeout1");
[application setKeepAliveTimeout:KEEP_ALIVE_INTERVAL handler: ^{
NSLog(#"startingKeepAliveTimeout2");
[self performSelectorOnMainThread:#selector(keepAlive) withObject:nil waitUntilDone:YES];
}];
__block UIBackgroundTaskIdentifier bgTask;
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//[self deregis];
[application endBackgroundTask: bgTask]; //End the task so the system knows that you are done with what you need to perform
bgTask = UIBackgroundTaskInvalid; //Invalidate the background_task
NSLog(#"\n\nRunning in the background!\n\n");
});
}
"keepAlive" function is empty.
I've read that for this function keepAlive is enought - but without background task it doesn't work even 10 minuts.
Application has to works in iOS7 and iOS6
Apps running in background, is performed using setKeepAliveTimeout method in IOS. But from later versions, setKeepAliveTimeout method is deprecated.
You want to use Remote notifications to receive incoming call. particularly for VOIP incoming calls, apple introduced Callkit framework. please follow that framework to accept incoming calls while your VOIP App is in background state.
https://www.raywenderlich.com/150015/callkit-tutorial-ios
I am working on the same project with same requirements and I came across with the same issue recently. I tried many different solutions and finally I found something that is working for now.
Please note that I recently found this solution and I am working on this. I haven't consider the reaction of this code at least for now.
Sample Code :
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self sipConnect];
backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundTask];
}];
}
-(void) endBackgroundTask
{
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
}
You probably use UPD. To make sure you use TCP, when setting up your registrar you need to specify something like this sip:ip_address:port;transport=tcp
Apple has deprecated keepAliveTimeoutHandler. To receive calls when applicationDidEnterBackground you have to use PushKit framework. Read documentation for more information here.

Location update background mode app getting rejected

I have done an app using location update as background mode, and I am updating my location after each keepAlive interval using webservice. I am using the below mentioned code.
if ([application respondsToSelector:#selector(setKeepAliveTimeout:handler:)])
{
[application setKeepAliveTimeout:120 handler:^{
DDLogVerbose(#"KeepAliveHandler");
// Do other keep alive stuff here.
}];
}
self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(#"ending background task");
[[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}];
self.backgroundTimerForLocationUpdate = [NSTimer scheduledTimerWithTimeInterval:120
target:self
selector:#selector(changeAccuracy)
userInfo:nil
repeats:YES];
and in the location delegate I am calling a web request for updating the location in server, to track the user.
Will apple reject the app for using the location update background mode.
Will apple reject the app for using the location update background mode.
According to the Apple Documentation you are only allowed to if your iOS App
Playing and Recording Background Audio An app that plays or records audio continuously (even while the app is running in the background) can register to perform those tasks in the background.Tracking the User’s Location and Implementing a VoIP Client (Voice over Internet Protocol). So Accordingly no problem at all but the final result will be declared by Apple Review Team
Good Luck
Apple will reject application if you start or stop location update while application is in background.
But you can get latest location update in background with method "startMonitoringSignificantLocationChanges". It will update current location value when app is in background and when you will come in foreground mode, You will have latest location information to perform any subsequence activity.

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