Newsstand background notification call aborts (time limit?) - ios

I am developing a iOS Newsstand App.
I use the background download feature.
Everything except the following works fine:
If the App is in the background (started and then home button clicked) and it receives a remote push notification the method [UIApplicationDelegate didReceiveRemoteNotification] is called as expected. Then I do some calculations to get all the download URL's.
The first time the push is received this works fine.
During the handling of the following pushes the called syncData Method is aborted.
The whole computation can take up to 10 seconds.
Is there something like a time out on background calls?
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"Received push notification: %#", userInfo.description);
[self.downloadManager syncData];
}

From what I understand, you need to call this method:
[UIApplication beginTaskWithExpirationHandler:];

Related

Call a method in specific time even if app is killed

I am developing an app which only works in 8.30am to 5.30pm. I want to store the data only in between 8.30am to 5.30 pm. I used local notification for doing so. But it only works when user tap the notification.In 8.30am and 5.30pm, i need to execute some code even if the app is killed. Is there any other mechanism to do so...?
Here is my code:
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification!=nil)
{
[self application:application didReceiveLocalNotification:notification];
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
code to be executed;
}
There is no way to execute a method in your app if the app is killed. You can bring the app up in the background using silent notifications. But silent notifications are better suited for News apps or apps which need to download content in the background so it is readily available for users when the app comes to the foreground.
Apart from this, the only way to execute the method is when it is either in the foreground or at least active in the background (using one of the available background modes). If your app is using a background mode only to stay in the background, Apple will reject the app, so be careful.

push notification Handling when app is not running / call a url if push receive the app

i have a question. I have configured my app for development push notifications and all was working fine.
All push notifications arrived the app on the device. But i want to handle the push notification in a special way. If my app is running or in background the function
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
will work fine. But is the app killed the function doesn't work. I need a idea to handle incomming push notifications whether if the app is running, in background or killed. If a push was received the app must call a callback URL. if there any way to realize this?
for example i send the following apn string:
{"aps":{"content-available":1,"alert":"This","badge":1},"callback":"https://www.xxxxxxxxx.com/sf/daniel_push/985270815/12323453534534/?device(id)=1111111111&s="}
At the end all what i want is to call the callback url if the app becomes a push notification.
Try this in didFinishLaunching:
NSDictionary *userData = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userData) {
// handle notification
}

IOS: Apple Push Notifications understanding

Four questions about Push Notifications.
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
//app is in foreground I can fetch
} else {
//App is in background, can I still fetch the server? Is there a time limit?
}
}
Using the code above, when the app is in background, can I still fetch the server? Is there a time limit? If the answer is: No, I cannot fetch the server in the background, are there alternatives?
Is there any other benefit of using silent notification besides triggering some method to run within 30 seconds when the app is in the background?
If using silent notification and the app is in the foreground, is the following method still called?
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
completionHandler(UIBackgroundFetchResultNewData);
}
In the payload does "content-available" : 1 calls the method in Q3 (above)? or is there anything else that can trigger the method in Q3?
Yes you can still fetch the server when the app is in background. For many apps, the background state is just a brief stop on the way to the app being suspended.
No this is the main benefit of using silent notification see this link.
Yes application:didReceiveRemoteNotification:fetchCompletionHandler: is called regardless of app state.If app is suspended or not running, then the system wakes up or launches your app and puts it into the background running state before calling the method.
This method is intended for showing the updated content to the user.When this method is called, your app has up to 30 seconds of wall-clock time to perform the download operation and call the specified completion handler block. If the handler is not called in time, your app will be suspended.
Yes it calls application:didReceiveRemoteNotification:fetchCompletionHandler:, If you want a push notification to wake your app up in the background you need to enable the Remote Notifications capability and then implement the code to handle that remote notification (either by detecting it in the application:didFinishLaunchingWithOptions: if your app is not already running, or by implementing application:didReceiveRemoteNotification:fetchCompletionHandler: in the case your app is already running). In response to the remote notificaiton you would internally trigger your fetching code. Of course you also need to be doing registering for the remote notifications and sending the token to your server. please see this discussion

Schedule notification in background task

I'm developing a Calendar/Alarm app for iOS which is synchronising with a web server. When an activity is added on the server, a push notification is sent out so that the iOS client can fetch the new data and, if needed, update and schedule the time for next alarm (local notification).
But this only works when the app is open on client side. I would like the client to receive the push notifications and if needed, re-schedule the time for next alarm in background.
Is this impossible on iOS?
You can use Background Fetch for this, where the OS will "wake up" your app periodically to perform data fetching in the background.
First, enable the background fetch capability for your app. In XCode 6, view your project, then go to the Capabilities tab, turn on Background Modes, and check Background Fetch.
Then you'll have to implement some codes in the App Delegate:
In application:didFinishLaunchingWithOptions:, add:
[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
The above sets how often you wish the system to "wake up" your app for background processes ideally. Note that the final frequency is determined by an algorithm in the iOS, so it may not always be this often.
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
//fetch code here
completionHandler(UIBackgroundFetchResultNewData);
}
The above is the actual overridden function that is called during this period of background process. Remember to call the completionHandler - failing to do so might reduce the chance of your app being run in the background next time (or so says the docs). The enums you may pass to the completionHandler are UIBackgroundFetchResultNewData, UIBackgroundFetchResultNoData, UIBackgroundFetchResultFailed. Use one of these depending on the result of your fetch.
// use this methods in Appdeleagte
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[self showAlarm:notification.alertBody];
application.applicationIconBadgeNumber = 1;
application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber-1;
}
// call this in appdelagete
-(void)makeNotificationRequest:(UILocalNotification *)notification1
{
[self showAlarm:notification1.alertBody];
}
// call this mathods in appdelagte
- (void)showAlarm:(NSString *)text {
**strong text**
// set notification and call this notification methods your another view .....
[[NSNotificationCenter defaultCenter] postNotificationName:#"uniqueNotificationName" object:self]; //leak
}

Remote notification payload is null

I'm trying to integrate push notifications within my app. I've got handling remote notifications while the app is currently running working fine, however I'm trying to handle them when the app is not currently running using
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
however, it seems whenever I click the notification alert and it start up the app my notification payload is null.
I've used NSLog to log the payload and its showing that it is null.
Is there any reason it would be null? I know my notifications are setup right since they work within the app, but for some reason I cant figure this out. I've also logged the launchOptions and its showing null as well.
this is inside my didFinishLaunchingWithOptions method:
NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
NSLog(#"launch options: %#", launchOptions);
NSLog(#"payload: %#", notificationPayload);
I wrote a blog post about this a few years ago, and it's since changed in terms of where you set it up in Xcode.
But here's the blog post just in case, for some background information. http://runmad.com/blog/2010/06/debugging-executables-for-push-notifcations/
Now, in order to debug push notifications you need to edit the scheme.
You can change your target's launch settings in "Manage Scheme" to Wait for .app to be launched manually, which allows you debug by setting a breakpoint in application: didFinishLaunchingWithOptions: and sending the push notification to trigger the launch of your app.
application:didReceiveRemoteNotification: is a delegate method called when your app is currently active or comes to foreground.
It's important to test all 3 these scenarios thoroughly (from launch, from background, while active).
In the second method, you can check the app's state with
[application applicationState] == UIApplicationStateActive
and deal with the push notification accordingly.
If you want to see your payload information use following notification delegate method.And be sure that you have launch your app from notification center whenever you got notification, then only this method call.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"userInfo: %#", userInfo);
NSDictionary *dict=[userInfo objectForKey:#"aps"];
NSString *strPushMessege=[dict objectForKey:#"alert"];
NSLog(#"strPushMessege %#",strPushMessege);
}

Resources