1趾浅、在AppDelegate中注冊交互性通知:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert, categories: nil))
//MARK: - 提醒操作 設置
let notificationActionOk : UIMutableUserNotificationAction = UIMutableUserNotificationAction()
notificationActionOk.identifier = "completeRemindRater"
notificationActionOk.title = "再工作一會兒"
//是否取消提醒
notificationActionOk.destructive = false
//是否需要權限,例如鎖屏的時候涕蜂,執(zhí)行操作是否需要解鎖再執(zhí)行
notificationActionOk.authenticationRequired = false
//啟動app還是后臺執(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 設置
let notificationCompleteCategory: UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
//記住這個identifier 华匾,待會用
notificationCompleteCategory.identifier = "COMPLETE_CATEGORY"
notificationCompleteCategory.setActions([notificationActionOk,notificationActionRestNow], forContext: .Default)
notificationCompleteCategory.setActions([notificationActionOk,notificationActionRestNow], forContext: .Minimal)
//請求用戶允許通知
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:[.Sound,.Alert,.Badge], categories: NSSet(array: [notificationCompleteCategory]) as? Set<UIUserNotificationCategory>))
return true
}
2、在需要發(fā)送通知的地方實現(xiàn)以下代碼
@IBAction func sendNoti(sender: AnyObject) {
//設置10秒的提醒
// let completeNotification = setNotification("時間到了机隙,已完成任務",timeToNotification: 10,soundName: "提醒音樂.mp3",category:"")
let completeNotification = setNotification("時間到了蜘拉,已完成任務",timeToNotification: 5,soundName: "提醒鈴聲.mp3",category: "COMPLETE_CATEGORY")
UIApplication.sharedApplication().scheduleLocalNotification(completeNotification)
}
func setNotification(body:String,timeToNotification:Double,soundName:String,category:String) -> UILocalNotification {
let localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "滑動查看信息"
localNotification.alertBody = body
//在mainBundle的短于30秒的播放文件,否則就會是系統(tǒng)默認聲音
localNotification.soundName = soundName
localNotification.fireDate = NSDate(timeIntervalSinceNow: timeToNotification)
//下面這條category語句是對應下面的“有選項的本地操作”章節(jié)
localNotification.category = category
return localNotification
}
3、在AppDelegate中實現(xiàn)點擊交互按鈕的回調(diào)有鹿,
?completionHandler()回調(diào)必須實現(xiàn)旭旭,否則會報警告,而且執(zhí)行不了里面的流程
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
print("按下的選項的identifier是:\(identifier)")
//必須實現(xiàn)的方法葱跋,否則會報一個警告
completionHandler()
}