简体   繁体   English

如何在 iPhone 中点击应用程序图标时清除徽章计数器?

[英]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.我们使用 phonegap 开发了一个 ios 应用程序,并在我们的应用程序中实现了推送通知功能。 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.当我们点击警报列表时,它会将我们重定向到应用程序并清除警报列表中的所有通知,并且徽章计数器设置为 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我们在 didFinishLaunchingWithOptions 方法(在 appdelegate.m 文件中)中使用了以下代码,该代码仅在单击警报时清除警报并重置徽章

 application.applicationIconBadgeNumber = 0;

can anyone provide us the solution that shows same behaviour when we click on app icon with badge counter.当我们点击带有徽章计数器的应用程序图标时,任何人都可以向我们提供显示相同行为的解决方案。

To clear the badge count whenever the application becomes active use delegate method.要在应用程序变为活动状态时清除徽章计数,请使用delegate方法。 You can use UIApplicationDelegate in AppDelegate.您可以在 AppDelegate 中使用UIApplicationDelegate

call the [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];调用[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; in either applicationWillEnterForeground nor applicationDidBecomeActiveapplicationWillEnterForegroundapplicationDidBecomeActive 中

- (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:对于 Swift 3:

UIApplication.shared.applicationIconBadgeNumber = 0

iOS 13 > iOS 13 >

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

在此处输入图片说明

If you are on iOS 13 supporting multiple windows, do the same on the method Scene Became active like this.如果您使用的是支持多个窗口的 iOS 13,请对方法 Scene Became active 执行相同操作,如下所示。 This method is on SceneDelegate.此方法位于 SceneDelegate 上。

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

在 Swift 中,以下工作用于放入func applicationWillEnterForeground(application: UIApplication)

UIApplication.sharedApplication().applicationIconBadgeNumber = 0

This is how I reset it and remove it.这就是我重置和删除它的方式。 You can call this in any vc assuming it has access to the tabBarController:你可以在任何 vc 中调用它,假设它可以访问 tabBarController:

func resetAndRemoveTabBarBadge() {
    
    UIApplication.shared.applicationIconBadgeNumber = 0

    if let tabItems = tabBarController?.tabBar.items {

        let item = 4 // this is whichever tabBarItem you set the badge to appear on

        let tabItem = tabItems[item]
        tabItem.badgeValue = nil
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM