简体   繁体   English

UILocalNotification操作未显示的操作

[英]Actions Not Showing For UILocalNotification Action

I'm trying to add/schedule a local notification that provides two actions, like so: 我正在尝试添加/安排提供两个操作的本地通知,如下所示:

iOS 10通知动作示例

I'd also like the notification to show as a banner alert if I'm already using my phone, where I can just swipe down to see the actions. 如果我已经在使用手机,我还希望该通知显示为横幅警报,我可以在其中向下滑动以查看操作。

Right now, I'm successfully scheduling the notification and it does appear on the lock screen. 现在,我已经成功安排了通知,它确实出现在锁定屏幕上。 However, when I swipe left, then tap "View", I don't see my actions. 但是,当我向左滑动时,点击“查看”,则看不到我的动作。

Here's my code: 这是我的代码:

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
} 

Can anyone see what I'm doing wrong and suggest how to fix it? 谁能看到我在做什么错并建议如何解决?

Try below code with swift 3 it's work for me 尝试下面的代码迅速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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM