未显示UILocalNotification操作的操作

时间:2016-12-31 01:08:54

标签: ios swift uilocalnotification

我尝试添加/安排提供两项操作的本地通知,例如:

Example of iOS 10 notification actions

如果我已经在使用手机,我还希望将通知显示为横幅提醒,我可以向下滑动以查看操作。

现在,我已成功安排通知,它确实出现在锁定屏幕上。但是,当我向左滑动,然后点按“查看”时,我就看不到自己的行为了。

这是我的代码:

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
} 

任何人都可以看到我做错了什么并建议如何解决它?

1 个答案:

答案 0 :(得分:5)

尝试使用swift 3代码下面的代码

    let category = UIMutableUserNotificationCategory()

    let readAction = UIMutableUserNotificationAction()
    readAction.identifier = "xx"
    readAction.isDestructive = false
    readAction.title = "Read"
    readAction.activationMode = .background
    readAction.isAuthenticationRequired = false

    let saveAction = UIMutableUserNotificationAction()
    saveAction.identifier = "zz"
    saveAction.isDestructive = false
    saveAction.title = "Save"
    saveAction.activationMode = .background
    saveAction.isAuthenticationRequired = false

    let categoryIdentifier = "category.identifier"
    category.identifier = categoryIdentifier
    category.setActions([readAction,saveAction], for: .minimal)
    category.setActions([readAction,saveAction], for: .default)

    let categories = Set(arrayLiteral: category)
    let settings = UIUserNotificationSettings(types: .alert, categories: categories)
    UIApplication.shared.registerUserNotificationSettings(settings)


    let localNotif = UILocalNotification()
    localNotif.alertBody = "testBody"
    localNotif.category = categoryIdentifier
    localNotif.fireDate = Date()?.addingTimeInterval(10)

    UIApplication.shared.scheduleLocalNotification(localNotif)