Swift - 推送之本地推送(UILocalNotification)

时间:2023-03-08 17:46:10
Swift - 推送之本地推送(UILocalNotification)

// 本地推送通知是通过实例化UILocalNotification实现的。要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户。

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

// 首先在didFinishLaunchingWithOptions方法内添加代码,IOS8推送消息首先要获得用户的同意,在初次安装App时会提示用户是否允许程序推送消息,此方法是App第一次运行的时候被执行一次,每次从后台激活时不执行该方法.

if (UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8 {

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil))

}

return true

}

// 当App既将进入后台、锁屏、有电话进来时会触发此事件

func applicationWillResignActive(application: UIApplication) {

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

// 当App进入后台时触发此事件,此时在applicationDidEnterBackground方法内写入以下代码:

// 此时将按Home键将App切换到后台时会有一条推送消息,App角标变为了“1”

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()

let notification = UILocalNotification()

//notification.fireDate = NSDate().dateByAddingTimeInterval(1)

//setting timeZone as localTimeZone

notification.timeZone = NSTimeZone.localTimeZone()

notification.repeatInterval = NSCalendarUnit.Day

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)

}

// 当用户点击消息时会触发didReceiveLocalNotification事件,在这个事件内写些代码:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {

UIApplication.sharedApplication().cancelAllLocalNotifications()

let userInfo = notification.userInfo!

let title = userInfo["key"] as! String

let alert = UIAlertView()

alert.title = title

alert.message = notification.alertBody

alert.addButtonWithTitle(notification.alertAction!)

alert.cancelButtonIndex = 0

alert.show()

}

//  当App从后台即将回到前台时触发此事件

func applicationWillEnterForeground(application: UIApplication) {

// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

// 当App变成活动状态时触发此事件

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.

// 当程序处于活动状态的时候清除ICON的角标

application.cancelAllLocalNotifications()

application.applicationIconBadgeNumber = 0

}

// 当App退出时触发此方法,一般用于保存某些特定的数据

func applicationWillTerminate(application: UIApplication) {

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}