从 iOS 应用程序图标中删除徽章

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9557132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:09:29  来源:igfitidea点击:

Removing badge from iOS app icon

iosiphonebadge

提问by thar

In this application that I'm trying to make, I use push notifications. This part works just fine. When I send a notification I also add a badge to the app icon. The problem is when I lunch the application it should disappear again, but it does not.

在我尝试制作的这个应用程序中,我使用推送通知。这部分工作得很好。当我发送通知时,我还会在应用程序图标上添加一个徽章。问题是当我用午餐时应用程序应该再次消失,但它没有。

-(IBAction)Push{

    NSMutableDictionary *data = [NSMutableDictionary dictionary];

    [data setObject:@"Numfeud: Troels made a move!" forKey:@"alert"];

    [data setObject:[NSNumber numberWithInt:1] forKey:@"badge"];

    [data setObject:@"bar" forKey:@"foo"];

    [PFPush sendPushDataToChannelInBackground:@"GameChannel2" withData:data];
}

In the application didFinishLaunchingWithOptionsI try to set badge to 0 in this way:

application didFinishLaunchingWithOptions我尝试以这种方式将徽章设置为 0 时:

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

How can I clear the application icon badge?

如何清除应用程序图标徽章?

回答by Mark

If your app becomes active again and is still in the background you should reset the badge count in -applicationDidBecomeActive:as well:

如果您的应用再次激活并且仍在后台,您也应该重置徽章计数-applicationDidBecomeActive:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    application.applicationIconBadgeNumber = 0;
}

If your app is still running in the background -application:didFinishLaunchingWithOptions:won't be called.

如果您的应用程序仍在后台运行,-application:didFinishLaunchingWithOptions:则不会被调用。

回答by Fabian Kreiser

Likely, -application:didFinishLaunchingWithOptions:is not being called, because your app is still running in the background. In order to remove the badge count when the app is launched from the background you'll have to reset the badge number in -applicationWillEnterForeground:, too.

可能-application:didFinishLaunchingWithOptions:没有被调用,因为您的应用程序仍在后台运行。为了在从后台启动应用程序时删除徽章计数,您还必须在 中重置徽章编号-applicationWillEnterForeground:

回答by Sourabh Kumbhar

In Swift and In AppDelegate

在 Swift 和 AppDelegate 中

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

回答by handiansom

You can use this codes also.

您也可以使用此代码。

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    application.applicationIconBadgeNumber = 0;
}

or In a specific ViewController

或在特定的 ViewController

- (void)awakeFromNib {
   [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}

回答by S. Matsepura

Maybe call it in applicationWillResignActive(in AppDelegate.m):

也许在applicationWillResignActive(in AppDelegate.m) 中调用它:

-(void)applicationWillResignActive:(UIApplication *)application{
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}

This will help you to clear badgeif pushcome when app is being open. User seeing notificationand you clear it, when he press Home Button(once or twice). Also it will be clear if appbeing closed(clear after user open it).

这将帮助您清除应用程序打开时badge是否push来。当用户notification按下Home Button(一次或两次)时,用户看到并清除它。此外,它很清楚,如果appclosed(用户打开它后清除)。

Hereyou can see when this method called.

在这里你可以看到这个方法何时被调用。