iOS:Issue with Silent Push triggering Local Notification - ios

In my app I am using a daily silent push to trigger some calculations inside my app and based on the calculation result I am triggering a local notification.
Say for example: A daily goal.
Once the silent notification reaches my app, I trigger a background method to calculate the user data and compare whether his/ her data achieved a goal and if yes I trigger a local notification so that user can open the app and check.
It is not working consistently, sometimes I get it and sometimes don't. When I debugged, what I saw was that the execution get paused in between and execution gets resumed only if the user opens the app again or I trigger a silent push again. Not sure why the background execution (the calculation) gets paused, and if I trigger a push or if i manually try to open the app, I can see the breakpoint appearing back and it continues from the place it was paused.
I am wondering whether it is because of some time limit??
UPDATE:
I am using UrbanAirshipSDK and they have some handlers overridden and I am using the below method to handle the notification. This is written in the appdelegate and gets called when I get a notification when app is in background.
/**
* Called when a push notification is received while the app is running in the background
* for applications with the "remote-notification" background mode.
* Overridden by receivedBackgroundNotification:fetchCompletionHandler.
*
* #param notification The notification dictionary.
*/
- (void)receivedBackgroundNotification:(NSDictionary *)notification;
I first check for content-available in the payload and treat as silent and do the calculations. This works fine intermittently but not consistently. I am closing towards release dates and I really worried.

I assume that you use:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
In this case you should not forget to call completion handler and according to Apple docs you have only 30 seconds to do so:
As soon as you finish processing the notification, you must call the
block in the handler parameter or your app will be terminated. Your
app has up to 30 seconds of wall-clock time to process the
notification and call the specified completion handler block. In
practice, you should call the handler block as soon as you are done
processing the notification. The system tracks the elapsed time, power
usage, and data costs for your app’s background downloads. Apps that
use significant amounts of power when processing remote notifications
may not always be woken up early to process future notifications.

Related

DidRecieveRemoteNotification not inwoked immediately when app in background

I'm trying to use Apple push notifications. Everyting works fine except when app is in background.
The callback method DidRecieveRemoteNotification is sometimes not called immediately after notification is received. Sometimes it takes more then 5 minutes to be invoked. Is this standard behavior?
When the app is running in foreground callback method is called right after receiving notification. I would like the same behavior even when app is in background.
Silent notifications will invoke their callback immediately when in the background as their purpose is to wake the app up for a short period of time.
In order to save battery, regular push notifications won't invoke the callback while in the background (unless your app is executing something else in the background). The callback will be called the next time the app is opened, whether from tapping the notification or otherwise.
This is not something you can fix, this is how the eco-system works. If you need your app to do something immediately, you will have to use silent notifications

iOS Background Task/Push Notification

I am building an app that needs to speak data while the app is in the background (or screen is off). The speaking part is done via AVSpeechSynthesizer.
My Android app launches a service that listens for the data and whenever it needs to say something, I say it and its done. Now iOS doesn't allow these kind background tasks from what I read. The closet thing is called 'Executing Finite-Length Tasks' which looks like has a time limit of 10 mins max. So this will not work for me. Instead it looks like I need to use Push Notifications.
Looking at the documentation for Push Notifications, if I understand it correctly, if my app is in the Foreground, then my app receives the Push Notification instantly. While if my app is in the Background, a notification is set (notification center), and once the user hits the notification, my app launches with the payload.
The question here is, is there anyway to make a push notification wake my app immediately so I can speak some info?
Or what are some of the other alternative approaches that should be used in this case?
My other idea would be to implement some sort of mp3 stream per user, that would stream audio which I could play in the background. But this seems excessive for what I am trying to accomplish?
I'm not sure if you'll be able to invoke speech synthesis from the background, but you can have you app notified when the push arrives. The trick is to implement the
- (void) application: (RMApp *) application didReceiveRemoteNotification: (NSDictionary *) userInfo fetchCompletionHandler: (void (^)(UIBackgroundFetchResult result)) completionHandler
UIApplicationDelegate method (note the completionHandler parameter).
You must also set the Remote Notifications Background Mode capability.
And you must set content-available in your push notification payload.
A decent writeup on these steps found here:
http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled/

Trigger backgroundFetch with UILocalnotification?

PeopleInTheKnow!
I'm writing this app, that uses Background Fetch, to see if there's some new information on a server.
Background Fetch works fine, but is pretty unpredictable in when it performs its trick.
Therefore, I though I should use scheduled UILocalNotifications, so I have more control over the exact timing and frequency of the execution of the method associated with Background Fetch.
But this is where I get confused:
My scheduled UILocalNotifications fire as expected. But the delegate
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
// I recieved a notification
NSString * notificationType = [notification.userInfo valueForKey:#"dailyCheck"];
NSLog(#"Received Notification of type %#",notificationType);
}
in which I would like to call the server check method, is only called when the app is active.
This seems to make such a mechanism useless for my case.
Could any of you advise me what would be the best approach, to make sure that my app, be it in the foreground or background, will check the server every day at a a specific time?
I should add, that I don't expect any user interaction here. So, no alerts with buttons or anything like it.
Thanks ahead

Is it posible to run an start an app (in background) automatically at a given time?

I have an app that has different downloaded content each day, but I want to automatically update this content each time its available and send a notification to the user when the app has already downloaded the content locally (running in background). Should I use the applicationDidEnterBackground?
- (void)applicationDidEnterBackground:(UIApplication *)application{}
but I was reading this background state may only last minutes and is must likely to enter background after the user changed to another app or clicked home, so how can I trigger this background state (when inactive) at 1:00 am for example? ... Another alternative could be just sending a notification to the user when new content is available and download it as soon as the user opens the app (but this is certainly plan B).
I'm open to suggestions
You can send a silent push notification. This works the same was as a normal push notification only that it doesn't make a sound or alert the user to the fact that they have received it. Your app will then be opened in the background by iOS and will run your code.
The push will then trigger this del method:
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
You can then execute your code in the completion handler. There is a time limit and the silent push notifications are throttled by APNS automatically to prevent constant spamming.
The next time the user opens your app the new content will be preloaded and waiting for them. This seems to be what you are after ... ?
See: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4
Near the bottom there are two parts:
"Fetching Small Amounts of Content Regularly"
and
"Using Push Notifications to Initiate a Download"

How to update Database BackgroundMode when Local Notification are arrive?

I have a Local Notification which fire every minute. Its fire date & other data are taking into the local database. I am used the notification delegate method for notification which gives me notification Active & Background mode.
When Notification fire then update sqlite database every notification (every minute). when notification fire it’s update sqlite database In Application Active State. That’s state working done.
my code like
-(void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
// I recieved a notification
if (notification) {
[Update DataBase Method];
}
}
But my problem is when Application goes Background Mode then Database not Update only Notification Received.
How can I update my database its goes in Background mode (or any state) at every notification fire?
Here is the apple site that explain the task in background/suspend mode..
(Recommended) Register for any notifications that report system changes your app needs. When an app is suspended, the system queues key notifications and delivers them when the app resumes execution. Apps should use these notifications to make a smooth transition back to execution. For more information, see “Processing Queued Notifications at Wakeup Time.”
So if app is queuing the notification than you can access this notification data when your app opens next time.
So what you should have to do is that ..
When your app start check that if there is any notification.
If there is than do the database operation according to the data of the notification
Do the Database update task from applicationDidBecomeActive method. applicationDidBecomeActive method will called whenever your app become active.
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive.
//If the application was previously in the background, optionally refresh the user interface.
NSLog(#"Dilip applicationDidBecomeActive");
}
I think this will do the work...

Resources