iPhone:推送通知后如何删除徽章?

时间:2022-06-01 20:38:37

What is the code to remove the badge on my app's icon? When I receive push, I need to remove it when a button is clicked!

在我的应用程序图标上移除标记的代码是什么?当我收到push时,我需要在点击按钮时移除它!

4 个解决方案

#1


92  

objC :

objC:

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

swift :

迅速:

UIApplication.sharedApplication().applicationIconBadgeNumber = 0;

#2


9  

You can remove badge from push notifications by adding the following lines to your code

通过在代码中添加以下代码,可以从推送通知中删除badge

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

#3


7  

As for iOS5, just setting badge number won't remove those push notification in the notification center. You have to do this...

对于iOS5,设置badge编号不会删除通知中心的推送通知。你必须这么做……

[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

I already tested this. It looks like cancelAllLocalNotifications method also works with push notifications in notification center as well.

我已经测试了这个。看起来cancelAllLocalNotifications方法也可以在通知中心中使用推送通知。

#4


1  

Swift 3

斯威夫特3

UIApplication.shared.applicationIconBadgeNumber = 0

Can be added to following methods:

可以添加到以下方法:

optional public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool

and

optional public func applicationDidBecomeActive(_ application: UIApplication)

#1


92  

objC :

objC:

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

swift :

迅速:

UIApplication.sharedApplication().applicationIconBadgeNumber = 0;

#2


9  

You can remove badge from push notifications by adding the following lines to your code

通过在代码中添加以下代码,可以从推送通知中删除badge

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

#3


7  

As for iOS5, just setting badge number won't remove those push notification in the notification center. You have to do this...

对于iOS5,设置badge编号不会删除通知中心的推送通知。你必须这么做……

[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

I already tested this. It looks like cancelAllLocalNotifications method also works with push notifications in notification center as well.

我已经测试了这个。看起来cancelAllLocalNotifications方法也可以在通知中心中使用推送通知。

#4


1  

Swift 3

斯威夫特3

UIApplication.shared.applicationIconBadgeNumber = 0

Can be added to following methods:

可以添加到以下方法:

optional public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool

and

optional public func applicationDidBecomeActive(_ application: UIApplication)