iOS 10 以下系統(tǒng):
1. 添加通知:
幾種觸發(fā)方式:
UNPushNotificationTrigger 通過 APNs 服務(wù)發(fā)來的推送自動(dòng)創(chuàng)建
UNTimeIntervalNotificationTrigger 定時(shí)觸發(fā)
UNCalendarNotificationTrigger 定期觸發(fā)
UNLocationNotificationTrigger 定位觸發(fā)
// 設(shè)置每一周的某一天彈出通知
let notification = UILocalNotification()
notification.alertBody = R.string.localize.heyItSTimeToWorkout()
notification.timeZone = NSTimeZone.default
let calendar = NSCalendar.autoupdatingCurrent
var components = calendar.dateComponents([ .year, .weekOfYear], from: Date())
components.hour = localNotificationModel.hour
components.minute = localNotificationModel.minute
components.weekday = localNotificationModel.weekday
notification.repeatInterval = .weekOfYear
let date = NSCalendar.current.date(from: components)
print("localNotificationModel.weekday___\(localNotificationModel.weekday)____fireDate________\(String(describing: date))")
notification.fireDate = date
notification.userInfo = ["identifier": localNotificationModel.recordID]
UIApplication.shared.scheduleLocalNotification(notification)
2. 移除通知:
guard let notifications = UIApplication.shared.scheduledLocalNotifications else {
return
}
for notification in notifications {
guard let userInfo = notification.userInfo else {
continue
}
if (userInfo["identifier"] as? String ) ?? "" == identifer {
UIApplication.shared.cancelLocalNotification(notification)
break
}
}
3. 移除全部還未觸發(fā)的通知:
UIApplication.shared.cancelAllLocalNotifications()
iOS 10以上版本系統(tǒng)
1. 添加通知:
if #available(iOS 10.0, *) {
var components = DateComponents()
components.hour = localNotificationModel.hour
components.minute = localNotificationModel.minute
components.weekday = localNotificationModel.weekday
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
let content = UNMutableNotificationContent()
content.body = R.string.localize.heyItSTimeToWorkout()
let request = UNNotificationRequest(identifier: localNotificationModel.recordID, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
print("error==\(error.debugDescription)" )
}
}
2. 移除通知:
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [identifer])
}
3. 移除全部還未觸發(fā)的通知:
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
需要注意的問題:
1. 通知的添加一定要設(shè)置 alertBody,不然通知無法顯示
2. 在iOS 10中,用同一個(gè) identifier 添加多次通知,這時(shí)候永遠(yuǎn)是最后一個(gè)有效淡喜,也就是說后面添加的會(huì)覆蓋前面添加的; 但是 在 iOS 10以下沉删,同一個(gè) identifier 添加多次通知讥珍,只要 Notification觸發(fā)的時(shí)間設(shè)置的不一樣徙融,后面添加的就不會(huì)覆蓋前面添加的毯侦,所以會(huì)添加多個(gè)通知哭靖。這是一個(gè)坑.
3.一個(gè)坑點(diǎn): APP可以刪除,但是設(shè)置的通知并不會(huì)被刪除
APP設(shè)置通知之后侈离,如果你沒有把通知移除掉试幽,這時(shí)候你把APP刪除,重新安裝之后卦碾,你之前設(shè)置的通知依舊會(huì)出現(xiàn)在新安裝的APP之中铺坞,系統(tǒng)保留了這些通知的信息。
調(diào)試:
//獲取已添加的通知隊(duì)列中洲胖,待觸發(fā)的通知信息
func getPendingNotificationRequests(){
#if DEBUG
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationReqeusts) in
print("notificationReqeusts.count____\(notificationReqeusts.count)")
for request in notificationReqeusts {
let trigger = request.trigger as! UNCalendarNotificationTrigger
let nextTriggerDate = trigger.nextTriggerDate()
print("nextTriggerDate____\(String(describing: nextTriggerDate))")
}
}
} else {
let notifications = UIApplication.shared.scheduledLocalNotifications
print("notifications.count_______\(String(describing: notifications?.count))")
for notification in notifications! {
print("nextTriggerDate______\(notification.fireDate)")
}
}
#endif
}
我 18:49 添加了7個(gè)通知打印輸出如下:
nextTriggerDate____2017-05-21 10:49:00 +0000
nextTriggerDate____2017-05-22 10:49:00 +0000
nextTriggerDate____2017-05-16 10:49:00 +0000
nextTriggerDate____2017-05-17 10:49:00 +0000
nextTriggerDate____2017-05-18 10:49:00 +0000
nextTriggerDate____2017-05-19 10:49:00 +0000
nextTriggerDate____2017-05-20 10:49:00 +0000
發(fā)現(xiàn)輸出的通知觸發(fā)時(shí)間和我添加通知的時(shí)間剛好相差8h康震,這是時(shí)區(qū)的問題,那么應(yīng)該如何解決這個(gè)問題呢宾濒?答案是不需要處理,經(jīng)過驗(yàn)證發(fā)現(xiàn)屏箍,通知觸發(fā)時(shí)間依然會(huì)是你添加通知的時(shí)間绘梦,而不是你打印的通知信息的那個(gè)時(shí)間橘忱,原因估計(jì)是系統(tǒng)做過處理了
其他在查資料的過程中的一些零碎有用信息:
更新和取消通知
1.取消已展示的推送
[notificationCenter removeDeliveredNotificationsWithIdentifiers:@[requestIdentifier]];
2.取消未展示的推送
[notificationCenter removePendingNotificationRequestsWithIdentifiers:@[requestIdentifier]];
3.更新
// 無論顯示還是未顯示的通知想要更新只需要新建一個(gè) UNNotificationRequest 將 requestIdentifier 設(shè)置成想替換的通知標(biāo)識(shí)就可以完成替換
DeviceToken變化
DeviceToken不僅僅系統(tǒng)升級(jí)的時(shí)候會(huì)改變,系統(tǒng)重刷卸奉,應(yīng)用刪除再安裝钝诚,DeviceToken都會(huì)改變