2017-08-09 4 views
2

J'ai deux notifications locales, l'une qui se déclenche en fonction de la date et l'autre en fonction de l'heure.Comment différencier deux notifications locales

Quand ils sont déclenchés le délégué didReceive est appelé avec l'identifiant UNNotificationDefaultActionIdentifier:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
    switch response.actionIdentifier { 
    case UNNotificationDismissActionIdentifier: 
     print("Dismiss Action") 
    case UNNotificationDefaultActionIdentifier: 
     // this part is called when notification is triggered 
    ...................................... 
    default: 
     print("Unknown action") 
    } 

    completionHandler() 
} 

Est-il possible à l'intérieur de ce délégué à la différence entre les deux notifications?

Je souhaite des actions différentes en fonction des notifications déclenchées.

Répondre

1

Votre réponse est un UNNotificationResponse. Il a deux propriétés immuables:

  • actionIdentifier, un String qui est en corrélation avec les catégories que vous avez ajoutés à userNotificationCenter
  • notification qui est un UNNotification qui contient l'original demande-à-dire qu'il est une instance de UNNotificationRequest .

interrupteur Donc, en utilisant: response.notification.request.identifier

1

Essayez avec

response.notification.request.identifier 

l'UNNotificationRequest ont l'identifiant, comme spectacle dans UNNotificationRequest.h

Hope this helps