2016-12-31 66 views
2

我想添加/安排本地通知,提供兩個動作,就像這樣:行動沒有針對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 
} 

有人能看到我在做什麼錯,並提出如何解決它?

回答

4

嘗試用以下3 SWIFT代碼它的工作對我來說

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) 
+0

謝謝!我缺少category.setActions([readAction,saveAction],用於:.default) –

+1

謝謝,我錯過了'registerUserNotificationSettings(_ :)'調用。 –