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

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.

Related

objective c Push notification BackGround remove old one when receive new one in Notification center

I created a application. That receives push notifications from server.
This all works for me.
But the only thing is that i want that my old push notification in notification center will be removed when i receive a new one.
What do i need to do in this function
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo {
This doesn't solve my problem
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
doesn't work
It is for Background(when you are home screen or locked your phone).
Or is it possible to replace your old push notification for new one

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.

Notifications in iOS

I have a requirement for an application, when it has multiple notifications and when the user clicks one of those, the rest of the notifications of the same app should still be visible in the notification centre. Normally what happens is those other notifications also get cleared. Is there a way that I can bypass this scenario?
Update - Is there a way that we can access received push notifications in the notification center?
Its done by iOS. Programmers have no control over it.
You can get the notification if the user click on the item in notification center and application is opening from it. Following is the code for it
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
//your notification will be in the dictionary if application is opening from notification center
return YES;
}

How can I defer handling a push notification in a running ios app?

If application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo is triggered in my running app, but I don't want to handle it right now, is it possible to leave it posted in the Notification Center so the user can swipe down later and click on it?
No, active apps get the push notification directly and the notification will not be added to the notification center.
You will have to handle it directly or store it your self for later use.

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