0

Je développe une application WatchOS3 dans laquelle l'utilisateur reçoit des notifications locales avec des actions personnalisées. L'utilisateur dispose de 2 actions personnalisées qu'il peut appeler sur la notification, l'option 1 et l'option 2. Une fois que l'utilisateur a tapé sur l'une des options, l'application doit se lancer dans une vue spécifique.Comment ouvrir un contrôleur de vue spécifique à partir d'une action de notification personnalisée dans WatchOS 3

Jusqu'à présent, les actions de notification sont traitées correctement avec cette fonction dans le ExtenionsDelegate:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
    print("Tapped in notification") 
    let identifier = response.actionIdentifier 
    print(identifier) 

    switch(identifier){ 
    case "option1": 
     print("tapped option1") 
    case "option2": 
     print("tapped option2") 
    default: break 
    } 
    completionHandler() 
} 

Et voici le code de mon principal InterfaceController dans lequel les catégories de notifications sont définies:

func actioncategories() { 

    let option1 = UNNotificationAction(identifier: "option1", title: "Test Option 1", options: .foreground) //Button 1 
    let option2 = UNNotificationAction(identifier: "option2", title: "Test Option 2", options: .foreground) //Button 2 

    let actioncategory = UNNotificationCategory(identifier: "action_category", actions: [option1, option2], intentIdentifiers: []) 

    UNUserNotificationCenter.current().setNotificationCategories([actioncategory]) //setting actions & categories 
} 

Maintenant, comment puis-je dire à mon application de se lancer dans une vue spécifique lorsque l'option 1 ou l'option 2 est sélectionnée?

Répondre

0

J'ai trouvé une solution:

  • Au lieu d'utiliser func userNotificationCenter dans ExtensionsDelegate, utilisez func handleAction (identifiant de withIdentifier: String ?, pour la notification: UNNotification) dans votre contrôleur principal de l'interface

  • avec presentController (withName:, context:) vous pouvez ouvrir une vue spécifique

Code (InterfaceController):

override func handleAction(withIdentifier identifier: String?, for notification: UNNotification) { 
    print("Tapped in notification") 
    print(identifier) 

    switch(identifier){ 
    case "option1"?: 
     print("tapped option1") 
     presentController(withName: "Option1_Screen", context: "segue") 
    case "option2"?: 
     print("tapped option2") 
     presentController(withName: "Option2_Screen", context: "segue") 
    default: break 
    } 
}