Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS application: how to clear notifications?

I've an iOS application where some Push Notification are sent to. My problem is, that the messages/notifications stays in the Notification Center in iOS after then are tapped. How can I remove a notification for my application in the Notification Center next time the application opens?

I came across posts where people are calling setApplicationIconBadgeNumber to a zero-value to clear the notifications. That's seems very weird to me, so I believe that maybe another solution exists?

EDIT1:

I'm having some problems clearing the notifications. Please see my code here:

- (void) clearNotifications {     [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];     [[UIApplication sharedApplication] cancelAllLocalNotifications]; }  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     if (launchOptions != nil)     {         NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];         if (dictionary != nil)         {             NSLog(@"Launched from push notification: %@", dictionary);              [self clearNotifications];         }     }      return YES; }  - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {         NSLog(@"Received notification: %@", userInfo);     [self clearNotifications]; } 

I'm running the App through Xcode. When the App is minimized and I start the App using the notification in the Notification Center, I can see in the log, that the didReceiveRemoteNotification is called and using breakpoints I can see, that the clearNotifications has ran. But still the notification hangs in the Notification Center. Why?

like image 851
dhrm Avatar asked Dec 30 '11 17:12

dhrm


People also ask

How do you get rid of a notification on an app that won't go away?

First, press-and-hold on the persistent notification you want to remove. Another option is to swipe the notification left or right, and then tap on the cogwheel icon shown next to it. Next, tap on the switch next to Permanent to disable it, and then press Save.

How do I delete multiple notifications on my iPhone?

Clear a Group of NotificationsSwipe down from the top left of the screen to show the Notification Center. Tap the X on the group of notifications you want to remove. Tap on Clear.

How do I get rid of the red number on my app icon iPhone?

Open Settings on your iPhone. Scroll down and click on Notifications. Find the app for which you want to hide the red notification bubbles with numbers, i.e., the app icon notification badges. On the next screen, disable the toggle for Badges.


2 Answers

Most likely because Notification Center is a relatively new feature, Apple didn't necessarily want to push a whole new paradigm for clearing notifications. So instead, they multi-purposed [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; to clear said notifications. It might seem a bit weird, and Apple might provide a more intuitive way to do this in the future, but for the time being it's the official way.

Myself, I use this snippet:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

which never fails to clear all of the app's notifications from Notification Center.

like image 104
Patrick Perini Avatar answered Nov 13 '22 00:11

Patrick Perini


Just to expand on pcperini's answer. As he mentions you will need to add the following code to your application:didFinishLaunchingWithOptions: method;

[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

You Also need to increment then decrement the badge in your application:didReceiveRemoteNotification: method if you are trying to clear the message from the message centre so that when a user enters you app from pressing a notification the message centre will also clear, ie;

[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1]; [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
like image 32
ADAM Avatar answered Nov 13 '22 00:11

ADAM