本地通知

這里說的通知不是我們平常說的NSNotificationCenter,而是一種推送信息.
本地推送是在不需要聯(lián)網(wǎng)的情況下發(fā)出的推送通知
經(jīng)常用來定時(shí)提醒用戶,比如說鬧鐘.
下面看一下如何使用:
有三步:

  1. 創(chuàng)建本地通知對(duì)象
  2. 設(shè)置通知內(nèi)容
  3. 調(diào)用通知
  override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        // 1. 創(chuàng)建本地通知
        let localNote = UILocalNotification()
        
        // 2. 內(nèi)容
        // 發(fā)出通知的時(shí)間
        localNote.fireDate = NSDate(timeIntervalSinceNow: 3)
        localNote.alertBody = "這是一條本地通知"

        // 重復(fù)的頻率. 枚舉值
        localNote.repeatInterval = .Day
        
        localNote.alertAction = "呵呵"
        // 可以隨便寫,都會(huì)調(diào)用LaunchImage圖片
        localNote.alertLaunchImage = "haha"
        localNote.alertTitle = "通知"
        // 通知聲音 系統(tǒng)的字符串
        localNote.soundName = UILocalNotificationDefaultSoundName
        
        // 設(shè)置
        localNote.applicationIconBadgeNumber = 10
        
        // 其他信息,這個(gè)可以在代理方法中獲取
        localNote.userInfo = ["note" : "haha"]

        // 3. 調(diào)用    UIApplication.sharedApplication().scheduleLocalNotification(localNote)
    }

注意
在iOS8以上,需要獲取權(quán)限,在func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool中設(shè)置

 if Double(UIDevice.currentDevice().systemVersion) >= 8.0 {
          // Types可以設(shè)置的內(nèi)容是個(gè)枚舉值
            let setting = UIUserNotificationSettings(forTypes: [.Badge, .Alert, .Sound], categories: nil)
            application.registerUserNotificationSettings(setting)
            
   }

添加上面代碼,在第一次啟動(dòng)應(yīng)用時(shí)會(huì)進(jìn)行選擇.


如果我們需要在點(diǎn)擊通知后做某些事情的話,需要在下面的代理方法里面寫

    // 當(dāng)程序進(jìn)入前臺(tái)或在前臺(tái)時(shí),執(zhí)行該方法
    func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
        debugPrint("接收到通知")
        // 打印的是上面說的 localNote.userInfo
        debugPrint(notification.userInfo)
        // 不是處于前臺(tái)
        if application.applicationState == .Inactive {
            debugPrint("進(jìn)行一些事件處理")
        }
    }

但是如果退出了應(yīng)用,就不會(huì)執(zhí)行上面的代理方法,這時(shí)點(diǎn)擊通知依然會(huì)啟動(dòng)程序,這種情況需要在 didFinishLaunchingWithOptions 方法中獲取應(yīng)用啟動(dòng)方式,如果是

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        if Double(UIDevice.currentDevice().systemVersion) >= 8.0 {
            
            let setting = UIUserNotificationSettings(forTypes: [.Badge, .Alert, .Sound], categories: nil)
            application.registerUserNotificationSettings(setting)
            
        }
        // 各種打開apple的方式,key是字符串常量,如果正常點(diǎn)擊圖標(biāo)啟動(dòng)的話launchOptions是一個(gè)空值nil
// 這里是判斷是否是通過本地通知打開
        if ((launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey]) != nil) {
            debugPrint("通過本地通知進(jìn)入,處理事件") // 打印無效果
/*
            // 由于重新啟動(dòng)了程序無法驗(yàn)證打印的信息,我們可以通過添加控件進(jìn)行驗(yàn)證
            let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
            view.backgroundColor = UIColor.redColor()
            window!.rootViewController?.view.addSubview(view)
        }
*/  
        
        return true
    }```
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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