iOS LocalNotification 相關(guān)

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ì)改變

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市榄棵,隨后出現(xiàn)的幾起案子凝颇,更是在濱河造成了極大的恐慌,老刑警劉巖疹鳄,帶你破解...
    沈念sama閱讀 216,843評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拧略,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡瘪弓,警方通過查閱死者的電腦和手機(jī)垫蛆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,538評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來腺怯,“玉大人袱饭,你說我怎么就攤上這事∏赫迹” “怎么了虑乖?”我有些...
    開封第一講書人閱讀 163,187評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)晾虑。 經(jīng)常有香客問我疹味,道長(zhǎng),這世上最難降的妖魔是什么走贪? 我笑而不...
    開封第一講書人閱讀 58,264評(píng)論 1 292
  • 正文 為了忘掉前任佛猛,我火速辦了婚禮,結(jié)果婚禮上坠狡,老公的妹妹穿的比我還像新娘继找。我一直安慰自己,他們只是感情好逃沿,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,289評(píng)論 6 390
  • 文/花漫 我一把揭開白布婴渡。 她就那樣靜靜地躺著,像睡著了一般凯亮。 火紅的嫁衣襯著肌膚如雪边臼。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,231評(píng)論 1 299
  • 那天假消,我揣著相機(jī)與錄音柠并,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛臼予,可吹牛的內(nèi)容都是我干的鸣戴。 我是一名探鬼主播,決...
    沈念sama閱讀 40,116評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼粘拾,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼窄锅!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起缰雇,我...
    開封第一講書人閱讀 38,945評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤入偷,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后械哟,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體疏之,經(jīng)...
    沈念sama閱讀 45,367評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,581評(píng)論 2 333
  • 正文 我和宋清朗相戀三年戒良,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了体捏。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,754評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡糯崎,死狀恐怖几缭,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情沃呢,我是刑警寧澤年栓,帶...
    沈念sama閱讀 35,458評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站薄霜,受9級(jí)特大地震影響某抓,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜惰瓜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,068評(píng)論 3 327
  • 文/蒙蒙 一否副、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧崎坊,春花似錦备禀、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,692評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至男翰,卻和暖如春另患,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蛾绎。 一陣腳步聲響...
    開封第一講書人閱讀 32,842評(píng)論 1 269
  • 我被黑心中介騙來泰國打工昆箕, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留鸦列,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,797評(píng)論 2 369
  • 正文 我出身青樓为严,卻偏偏與公主長(zhǎng)得像敛熬,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子第股,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,654評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容