I was wondering if there was a way to wake up an app that has been terminated by the user on ios8-9. By terminated I mean double click on the home button and swipe up.
Is it somehow possible to launch an app by sending a silent push notification so that didreceiveremotenotification gets fired and gives me some runtime ?
I have noticed that a fair share of my users terminate my app. As I rely heavily on background fetch, this a problem. My idea was to send silent push notifications to launch the app in the background and trigger background fetch.
Short Answer: No That is not possible.
Detail:
When there is any new content on server you will send Remote Notification to your application to inform about that. (A Remote Notification is really just a normal Push Notification with the content-available flag set)
When application received this Remote Notification it calls following method:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
In Documentation of this method it is clearly written:
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.
Reference:
objc.io: Remote Notifications
Apple Doc about application:didReceiveRemoteNotification:fetchCompletionHandler:
Related
We had implemented following method to receive remote notification and enabled "Background Fetch and Remote Notification under Capabilities of a project". Method is hitting even if app is in foreground or background. But "If app is killed, received notification in background then following method doesn't get called". How to fix this?
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Some storage code
}
Advance Thanks for any help !
Killing the app indirectly disables push notifications.
This is because if the user kills the app, the OS will not relaunch it unless the user taps the app icon on the home screen.
This is unfortunate, because most users don't know that killing apps (which is fun!) has these annoying collateral effects.
This is from Apple's "App Programming Guide for iOS":
In most cases, the system does not relaunch apps after they are force quit by the user. One exception is location apps, which in iOS 8 and later are relaunched after being force quit by the user. In other cases, though, the user must launch the app explicitly or reboot the device before the app can be launched automatically into the background by the system.
When an app is killed. Push notification will not support. Because your app is no longer running. So, there is no way to receieve you the push notification.
If the app is not active, this method won't be called. Instead, if the user launches the app from the notification the app will have a payload in didFinishLaunchingWithOptions, in here you can handle the notification.
If the app isn't opened from the notification, you won't get this.
What exactly are you trying to? You can have the app ask for background time, and execute code as needed.
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.
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/
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.
I have a problem with detecting a push notification from APNS.
If there is a push notification from APNS when the app is not running or in background,
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
is not work.
And when I'm back to the foreground without selecting a push notification message from notification centre, it is not work.
How can I solve this problem?
I'm testing the app in iOS 6.13 and iOS 7.0.4.
Thanks for your help. :)
Actually,the first receiver of notifications is system,not your app.
If your app isn't in foreground, app's application:didReceiveRemoteNotification:fetchCompletionHandler will never be called until you tap the notification to make your app becomes to the foreground again.
When a push notification arrives and the user clicks 'cancel', your app has no way to read that push notification again. You have to implement a separate functionality (most probably on server-side) to fetch a list of notifications sent to this device.
From Apple documentation:
Implement this method if your app supports the remote-notification background mode.
Which mean the method you are using focuses on the background task. And only available after iOS 7.0.
I recommend you use application:didReceiveRemoteNotification:.
From Apple documentation:
If the app is running and receives a remote notification, the app calls this method to process the notification.