Swift 本地推播通知UILocalNotification

來源:互聯網
上載者:User

標籤:控制   idf   nic   imei   dict   資源   obj   多資源   options   

Notification是智能手機應用開發中常用的資訊傳遞機制,它不用消耗更多資源去不停的檢查資訊狀態,可以非常好的節省資源。

在iOS中分為兩種通知:本地、遠程。本地的UILocalNotification由全域的NotificationManager統一管理,我們只需要將本地通知對象添加到系統的Notification隊列中就可以了,系統會在指定的時間激發本地通知。

本地推播通知:UILocalNotification
  1. 如果要使用推播通知,必須先在蘋果的推播通知服務裡註冊你要使用哪幾種類型的通知,就比如下面的一段代碼就表示同時註冊了提醒、標記和聲音兩種類型的通知(ios 8之前是不需要註冊的):
// 在appDelegate中註冊通知func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {        if #available(iOS 8.0, *) {            let uns = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)            UIApplication.sharedApplication().registerUserNotificationSettings(uns)        }}

2.建立並添加本地通知

class LocalNotificationUtils: NSObject {     /** 添加建立並添加本地通知 */    class func addNotification() {         // 初始化一個通知         let localNoti = UILocalNotification()        // 通知的觸發時間,例如即刻起15分鐘後         let fireDate = NSDate().dateByAddingTimeInterval(-15*60)          localNoti.fireDate = fireDate         // 設定時區         localNoti.timeZone = NSTimeZone.defaultTimeZone()         // 通知上顯示的主題內容         localNoti.alertBody = "通知上顯示的提示內容"         // 收到通知時播放的聲音,預設訊息聲音         localNoti.soundName = UILocalNotificationDefaultSoundName         //待機介面的滑動動作提示         localNoti.alertAction = "開啟應用"         // 應用程式圖示右上方顯示的訊息數         localNoti.applicationIconBadgeNumber = 0         // 通知上綁定的其他資訊,為索引值對         localNoti.userInfo = ["id": "1",  "name": "xxxx"]        // 添加通知到系統隊列中,系統會在指定的時間觸發                UIApplication.sharedApplication().scheduleLocalNotification(localNoti)    }}

3.擷取所有本地通知

let locals = UIApplication.sharedApplication().scheduledLocalNotifications

4.取消一個本地推送

    // 通過通知上綁定的id來取消通知,其中id也是你在userInfo中儲存的資訊    class func deleteNotification(id: String) {        if orderID.isEmpty {            return        }        if let locals = UIApplication.sharedApplication().scheduledLocalNotifications {            for localNoti in locals {                if let dict = localNoti.userInfo {                    if dict.keys.contains("id") && dict["id"] is String && (dict["id"] as! String) == id {                        // 取消通知                        UIApplication.sharedApplication().cancelLocalNotification(localNoti)                    }                }            }        }    }

5.取消所有本地通知

UIApplication.sharedApplication().cancelAllLocalNotifications()

6.點擊通知後的觸發事件

1.應用在正在運行(在前台或後台運行),點擊通知後觸發appDelegate代理方法::didReceiveLocalNotification

class AppDelegate: UIResponder, UIApplicationDelegate{    /** 接收本地通知 */    func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {        // 擷取通知上綁定的資訊        guard let dict = notification.userInfo else {            return        }        // 後面作相應處理...    }}

2.應用未運行,點擊通知啟動app,走appDelegate代理方法:didFinishLaunchingWithOptions

class AppDelegate: UIResponder, UIApplicationDelegate{    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {        // 省略建立window及根控制器等代碼        ......        // 此處只介紹當點擊通知啟動應用後如何擷取通知        if launchOptions != nil {            if let localNotification = launchOptions!["UIApplicationLaunchOptionsLocalNotificationKey"] as? UILocalNotification {                if let dict = localNotification.userInfo {                        // 擷取通知上綁定的資訊後作相應處理...                }            }        }        return true   }}

參考:http://www.cnblogs.com/kenshincui/p/4168532.html#localNotification

Swift 本地推播通知UILocalNotification

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.