一. 推送通知的介紹
-
什么是推送通知
- 首先要明確一點(diǎn), 推送通知與通知Notification和通知中心等一點(diǎn)關(guān)系都沒有, 完全就不是一碼事
- 推送通知的表現(xiàn): 就是想用戶推送一條消息, 告知用戶App的一些情況
- 當(dāng)你的App在后臺/關(guān)閉的時候, 可以通過推送通知, 來通知用戶一些與此App有關(guān)的信息
- 通知的應(yīng)用場景就不詳細(xì)解釋了, 比如QQ/微信接收到新消息, 這都是推送通知
-
推送通知的分類
-
本地推送通知
- 本地通知主要是可以在不聯(lián)網(wǎng)的情況下發(fā)送給用戶
- 一般是在固定的時間點(diǎn), 推送給用戶
-
遠(yuǎn)程推送通知
- 與本地相比, 遠(yuǎn)程推送通知必須在聯(lián)網(wǎng)的情況下才能接收到
- 遠(yuǎn)程推送服務(wù): 又稱APNs(Apple Push Notification Service), 即蘋果推送通知服務(wù), 記好這個名稱的概念, 他是由蘋果推送給用戶的
- 一般的應(yīng)用場景表現(xiàn)為, 當(dāng)App徹底退出了, 或者在不確定的時間, 由App官方的服務(wù)器向讓用戶知曉一些情況, 就需要使用遠(yuǎn)程推送通知
使用原則:
誰能確定時間和內(nèi)容, 誰就可以發(fā)送通知(即開發(fā)人員可以通過App的內(nèi)部, 以代碼的形式發(fā)送通知=本地推送通知; 開發(fā)人員無法確定, 由服務(wù)器確定并提供的信息=遠(yuǎn)程推送通知)
-
-
通知的展示樣式
- 在屏幕頂部彈出的一個橫條(一般會顯示通知的標(biāo)題, 在通知中心可以查看具體的內(nèi)容)
- 在屏幕中間彈出一個UIAlertView(顯示通知的具體內(nèi)容)
- 在鎖屏界面出現(xiàn)一塊橫幅(也會顯示具體內(nèi)容)
- 更新App的圖標(biāo)數(shù)字(告知新內(nèi)容的數(shù)量)
- 播放音效, 提醒用戶
- 注: 我們雖然可以設(shè)定通知的內(nèi)容, 但是樣式是由用戶在設(shè)置->通知中心自己決定的
二.本地推送通知的一些基本使用
- 基本用法
首先, 創(chuàng)建本地推送通知的主要類為: UILocalNotification
let notification = UILocalNotification()
-
為本地通知設(shè)置兩個必要的屬性:
- 推送通知的觸發(fā)時間:
notification.fireDate
- 推送通知的具體內(nèi)容:
notification.alertBody
- 推送通知的觸發(fā)時間:
-
本地通知的一些可選屬性:
- 通知的聲音:
notification.soundName
- App圖標(biāo)右上側(cè)的數(shù)字提示:
notification.applicationIconBadgeNumber
- 通知的圖片(但經(jīng)過測試iOS9.0之后不能用):
notificcation.alertLaunchImage
- 通知的重復(fù)間隔:
notification.repeatInterval
, 最小為1分鐘 - 其他的基本是一些不常用的了, 大家自行發(fā)掘吧
- 通知的聲音:
-
推送通知:
- 立即推送通知:
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
, 但是通知一般都是為了App在后臺的時候通知用戶, 所以基本不會用這個方法 - 根據(jù)觸發(fā)時間, 推送通知:
UIApplication.sharedApplication().scheduleLocalNotification(notification)
- 立即推送通知:
-
取消調(diào)度本地推送通知
- 取消單個通知:
UIApplication.sharedApplication().cancelLocalNotification(notification)
- 取消所有的通知:
UIApplication.sharedApplication().cancelAllLocalNotifications()
- 取消單個通知:
獲取目前所有準(zhǔn)備調(diào)度的通知:
UIApplication.sharedApplication().scheduledLocalNotifications
-
在iOS8.0之后, 如果要使用通知功能, 必須讓用戶授權(quán), 才可以使用, 通常我們在AppDelegate中, 當(dāng)程序進(jìn)入前臺時就請求授權(quán)
extension AppDelegate { func localNotificationAuthority() { if #available(iOS 8.0, *) { // 1. 用通知的類型, 組合一個位移枚舉 let typeValue = UIUserNotificationType.Alert.rawValue | UIUserNotificationType.Badge.rawValue | UIUserNotificationType.Sound.rawValue // 1.2 使用唯一枚舉的真實(shí)值, 創(chuàng)建一個通知類型對象 let type = UIUserNotificationType(rawValue: typeValue) // 2. 使用通知類型對象, 創(chuàng)建一個注冊設(shè)置對象 let setting = UIUserNotificationSettings(forTypes: type, categories: nil) // 3. 根據(jù)設(shè)置對象, 注冊請求通知的權(quán)限 UIApplication.sharedApplication().registerUserNotificationSettings(setting) } } }
-
簡單的代碼演示:
class ViewController: UIViewController { // 懶加載位置管理者 lazy var locationManager : CLLocationManager = { let locationManager = CLLocationManager() if #available(iOS 8.0, *) { locationManager.requestAlwaysAuthorization() } return locationManager }() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } @IBAction func sendLocalNotification(sender: AnyObject) { let notification = UILocalNotification() notification.fireDate = NSDate(timeIntervalSinceNow: 5) notification.alertBody = "本地通知~" // 設(shè)置時間對應(yīng)的時區(qū) notification.timeZone = NSTimeZone.defaultTimeZone() // 設(shè)置重復(fù)間隔(最少一分鐘) // notification.repeatInterval = .Minute // 設(shè)置監(jiān)聽區(qū)域 // 該功能只有在iOS8.0之后才能使用 // if #available(iOS 8.0, *) { // let circleRegion = CLCircularRegion(center: CLLocationCoordinate2DMake(21.123, 123.234), radius: 1000, identifier: "監(jiān)聽區(qū)域") // // 1. 獲取位置信息 // locationManager.startMonitoringForRegion(circleRegion) // // 2. 設(shè)置觸發(fā)通知的區(qū)域 // notification.region = circleRegion // // 3. 設(shè)置區(qū)域的觸發(fā)次數(shù), 如果為false代表不限次數(shù) // notification.regionTriggersOnce = false // } // 設(shè)置通知在滑屏解鎖界面, 左滑時, 右側(cè)顯示的內(nèi)容 notification.hasAction = true notification.alertAction = "回復(fù)" // 通知的啟動圖片 // 如果找不到圖片的話, 會使用系統(tǒng)默認(rèn)的圖片 // iOS9.0之后, 經(jīng)過試驗(yàn), 不能使用了, 不會顯示圖片 notification.alertLaunchImage = "2.jpg" // 通知的聲音 notification.soundName = "win.aac" // App圖標(biāo)右上角的文字 notification.applicationIconBadgeNumber = 10 // 設(shè)置通知的標(biāo)題, 作用在通知中心 if #available(iOS 8.2, *) { notification.alertTitle = "新短信" } // 設(shè)置通知的附加信息 // notification.userInfo // 發(fā)送本地通知 UIApplication.sharedApplication().scheduleLocalNotification(notification) // UIApplication.sharedApplication().presentLocalNotificationNow(notification) } @IBAction func cancelLocalNotification(sender: AnyObject) { // UIApplication.sharedApplication().cancelLocalNotification(notification) UIApplication.sharedApplication().cancelAllLocalNotifications() } @IBAction func checkLocalNotification(sender: AnyObject) { let notification = UIApplication.sharedApplication().scheduledLocalNotifications print(notification) } }