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.
Related
I am trying to achieve silent push notification.I need to save the silent push notification data in my database in app so that I can show the unseen notification and its count to users when the users uses the app.
I am using Xcode 6.1 and targeting iOS 7 and later devices. Currently, I am using development certificates for push notification.
I have checked remote notification in the background modes of target project capabilities, also the info-list's background mode has "App downloads content in response to push notifications" in required background modes.
In my AppDelegate.m, I am able to register remote notification and also get the device token. Now when I send push notification, my delegate method gets called when app is in foreground but when app is in background/not running, it doesn't get called though I receive the push notification in banner.
I have implemented this method in AppDelegate.m
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(#"Received push notification");
}
The payload I am sending looks like this:
{
aps: {
content-available: 1
}
}
I tried adding priority, sound etc. But nothing helped me.
Can someone help me to figure out how to save silent push notification data in app when app is in running in the background or not running?
Kindly help me to resolve this issue.
So, you should definitely be able to receive them in the background, but be warned that if the user deliberately kills your app from the task switcher then it won't get them anymore.
With iOS 7 and above the callback is as you say, but before that it was
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
Seems unlikely you are targeting iOS 6 though so I would go with either a malformed push packet or background push settings not being quite right.
Do you have the apps info.plistUIBackgroundModes set to remote-notificiton?
Then also add this method to help you debug
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
//Success
handler(UIBackgroundFetchResultNewData);
}
Silent push notifications are different than user-facing notifications. They are treated as low priority and are rate limited by both APNS and iOS. In practice, this means that silent notifications can only be sent infrequently and there may be a very long delay before they are delivered to an application.
The behavior you are describing is likely the wakeup rate limiter on the device. iOS limits how often apps are launched to perform background work. This is to prevent abusive behavior - some apps might want to stay alive in the background draining the battery forever.
The wakeup rate limiter is not active when Xcode is attached, which makes silent notifications appear to be delivered instantly.
My iOS Notifications book has a lengthy chapter describing the rate limiters and how to work with them.
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
When receive a push notification and my application is totally closed, how can handle this info?
Only can get data from NSDictionary on this method didFinishLaunchingWithOptions: or
didReceiveRemoteNotification:
for example: when the user open the application how get data from the push notification?, and not when the user open the push notification directly.
Is there a method that responds and detect if a notification has not been read?
You'll want to implement
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
This will launch your app if needed, then you'll want to save the data somewhere so when the user next's starts the app you grab the data and do something with it.
From Apples Doc's:
Discussion
Use this method to process incoming remote notifications for your app.
Unlike the application:didReceiveRemoteNotification: method, which is
called only when your app is running in the foreground, the system
calls this method when your app is running in the foreground or
background. In addition, if you enabled the remote notifications
background mode, the system launches your app (or wakes it from the
suspended state) and puts it in the background state when a push
notification arrives. 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.
Just look into the method and I'm certain you'll figure it out :)
I did a quick google, these look like they will help:
SO example: didReceiveRemoteNotification: fetchCompletionHandler: open from icon vs push notification
The first tutorial i saw on it: http://hayageek.com/ios-background-fetch/
Update Question :
The requirement is; as soon as I receive silent notification, I want to run a web service and show the one liner in the notification bar. It has to work if the app is killed also. any workaround ?
I am trying following method below.
I am new to iOS and i struggled with silent push notification,googled a lot and got stuck.
Will iOS awake my app when i receive silent push notification when app is not launched(i.e when app is removed from app switcher).
my pay load is as
{
aps: {
content-available: 1,
sound: ""
}
}
.
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler{
int CA=[[[userInfo valueForKey:#"aps"] valueForKey:#"content-available"] intValue];
if (CA==1) {
my action...
}
completionHandler(UIBackgroundFetchResultNewData);
}
this method is called and works fine when app is in foreground and background.cant awake my app when app is not in running state(i.e app is not launched or killed from app switcher)..
If the App has been removed from the App Switcher, iOS will not awake your app, since the user specifically asked for closing your app.
If the user open your app at least once, and do not remove it from App Switcher, iOS will awake your app
What we have done server-side to handle this is :
If the user's app doesn't connect in the minute after we sent the silent notification, (you can set it as you wish), we send another non-silent push notification to alert the user.
Since the App (is not closed by the user) should automatically fetch data, it should take under a minute.
But for that of course you need a more complex server code than simply sending silent push.
EDIT : (Getting a vote up on this question showed me that it was outdated)
This answer is no longer True... you can now with PushKit wake up your app to do some minor things (like downloading small chunks of data to update content) even if the App has been removed from App Switcher.
Please checkout this:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification:fetchCompletionHandler:
Use this method to process incoming remote notifications for your app.
Unlike the application:didReceiveRemoteNotification: method, which is
called only when your app is running in the foreground, the system
calls this method when your app is running in the foreground or
background. In addition, if you enabled the remote notifications
background mode, the system launches your app (or wakes it from the
suspended state) and puts it in the background state when a remote
notification arrives. 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.
This clearly says that Using new Background Push feature you can Awake the App Only if Your app is suspended Not if it is terminated forcefully by User.
As far I have tested when the app is terminate by the user (swiping up from the app switcher) you won't have background execution time due to silent push flag (content-available) or background fetch.
Also this:
Also keep in mind that if you kill your app from the app switcher
(i.e. swiping up to kill the app) then the OS will never relaunch the
app regardless of push notification or background fetch. In this case
the user has to manually relaunch the app once and then from that
point forward the background activities will be invoked. -pmarcos (Apple worker)
From apple forums: https://devforums.apple.com/message/873265#873265
You CAN get a PUSH-notification and work with it. I know a little way to do this... Open the AppDelegate.m and and find or put this method - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions.
After that, put into this method code like mine:
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
NSDictionary *apsInfo = [userInfo objectForKey:#"aps"];
NSString *alertType = [apsInfo objectForKey:#"type"]; //my own param in PUSH-notification
globalPushType = alertType; //global variable for working with it in some ViewControllers after app's load
}
I know, this helps a lot of people. =)
Is it possible to call a certain method from the AppDelegate after a push notification has been received while the app is in inactive state (Terminated)?
I know it is possible to call a method when the app is opened from the notification (eg. tapping a banner in notification center.)
I'd like to perform a small geolocation check when a push is received from my server. Is that possible and wont my app get rejected?
You can handle a push notification in
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
when app is in background state and working, not inactive.
But you can send a sound & text notification when app is inactive but you can't do anything.
Inactive I mean suspended - not working at all (neither foreground nor background).
It is not possible. Your app badge will only be updated if you are sending it from the server.
There is no API that allows doing something after push on inactive app without user interactions
Apple says: "Inactive State:: The app is running in the foreground but is currently not receiving events. (It may be executing other code though.) An app usually stays in this state only briefly as it transitions to a different state."
So It's not possible that you want.