Search code examples
iosswiftuilocalnotification

How to differentiate between two local notifications


I have two local notifications, one that triggers based on date and the other one based on time.

When they are triggered the didReceive delegate is called with the UNNotificationDefaultActionIdentifier identifier:

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()
}

Is there a way inside this delegate to differentiate between the two notifications ?

I want different actions based on what notifications was triggered.


Solution

  • Your response is a UNNotificationResponse. It has two immutable properties:

    • actionIdentifier, a String which is correlates to the categories you've added to userNotificationCenter
    • notification which is a UNNotification which contains the original request ie it's an instance of UNNotificationRequest.

    So switch using: response.notification.request.identifier