Showing and deleting number on badge depending upon the Apple Push Notifications - ios

I have an app where I am using apple push notifications. Now I need to show the badge number depending upon the notifications, and I also need to decrease the badge number accordingly.
I know this method is used for showing badge, but I am getting how to implement it.
[UIApplication sharedApplication].applicationIconBadgeNumber = badgeNumer;
Can anyone help me with this?

You can use your code inside this method to show badge number.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

It will show until you set it to zero and you can do it with the following code:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]
It is more common to set the badge number as you receive the notification, in either application:didReceiveRemoteNotification: or application:didFinishLaunchingWithOptions: methods of your UIApplicationDelegate class.
You can read more about it in the Local and Push Notification Programming Guide

Related

iOS: Programmatically dismiss a notification that is displayed on lock screen?

Imagine this: A user sees a notification on their lock screen e.g. "your server is online". Then something changes e.g. the server goes offline. Can I programmatically remove that notification (dismiss it) from the background even after it has been displayed on the lock screen?
Yes you actually can do this, you typically see it in action in messenger apps or social networking apps, for example, in some messenger app that has a web version, if you receive a message and you read it from the web but you already have received the push on your iOS app, when this happens you must send another push without display message, but a tag with value that indicates whatever you want:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
if([[userInfo objectForKey:#"reset"] boolValue]){
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
}
}
This is a silent push notification.

How to know what's coming from push notification in iOS?

I can't NSLog as simulator doesn't support push notification. How I can view the data coming from push notification and what's in that launchOption dictionary?
First of all you want real devices to check push notification.
There is one method to check content of push notification in ios
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NsLog("%#",userInfo);//In the userinfo you will get the content of push notification.
}
May be it will help you.

How can we remotely dismiss a Push Notification?

My app daily broadcasts a Push Notification (PN) to all users which becomes irrelevant after 4 hours. Is there any way I can remove that notification on every user's notification centre that has not tapped it within those 4 hours?
I used to think this is not possible yet, but became hopeful after seeing the Google's Hangout app behaviour - It sends PN to Mac & iOS... and if I read the message on Mac, it automatically immediately removes it from iOS' Notification Center.
I did extensive research on google, surprisingly found nothing on this - just one question here which has been duly closed!
The trick is to make your app support background fetching and handle the push notification when you app is in the background.
Then in the application:didReceiveRemoteNotification:fetchCompletionHandler: set the application badge to 0 so that all you push notification are removed from the notification center.
Send a special push notification where there is not data displayed to user but does contain a an command to reset the push notification state.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
if([[userInfo objectForKey:#"reset"] boolValue]){
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
}
}
It is only possible to dismiss a notification once the user has tapped. You can't dismiss on hide it if the user hasn't tapped it to open.
If you want to dismiss an opened notification, you can try cancelLocalNotification: to dismiss a notification that is presenting an alert at present.
According to apple documentation:
You can cancel a specific scheduled notification by calling cancelLocalNotification: on the application object, and you can cancel all scheduled notifications by calling cancelAllLocalNotifications. Both of these methods also programmatically dismiss a currently displayed notification alert.

Is it possible to handle the selected local notification when activing the app from iOS notification panel?

When launching the app from a local notification you can use the application:didFinishLaunchingWithOptions: method of ApplicationDelegate to handle the notification, but this method is not called when you select the notification in the notifications panel and the app is in background.
Is there a way to handle the notification in that case?
I want to show a special viewcontroller when the app becomes active by this cause.
Thanks in advance!
Did you check the below delegate method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

Remove single remote notification from Notification Center

my app receives remote notification from Apple server.
Is there a way to remove a single remote notification from notification center (the drop-down menu available from iOs 5.0+), when the user taps on it?
Thanks!
There is no way to remove a specific notification as of iOS SDK 5.0. The way to remove all the notifications from your app so they don't show in the Notification Center when the user opens the app from one of them, is to set the app badge to 0, like this:
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
EDIT: on iOS 8, SpringBoard seems to be automatically dismissing a notification when you tap on it on the Notification Center to open the app.
Here is a suggestion, though it does have its flaws, and I haven't tried it myself:
Push a silent notification (contentAvailable:true), don't include a "alert" inside the push, place the alert text in a custom property of the push
Handle the incoming push and trigger a local notification, display it immediately
If the user clicks the local notification, use the [UIApplication cancelLocalNotification:] which should remove the notification from the notification center.
When you call the method:
[application cancelAllLocalNotifications];
inside the AppDelegate methods:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
and
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
All Local and Push Notifications will be remove on that for the particular app.

Resources