我们都知道这个方法 [UIApplication sharedApplication].applicationIconBadgeNumber = 0; 可以从通知中心删除我们应用程序的所有远程通知 . 但是,出于某种原因,我想删除用户点击通知中心的那个,并保留其他通知中心 .
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
有没有办法做到这一点?
如果您只是想从徽章编号中删除一个号码:
[UIApplication sharedApplication].applicationIconBadgeNumber = MAX([UIApplication sharedApplication].applicationIconBadgeNumber - 1, 0);
如果您询问如何以编程方式从通知中心删除单个通知,则无法在代码中完成 . 显然在iOS8中,当用户点击它时,操作系统将删除单个通知 . 否则你不可能处理 .
见:https://stackoverflow.com/a/10569847/620577
通过为iOS 10及更高版本引入 UNUserNotificationCenter ,现在可以删除应用程序的几个或所有远程通知 .
UNUserNotificationCenter
UNUserNotificationCenter文档
使用此类的共享单例实例,可以在设备上管理传递的远程通知 . 具体来说,可以使用以下方法: func removeDeliveredNotifications(withIdentifiers: [String]) 如果要删除应用程序的特定通知,或 func removeAllDeliveredNotifications() 删除应用程序的所有通知 .
func removeDeliveredNotifications(withIdentifiers: [String])
func removeAllDeliveredNotifications()
2 回答
如果您只是想从徽章编号中删除一个号码:
[UIApplication sharedApplication].applicationIconBadgeNumber = MAX([UIApplication sharedApplication].applicationIconBadgeNumber - 1, 0);
如果您询问如何以编程方式从通知中心删除单个通知,则无法在代码中完成 . 显然在iOS8中,当用户点击它时,操作系统将删除单个通知 . 否则你不可能处理 .
见:https://stackoverflow.com/a/10569847/620577
通过为iOS 10及更高版本引入
UNUserNotificationCenter
,现在可以删除应用程序的几个或所有远程通知 .使用此类的共享单例实例,可以在设备上管理传递的远程通知 . 具体来说,可以使用以下方法:
func removeDeliveredNotifications(withIdentifiers: [String])
如果要删除应用程序的特定通知,或func removeAllDeliveredNotifications()
删除应用程序的所有通知 .