简体   繁体   English

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

[英]iPhone: how to remove badge after Push Notification?

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! 当我收到推送时,我需要在单击按钮时将其删除!

objC : objC

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

swift : 迅捷

UIApplication.sharedApplication().applicationIconBadgeNumber = 0;

You can remove badge from push notifications by adding the following lines to your code 您可以通过在代码中添加以下行来从推送通知中删除徽章

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

As for iOS5, just setting badge number won't remove those push notification in the notification center. 对于iOS5,只设置徽章编号不会删除通知中心的推送通知。 You have to do this... 你必须这样做......

[[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方法也适用于通知中心的推送通知。

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)

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

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