Removing badge from iOS app icon

时间秒杀一切 提交于 2019-12-17 15:39:31

问题


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:

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

How can I clear the application icon badge?


回答1:


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

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

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




回答2:


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.




回答3:


In Swift and In AppDelegate

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



回答4:


You can use this codes also.

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

or In a specific ViewController

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



回答5:


Maybe call it in applicationWillResignActive (in 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. User seeing notification and you clear it, when he press Home Button (once or twice). Also it will be clear if app being closed (clear after user open it).

Here you can see when this method called.



来源:https://stackoverflow.com/questions/9557132/removing-badge-from-ios-app-icon

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!