How to clear icon counter when clicking an app icon in iphone?

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

But when we click on the app icon (icon counter), it brings the app to the front, but the icon counter and alerts are not cleared.

We used the following code in the didFinishLaunchingWithOptions method (in the appdelegate.m file), which clears the alerts and resets the icon only when the alerts are clicked

 application.applicationIconBadgeNumber = 0;

      

can anyone provide us with a solution that shows the same behavior when we click on an app icon with an icon counter.

+3


source to share


2 answers


To clear the icon counter whenever an app becomes active, use the method delegate

call [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; in applicationWillEnterForeground and 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

      

enter image description here

+20


source


In Swift, do the following to add func applicationWillEnterForeground(application: UIApplication)

:



UIApplication.sharedApplication().applicationIconBadgeNumber = 0

      

+3


source







All Articles