3 回答
TA貢獻2003條經驗 獲得超2個贊
很可能是因為Notification Center是一個相對較新的功能,Apple并不一定要推動全新的范例來清除通知。因此,他們有多種用途[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];來清除上述通知。這似乎有點怪異,Apple將來可能會提供一種更直觀的方式來執行此操作,但目前這是官方方式。
我自己,使用以下代碼段:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
永遠不會從通知中心清除該應用程序的所有通知。
TA貢獻1848條經驗 獲得超10個贊
iOS 10更新(Swift 3)
為了清除iOS 10應用中的所有本地通知,您應該使用以下代碼:
import UserNotifications
...
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests() // To remove all pending notifications which are not delivered yet but scheduled.
center.removeAllDeliveredNotifications() // To remove all delivered notifications
} else {
UIApplication.shared.cancelAllLocalNotifications()
}
此代碼處理針對iOS 10.x和所有先前版本的iOS的本地通知的清除。您將需要import UserNotificationsiOS 10.x代碼。
- 3 回答
- 0 關注
- 1053 瀏覽
添加回答
舉報
