简体   繁体   English

从iOS应用图标删除徽章

[英]Removing badge from iOS app icon

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 didFinishLaunchingWithOptions I try to set badge to 0 in this way: application didFinishLaunchingWithOptions我尝试通过以下方式将badge设置为0:

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

How can I clear the application icon badge? 如何清除应用程序图标标志?

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:

Likely, -application:didFinishLaunchingWithOptions: is not being called, because your app is still running in the background. 可能未调用-application:didFinishLaunchingWithOptions: ,因为您的应用程序仍在后台运行。 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. 为了从后台启动应用程序时删除徽章计数,您还必须在-applicationWillEnterForeground:重置徽章编号。

In Swift and In AppDelegate 在Swift和AppDelegate中

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

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;
}

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

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

This will help you to clear badge if push come when app is being open. 如果在打开应用程序时push这将帮助您清除badge User seeing notification and you clear it, when he press Home Button (once or twice). 当用户按下notification Home Button (一次或两次)时,您会看到notification并且您将其清除。 Also it will be clear if app being closed (clear after user open it). 此外,还会清除app是否closed (用户打开后清除)。

Here you can see when this method called. 在这里您可以看到何时调用此方法。

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

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