2
我想添加/安排本地通知,提供兩個動作,就像這樣:行動沒有針對UILocalNotification行動
我也很喜歡的通知顯示的一面旗幟警報如果我已經在使用我的手機,我只需向下滑動即可查看操作。
現在,我已成功安排通知,並確實顯示在鎖定屏幕上。但是,當我向左滑動,然後點擊「查看」,我看不到我的動作。
這裏是我的代碼:
func addNewNotificationWith (name: String, date: Date) {
let message = "You will see this, but can't do anything! lol."
let newLocalNotif = UILocalNotification()
newLocalNotif.fireDate = dueDateWarningTime
newLocalNotif.alertBody = message
newLocalNotif.timeZone = TimeZone.autoupdatingCurrent
newLocalNotif.soundName = UILocalNotificationDefaultSoundName
newLocalNotif.category = "DueDates"
let ignoreAction = getNotificationAction(identifier: "", btnTitle: "Ignore")
let doneAction = getNotificationAction(identifier: "", btnTitle: "Done")
let category = UIMutableUserNotificationCategory()
category.identifier = "DueDates"
category.setActions([ignoreAction, doneAction], for: UIUserNotificationActionContext.minimal)
let settings = UIUserNotificationSettings(types: .alert, categories: [category])
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.scheduleLocalNotification(newLocalNotif)
print("New notification added.")
}
func getNotificationAction (identifier: String, btnTitle: String) -> UIMutableUserNotificationAction {
let incrementAction = UIMutableUserNotificationAction()
incrementAction.identifier = identifier
incrementAction.title = btnTitle
incrementAction.activationMode = UIUserNotificationActivationMode.foreground
incrementAction.isAuthenticationRequired = true
incrementAction.isDestructive = false
return incrementAction
}
有人能看到我在做什麼錯,並提出如何解決它?
謝謝!我缺少category.setActions([readAction,saveAction],用於:.default) –
謝謝,我錯過了'registerUserNotificationSettings(_ :)'調用。 –