How a push notification behaves based on iOS application state - ios

I want to understand what will happen if a push notification arrives to device.
App Not running - what will happen if notification arrives - state change?
App is in foreground - ?
App is background - ?
App is inactive state ?
I am assuming if App is not running we can invoke by sending a silent push notification in some situations.
Can some one explain me how a push notification works based on app state.

If the application is not running or in background state, if the user accepted to receive push notifications, the push notification will be sent to the device that will display it on screen.
From this displayed notification, you can launch or wake the target application.
The traditional launch callback:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
will be fired and you can access the received notification and its payload from its launchOptions dictionary, using UIApplicationLaunchOptionsRemoteNotificationKey key.
If the application is running and in foreground, the AppDelegate method
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary *)userInfo
will be fired, userInfo containing the push notification payload.

Related

APNs: Change the app badge while the app is the foreground

I have an app and a server-side push sender. When new notifications arrive, the server sends an empty push message which only contains a badge update.
When the app is in the background, the badge is successfully updated. However, when the app is in the foreground, the badge is not updated at all - the push is delivered to the app, which discards it.
The obvious workaround is to catch the push and update the badge from within the app. For some technical reasons this would take some time to take effect (development time, app store check time, users who don't frequently upgrade etc.)
I wonder if there's a way to circumnavigate this and update the badge using a server side APNs push regardless of the app state, foreground or background.
Is there a way to change an iOS app badge using a push message, when the app is in the foreground, without handling the push notification from within the app?
This can only be achieved through application delegate methods defined in your AppDelegate
Deprecated in iOS 10
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
or,
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
The above delegate functions gets called when app is in foreground there you can decode your Push Payload and assign the application badge as follows
[UIApplication sharedApplication].applicationIconBadgeNumber=[[userInfo objectForKey:#"aps"] valueForKey:#"badge"];
Cheers.

Push notification data not getting when app launched directly by clicking app icon

I have a scenario in which app will get push notification and need to show that messages in home screen of my app, for that i saved the message array into user defaults from my app delegate and everything works well, but in following conditions it's not working
if app is in killed state and a notification came and user triggeres the app through the app icon (not from push notification )
if app is in background and notification came and user enters to app through app icon (not from push message ) in this case also
Then i searched for solutions and came to know about silent push notifications (for background mode) and nothing else so i need to know how to handle all scenarios by push notifications and my appdelegete is
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
[self handlePushMessage:remoteNotif];
}
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[self handlePushMessage:userInfo];
}
-(void)handlePushMessage :(NSDictionary*)userInfo{
//method to handle push message
}
Thanks in advance
This is a common issue: if the user does not open your app by means of the displayed notification, there is no* way of getting the related information.
* A possible solution employed by many apps is to check with a remote server for unread notifications (e.g. check for an unset read-date field).
In scenario 1. NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; here remoteNotif return nil as you enter into the app through triggering app icon.
In scenario 2. You can get push notification info through the following method
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
if (userInfo) {
[self handlePushMessage:userInfo];
}
}
if app is in killed state and a notification came and user triggeres the app through the app icon (not from push notification )
If the app is in killed state then the push notification payload can only be handed over to the app when the user taps the push notification itself. If the user launches the app from app icon then the notification payload will not be passed to the application
if app is in background and notification came and user enters to app through app icon (not from push message ) in this case also
if you are targeting iOS7 and up then you need to enable background mode for remote notifications. Please check below link in order to get notification payload even when app is in background
didReceiveRemoteNotification not working in the background
The above mentioned app delegate method gets called when the app is in foreground,Background and suspended state.
There is no way to get the notification payload when app is killed and when the app icon is directly clicked instead of push notification in notification center.
Enable "Remote Notifications" under background modes in capabilities in target settings. This will fetch the notification data even when the app is in the background. Also, make sure to implement:
-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler;
in your app delegate.

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:

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 DidReceiveRemoteNotification Issue

I am doing project in that push notification is the one of the key feature.It is working fine when i am in the app,I received notification and handle that notification.
But the issue is when my app is inactive state or else remove the instance of the app.In this scenario i have received the notification didReceiveRemoteNotification method is not called, and i didn't handle the push notification.
When the App is inactive and a push notification comes through the
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method gets called when the app is resumed and the launchOptions dictionary has the push notification, which you can get with
launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]
then you can process it as normal.

Resources