2016-12-31 3 views
2

Je suis en train d'ajouter/planifier une notification locale qui fournit deux actions, comme suit:Actions Non Affichage Pour UILocalNotification action

Example of iOS 10 notification actions

Je voudrais aussi la notification pour montrer comme une alerte bannière si je suis déjà en train d'utiliser mon téléphone, où je peux simplement glisser vers le bas pour voir les actions.

En ce moment, je planifie la notification avec succès et il apparaît sur l'écran de verrouillage. Cependant, quand je balaye à gauche, puis appuyez sur "Voir", je ne vois pas mes actions.

Voici mon 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 
} 

Quelqu'un peut-il voir ce que je fais mal et suggérer comment y remédier?

Répondre

4

Essayez ci-dessous avec le code swift 3, il est un travail pour moi

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

Merci! Il me manquait category.setActions ([readAction, saveAction], pour: .default) –

+1

Merci, il me manquait l'appel 'registerUserNotificationSettings (_ :)'. –