本地推送通知是經過實例化UILocalNotification實現的。要實現本地化推送能夠在AppDelegate.swift中添加代碼實現,本事例是一個當App進入後臺時推送一條消息給用戶。swift
1.首先在didFinishLaunchingWithOptions方法內添加代碼,IOS8推送消息首先要得到用戶的贊成,在初次安裝App時會提示用戶是否容許程序推送消息,此方法是App第一次運行的時候被執行一次,每次從後臺激活時不執行該方法.app
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { if (UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8 { //APService.registerForRemoteNotificationTypes( //UIUserNotificationType.Badge.rawValue | //UIUserNotificationType.Sound.rawValue | //UIUserNotificationType.Alert.rawValue, //categories: nil) application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil)) } //APService.setupWithOption(launchOptions) return true }
2.有幾個方法要說一下,ide
1.func applicationWillResignActive(application: UIApplication){} 當App既將進入後臺、鎖屏、有電話進來時會觸發此事件ui
2.func applicationDidEnterBackground(application: UIApplication) {} 當App進入後臺時觸發此事件this
3.func applicationWillEnterForeground(application: UIApplication) {} 當App從後臺即將回到前臺時觸發此事件rest
4.func applicationDidBecomeActive(application: UIApplication) {}當App變成活動狀態時觸發此事件orm
5.func applicationWillTerminate(application: UIApplication) {} 當App退出時觸發此方法,通常用於保存某些特定的數據blog
此時在applicationDidEnterBackground方法內寫入如下代碼:事件
func applicationDidEnterBackground(application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. UIApplication.sharedApplication().cancelAllLocalNotifications() var notification = UILocalNotification() //notification.fireDate = NSDate().dateByAddingTimeInterval(1) //setting timeZone as localTimeZone notification.timeZone = NSTimeZone.localTimeZone() notification.repeatInterval = NSCalendarUnit.CalendarUnitDay notification.alertTitle = "This is a local notification" notification.alertBody = "Hey,It's great to see you again" notification.alertAction = "OK" notification.soundName = UILocalNotificationDefaultSoundName //setting app's icon badge notification.applicationIconBadgeNumber = 1 var userInfo:[NSObject : AnyObject] = [NSObject : AnyObject]() userInfo["kLocalNotificationID"] = "LocalNotificationID" userInfo["key"] = "Attention Please" notification.userInfo = userInfo //UIApplication.sharedApplication().scheduleLocalNotification(notification) //UIApplication.sharedApplication().presentLocalNotificationNow(notification) application.presentLocalNotificationNow(notification) }
此時將按Home鍵將App切換到後臺時會有一條推送消息,App角標變爲了「1」it
3.當用戶點擊消息時會觸發didReceiveLocalNotification事件,在這個事件內寫些代碼:
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { UIApplication.sharedApplication().cancelAllLocalNotifications() let userInfo = notification.userInfo! let title = userInfo["key"] as! String var alert = UIAlertView() alert.title = title alert.message = notification.alertBody alert.addButtonWithTitle(notification.alertAction!) alert.cancelButtonIndex = 0 alert.show() //APService.showLocalNotificationAtFront(notification, identifierKey: nil) }
4.當程序處於活動狀態的時候清除ICON的角標
func applicationDidBecomeActive(application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. //setting the desk top application icon's badge as zero //application.applicationIconBadgeNumber = 0 application.cancelAllLocalNotifications() application.applicationIconBadgeNumber = 0 }