How to consume iOS Push notification in the app only - ios

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.

Related

How a push notification behaves based on iOS application state

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.

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.

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

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

Silent push notification is not working

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.

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.

Resources