swift notification update app in background if user doesnt click on notification - ios

I am trying to understand how I can update my app once I get a notification.
What I want to do is simply once the app get a notification I want to simply increment a value and store it like: userDefaults.set(counter, forKey: "someKey")
I know that I can do this easy if a user actually clicks on the notification and the app open.
But is there any way to do this even if the user doesnt click on the notification?

Enable background-fetch and remote notifications in the background modes which can be found in capabilities in Xcode.
If you send your notifications with "content-available" key set to 1, your app's app delegate method for remote notifications will be called. In that method you can store the new data to UserDefaults.
This is the method you need to implement in your app delegate:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
Also, note that this will not work if the application is killed by the user. If your application is a VOIP app, you may use PushKit to make your app open in the background for data updates even when user has killed the app before.

You can enable Remote Notification option in background mode of capabilities.
When you enabled it you can use the delegate method to update value and store it in userdefaults
application:didReceiveRemoteNotification:fetchCompletionHandler:
Apple Docs

Related

Launch an app with push notification after it has been terminated

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:

get data from push notification when app is closed in iOS and app not running in background

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/

How to consume iOS Push notification in the app only

I am working on a app, where I need to send a push notification to the app to start processing data as needed.
How do I send a push notification to the device so that instead of showing the alert message, the notification is forwarded to the app - whether the app is in the foreground or background..
I did implement the delegate method :
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
however this is called only when the app is in the foreground. When the app is in the background the notification shows up on the notification center.
Please advice.
To make this works you need to do few step:
set background mode remote-notification
implement application:didReceiveRemoteNotification:fetchCompletionHandler: method in app delegate
and make sure push notification payload contains key "content-available" : 1
related docs:
App States and Multitasking
Local and Push Notifications in Depth
Is your application using a backend? An example would be Parse or another to store your data.
Parse, for example, allows you to add push notifications that can be customized and changed to get the most out of your application.

iOS; Push notification is not work when the app is not running or in background

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.

Receive Push Notification in background and create a local Notification without the user knowledge

I am receiving a remote push notification, and i need to register a UILocalNotification, within 30 minutes, but i need to do it even if the user do not click in the notification, in the background.
There's any way to do it, like in :
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
}
It works fine here, but only if the user clicks in the notification, or if the app is currently running.
You can't handle a remote notification if the application is not running until the user doesn't launch your app. You should deal with the business logic on server-side. Keep track of when the notifications are sent and trigger the appropriate action after the time frame are exceeded.
You need to turn on "background fetch" in the "capabilities" section of your project settings, and use the application:didReceiveRemoteNotification:fetchCompletionHandler delegate method in your ApplicationDelegate file. This way you can handle remote notifications in the background. Don't forget to set the content-available flag in your aps dictionary when sending the push.

Resources