前言
通過(guò)這篇文章你會(huì)學(xué)到:
- 弄出本地通知
- 弄出有選項(xiàng)操作的通知
- 類似微信的快速回復(fù)
本地通知
我寫成了函數(shù)的形式方便設(shè)置
func setNotification(body:String,timeToNotification:Double,soundName:String,category:String) -> UILocalNotification {
let localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "滑動(dòng)查看信息"
localNotification.alertBody = body
//在mainBundle的短于30秒的播放文件,否則就會(huì)是系統(tǒng)默認(rèn)聲音
localNotification.soundName = soundName
localNotification.fireDate = NSDate(timeIntervalSinceNow: timeToNotification)
//下面這條category語(yǔ)句是對(duì)應(yīng)下面的“有選項(xiàng)的本地操作”章節(jié)
localNotification.category = category
return localNotification
}
在appDelegate里填下
//請(qǐng)求通知權(quán)限
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound , .Alert , .Badge], categories: nil))
}
使用時(shí):
//設(shè)置10秒的提醒
let completeNotification = setNotification("時(shí)間到了身腻,已完成任務(wù)",timeToNotification: 10,soundName: "提醒音樂(lè).mp3",category: nil)
UIApplication.sharedApplication().scheduleLocalNotification(completeNotification)
10秒后
有選項(xiàng)的本地操作
效果如圖:
設(shè)置Notification Action 和 Category
//MARK: - 提醒操作 設(shè)置
let notificationActionOk : UIMutableUserNotificationAction = UIMutableUserNotificationAction()
notificationActionOk.identifier = "completeRemindRater"
notificationActionOk.title = "再工作一會(huì)兒"
//是否取消提醒
notificationActionOk.destructive = false
//是否需要權(quán)限埠巨,例如鎖屏的時(shí)候戚炫,執(zhí)行操作是否需要解鎖再執(zhí)行
notificationActionOk.authenticationRequired = false
//啟動(dòng)app還是后臺(tái)執(zhí)行
notificationActionOk.activationMode = UIUserNotificationActivationMode.Background
let notificationActionRestNow: UIMutableUserNotificationAction = UIMutableUserNotificationAction()
notificationActionRestNow.identifier = "relaxNow"
notificationActionRestNow.title = "休息"
notificationActionRestNow.destructive = false
notificationActionRestNow.authenticationRequired = false
notificationActionRestNow.activationMode = UIUserNotificationActivationMode.Background
//MARK: -Notification Category 設(shè)置
let notificationCompleteCategory: UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
//記住這個(gè)identifier 突硝,待會(huì)用
notificationCompleteCategory.identifier = "COMPLETE_CATEGORY"
notificationCompleteCategory.setActions([notificationActionOk,notificationActionRestNow], forContext: .Default)
notificationCompleteCategory.setActions([notificationActionOk,notificationActionRestNow], forContext: .Minimal)
//請(qǐng)求用戶允許通知
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound , .Alert , .Badge], categories: NSSet(array: [notificationCompleteCategory]) as? Set<UIUserNotificationCategory>))
接著在使用時(shí)预烙,則將category參數(shù)的nil改為"COMPLETE_CATEGORY"
//跟之前的一樣,10秒后出通知币他,但category不同
let completeNotification = setNotification("時(shí)間到了鳍置,已完成任務(wù)",timeToNotification: 10,soundName: "提醒鈴聲.mp3",category: "COMPLETE_CATEGORY")
UIApplication.sharedApplication().scheduleLocalNotification(completeNotification)
在appDelegate使用下面method處理按下的選項(xiàng)
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
print("按下的選項(xiàng)的identifier是:\\\\(identifier)")
}
按下休息時(shí)登疗,終端顯示按下的選項(xiàng)的identifier是:relaxNow
通知快速回復(fù)
實(shí)現(xiàn)類似微信的快速回復(fù)
效果如圖:
實(shí)現(xiàn)基本同上面的選項(xiàng)通知一樣赂韵,就是創(chuàng)建時(shí)修改 action的behavior
為.TextInput
:
let replyAction = UIMutableUserNotificationAction()
replyAction.title = "回復(fù)"
replyAction.identifier = "inline-reply"
replyAction.activationMode = .Background
replyAction.authenticationRequired = false
replyAction.behavior = .TextInput
接著像設(shè)置有選項(xiàng)的操作那樣設(shè)置Category
let notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
notificationCategory.identifier = "REPLY_CATEGORY"
notificationCategory.setActions([replyAction], forContext: UIUserNotificationActionContext.Default)
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound , .Alert , .Badge], categories: NSSet(array: [notificationCategory]) as? Set<UIUserNotificationCategory>))
設(shè)置提醒娱节,使用本地操作時(shí)那個(gè)函數(shù)setNotification
let replyNotification = setNotification("說(shuō)些什么吧", timeToNotification: 10, soundName: "提醒音樂(lè).mp3" , category: "REPLY_CATEGORY")
UIApplication.sharedApplication().scheduleLocalNotification(replyNotification)
處理回復(fù):則在appDelegate下添加:
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
if let response = responseInfo[UIUserNotificationActionResponseTypedTextKey]{
let responseText = response as! String
print("\(responseText)")
}
completionHandler()
}
在快速回復(fù)如上圖打下Hello,world!
,終端就會(huì)出現(xiàn):
一些tips
//取消本地通知
UIApplication.sharedApplication().cancelAllLocalNotifications()
//修改App Icon上的數(shù)字如下圖中右上角的數(shù)字
UIApplication.sharedApplication().applicationIconBadgeNumber = 1
最后
通知的實(shí)現(xiàn)基本就如此了祭示。
(END and Thank U)
參考鏈接: