Auparavant, j'utilisais le code suivant pour différencier si ma notification est locale ou distante lorsque l'application démarre
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {}
Les conditions sont que mon application est tuée et je l'ouvre à partir de la notification. / p>
Le problème est que cette méthode
if (launchOptions?[UIApplication.LaunchOptionsKey.localNotification] != nil) { }
est obsolète et la méthode suivante n'est pas appelée lorsque l'application est ouverte à partir du centre de notification
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { if (launchOptions?[UIApplication.LaunchOptionsKey.localNotification] != nil) { } if (launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] != nil) { } }
3 Réponses :
Vous pouvez vérifier le type de notification dans userNotificationCenter: didReceiveNotificationResponse: withCompletionHandler:
également,
La hiérarchie des classes est:
UNNotificationResponse
>UNNotification
>UNNotificationRequest
>UNNotificationTrigger
Il existe 4 types de déclencheurs dans UNNotificationRequest
:
UNLocationNotificationTrigger
UNPushNotificationTrigger
UNTimeIntervalNotificationTrigger
UNCalendarNotificationTrigger
Utilisez simplement,
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { if response.notification.request.trigger is UNPushNotificationTrigger { print("remote notification"); } }
Cela ne fonctionne que lorsque l'application est au premier plan ou en arrière-plan. Cela ne fonctionne pas lorsque l'application est supprimée pour un cas de notification local
Lors de la création de la notification locale, définissez l'identifiant dans la notification qui peut être utilisé pour identifier la différence dans la notification de traitement
Voici un exemple de création de notification locale avec identifiant.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {     print("handling notification") if response.notification.request.identifier == "TestIdentifier" {   print("handling notifications with the TestIdentifier Identifier")   }else {   if let notification = response.notification.request.content.userInfo as? [String:AnyObject] {       let message = parseRemoteNotification(notification: notification)     print(message as Any)   } }   completionHandler() }  private func parseRemoteNotification(notification:[String:AnyObject]) -> String? {     if let aps = notification["aps"] as? [String:AnyObject] {       let alert = aps["alert"] as? String     return alert   }    return nil }
Gestion des notifications locales avec identifiant.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {     print("handling notification")   if let notification = response.notification.request.content.userInfo as? [String:AnyObject] {       let message = parseRemoteNotification(notification: notification)     print(message as Any)   }   completionHandler() }  private func parseRemoteNotification(notification:[String:AnyObject]) -> String? {     if let aps = notification["aps"] as? [String:AnyObject] {       let alert = aps["alert"] as? String     return alert   }    return nil }
Pour gérer les notifications à distance, vous pouvez utiliser la ligne suivante
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { if response.notification.request.identifier == "TestIdentifier" { Â Â print("handling notifications with the TestIdentifier Identifier") Â Â } completionHandler() }
Vous pouvez ajouter une condition supplémentaire pour gérer à la fois la notification dans la même méthode en vérifiant l'identifiant dans le premier ligne.
let content = UNMutableNotificationContent() content.title = "Title" content.body = "Body" content.sound = UNNotificationSound.default() let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) let request = UNNotificationRequest(identifier: "TestIdentifier", content: content, trigger: trigger) UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
Vous pouvez définir vos valeurs de clé de notifications locales dans content.userInfo.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { print(response.notification.request.content.userInfo) //you can check your notification types }
Ensuite, vérifiez la méthode de réponse didReceive de UNUserNotificationCenterDelegate:
content.userInfo = ["isMyLocationNotification" : true] //you can set anything
Dans la section de sortie, vous utiliserez les données d'informations avec la clé isMyLocationNotification, vous pouvez maintenant identifier la notification météo locale ou distante.