天天看點

Swift - 本地消息的推送通知(附樣例)

使用UILocalNotification可以很友善的實作消息的推送功能。我們可以設定這個消息的推送時間,推送内容等。

當推送時間一到,不管使用者在桌面還是其他應用中,螢幕上方會都顯示出推送消息。

1,推送消息的發送

 ​​

Swift - 本地消息的推送通知(附樣例)
​​  ​​
Swift - 本地消息的推送通知(附樣例)

​​

--- AppDelegate.swift ---

​import​

​ ​

​UIKit​

​@UIApplicationMain​

​class​

​AppDelegate​

​: ​

​UIResponder​

​, ​

​UIApplicationDelegate​

​{​

​var​

​window: ​

​UIWindow​

​?​

​func​

​application(application: ​

​UIApplication​

​,​

​didFinishLaunchingWithOptions launchOptions: [​

​NSObject​

​: ​

​AnyObject​

​]?) -> ​

​Bool​

​{​

​//開啟通知​

​let​

​settings = ​

​UIUserNotificationSettings​

​(forTypes: [.​

​Alert​

​, .​

​Badge​

​, .​

​Sound​

​],​

​categories: ​

​nil​

​)​

​application.registerUserNotificationSettings(settings)​

​return​

​true​

​}​

​func​

​applicationWillResignActive(application: ​

​UIApplication​

​) {​

​}​

​func​

​applicationDidEnterBackground(application: ​

​UIApplication​

​) {​

​}​

​func​

​applicationWillEnterForeground(application: ​

​UIApplication​

​) {​

​}​

​func​

​applicationDidBecomeActive(application: ​

​UIApplication​

​) {​

​}​

​func​

​applicationWillTerminate(application: ​

​UIApplication​

​) {​

​}​

​}​

--- ViewController.swift ---

​import​

​UIKit​

​class​

​ViewController​

​: ​

​UIViewController​

​{​

​override​

​func​

​viewDidLoad() {​

​super​

​.viewDidLoad()​

​//發送通知消息​

​scheduleNotification(12345);​

​//清除所有本地推送​

​//UIApplication.sharedApplication().cancelAllLocalNotifications()​

​}​

​//發送通知消息​

​func​

​scheduleNotification(itemID:​

​Int​

​){​

​//如果已存在該通知消息,則先取消​

​cancelNotification(itemID)​

​//建立UILocalNotification來進行本地消息通知​

​let​

​localNotification = ​

​UILocalNotification​

​()​

​//推送時間(設定為30秒以後)​

​localNotification.fireDate = ​

​NSDate​

​(timeIntervalSinceNow: 30)​

​//時區​

​localNotification.timeZone = ​

​NSTimeZone​

​.defaultTimeZone()​

​//推送内容​

​localNotification.alertBody = ​

​"來自hangge.com的本地消息"​

​//聲音​

​localNotification.soundName = ​

​UILocalNotificationDefaultSoundName​

​//額外資訊​

​localNotification.userInfo = [​

​"ItemID"​

​:itemID]​

​UIApplication​

​.sharedApplication().scheduleLocalNotification(localNotification)​

​}​

​//取消通知消息​

​func​

​cancelNotification(itemID:​

​Int​

​){​

​//通過itemID擷取已有的消息推送,然後删除掉,以便重新判斷​

​let​

​existingNotification = ​

​self​

​.notificationForThisItem(itemID) ​

​as​

​UILocalNotification​

​?​

​if​

​existingNotification != ​

​nil​

​{​

​//如果existingNotification不為nil,就取消消息推送​

​UIApplication​

​.sharedApplication().cancelLocalNotification(existingNotification!)​

​}​

​}​

​//通過周遊所有消息推送,通過itemid的對比,傳回UIlocalNotification​

​func​

​notificationForThisItem(itemID:​

​Int​

​)-> ​

​UILocalNotification​

​? {​

​let​

​allNotifications = ​

​UIApplication​

​.sharedApplication().scheduledLocalNotifications​

​for​

​notification ​

​in​

​allNotifications! {​

​let​

​info = notification.userInfo ​

​as​

​! [​

​String​

​:​

​Int​

​]​

​let​

​number = info[​

​"ItemID"​

​]​

​if​

​number != ​

​nil​

​&& number == itemID {​

​return​

​notification ​

​as​

​UILocalNotification​

​}​

​}​

​return​

​nil​

​}​

​override​

​func​

​didReceiveMemoryWarning() {​

​super​

​.didReceiveMemoryWarning()​

​}​

​}​

2,點選推送消息的響應

收到推送,如果點選推送内容,則會重新進入到App,這個時候會調用AppDelegate中的func application(application: UIApplication, didReceiveLocalNotification

notification: UILocalNotification)代理方法。

在這個方法中我們可以根據推送的消息内容實作相關的功能。

Swift - 本地消息的推送通知(附樣例)

​func​

​application(application: ​

​UIApplication​

​,​

​didReceiveLocalNotification notification: ​

​UILocalNotification​

​) {​

​//設定Badge數目​

​UIApplication​

​.sharedApplication().applicationIconBadgeNumber = 0​

​let​

​info = notification.userInfo ​

​as​

​! [​

​String​

​:​

​Int​

​]​

​let​

​number = info[​

​"ItemID"​

​]​

​let​

​alertController = ​

​UIAlertController​

​(title: ​

​"本地通知"​

​,​

​message: ​

​"消息内容:\(notification.alertBody)使用者資料:\(number)"​

​,​

​preferredStyle: ​

​UIAlertControllerStyle​

​.​

​Alert​

​)​

          let cancel = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil);

                   alertController.addAction(cancel);

​self​

​.window?.rootViewController!.presentViewController(alertController,​

​animated: ​

​true​

​, completion: ​

​nil​

​)​

​}​