Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear badge counter on click of app icon in iphone?

We have developed an ios app using phonegap and have implemented push notification functionality in our app. Push notification works perfectly fine for us. We have configured push notification for both (alerts and badge) and both works fine. When we click on the alert list it redirects us to the application and clears all the notifications from the alert list and also the badge counter is set to 0.

But when we click on the application icon(badge counter) it brings app to the foreground but the badge counter and alerts are not getting cleared.

We have used following code in didFinishLaunchingWithOptions method (in appdelegate.m file)that clears out the alerts and resets the badge only on-click of alerts

 application.applicationIconBadgeNumber = 0;

can anyone provide us the solution that shows same behaviour when we click on app icon with badge counter.

like image 735
user2240189 Avatar asked Dec 05 '14 08:12

user2240189


People also ask

How do I remove a badge from an app's icon?

Tap the "Badge App Icon" button to toggle it from "On" to "Off." Alternatively, if you don't want any notifications for that app, tap the "Notification Center" button at the top of the screen to turn it off instead. When you click the "Home" button, the badge is removed from the app's icon.

How do I Turn Off badge notifications on an iPhone?

Tap the "Settings" icon on the iPhone's home screen and then tap "Notifications." Scroll down through the list of apps in the Notification Section and select the app you want disabled from using badge alerts. The notifications used by that app are displayed beneath its name, such as "Badges," "Alerts" and "Banners."

How do I clear the Red Badge on my iPhone?

Launch the app that is displaying an alert by tapping its icon on the iPhone's home screen. In some cases, this is sufficient to clear the badge. For example, if there is a red circle with a number on the Messages icon, it indicates that you have new text messages. Launching the Messages app and closing it again erases the icon's red badge.

How do I remove a badge from the voicemail app?

If there is a badge on the "Voicemail" button, you need to tap that button and listen to the message to clear that badge. In the case of an iOS update, you'll have to install the update to clear that badge. When you go back to the home screen, the badge is removed from the app's icon.


3 Answers

To clear the badge count whenever the application becomes active use delegate method. You can use UIApplicationDelegate in AppDelegate.

call the [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; in either applicationWillEnterForeground nor applicationDidBecomeActive

- (void)applicationWillEnterForeground:(UIApplication *)application

{
 [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
 }

or

- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

Swift

func applicationWillEnterForeground(application: UIApplication) {
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
}

or

func applicationDidBecomeActive(application: UIApplication) {
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
}

For Swift 3:

UIApplication.shared.applicationIconBadgeNumber = 0

iOS 13 >

func sceneDidBecomeActive(_ scene: UIScene) {
UIApplication.shared.applicationIconBadgeNumber = 0
}

enter image description here

like image 131
Anbu.Karthik Avatar answered Oct 12 '22 00:10

Anbu.Karthik


If you are on iOS 13 supporting multiple windows, do the same on the method Scene Became active like this. This method is on SceneDelegate.

func sceneDidBecomeActive(_ scene: UIScene) {
    UIApplication.shared.applicationIconBadgeNumber = 0;
}
like image 39
JBarros35 Avatar answered Oct 12 '22 00:10

JBarros35


In Swift the following works to put in func applicationWillEnterForeground(application: UIApplication):

UIApplication.sharedApplication().applicationIconBadgeNumber = 0
like image 5
Ciryon Avatar answered Oct 11 '22 22:10

Ciryon