3

I'm currently working with notifications and wanna print out an identifier. How can I replace all if-statements with one switch?

Here is my enum that keeps all identifier with corresponding string value:

    enum NotificationIdentifier:String {
    case local = "Local Notification"
    case localWithAction = "Local Notification with Action"
    case localWithContent = "Local Notification with Content"
    case pushWithAPNs = "Push Notification with  APNs"
    case pushWithFirebase = "Push Notification with Firebase"
    case pushWithContent = "Push Notification with Content"
}

And my delegate method:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    if response.notification.request.identifier == NotificationIdentifier.local.rawValue {
        print("Handling notification with the \(NotificationIdentifier.local.rawValue)")
    } else if response.notification.request.identifier == NotificationIdentifier.localWithAction.rawValue {
        print("Handling notification with the \(NotificationIdentifier.localWithAction.rawValue)")
    } else if response.notification.request.identifier == NotificationIdentifier.localWithContent.rawValue {
        print("Handling notification with the \(NotificationIdentifier.localWithContent.rawValue)")
    } else if response.notification.request.identifier == NotificationIdentifier.pushWithAPNs.rawValue {
        print("Handling notification with the \(NotificationIdentifier.pushWithAPNs.rawValue)")
    } else if response.notification.request.identifier == NotificationIdentifier.pushWithFirebase.rawValue {
        print("Handling notification with the \(NotificationIdentifier.pushWithFirebase.rawValue)")
    } else {
        print("Handling notification with the \(NotificationIdentifier.pushWithContent.rawValue)")
    }

    completionHandler()
}
Bugrym
  • 85
  • 1
  • 9
  • 1
    Thanks new user for the excellent, basic, question. This site welcomes basic questions if they are correctly written, thanks again. – Fattie Jun 12 '20 at 11:02

4 Answers4

2

Initialize the enum of NotificationIdentifier and use switch case like below:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    guard let notificationIdentifier = NotificationIdentifier(rawValue: response.notification.request.identifier) else { return }
    switch notificationIdentifier {
    case .local:
    print("Handling notification with the \(NotificationIdentifier.local.rawValue)")
    case .localWithAction:
    print("Handling notification with the \(NotificationIdentifier.localWithAction.rawValue)")
    case .localWithContent:
    print("Handling notification with the \(NotificationIdentifier.localWithContent.rawValue)")
    case .pushWithAPNs:
    print("Handling notification with the \(NotificationIdentifier.pushWithAPNs.rawValue)")
    case .pushWithFirebase:
    print("Handling notification with the \(NotificationIdentifier.pushWithFirebase.rawValue)")
    case .pushWithContent:
    print("Handling notification with the \(NotificationIdentifier.pushWithContent.rawValue)")
    }
}
koen
  • 5,383
  • 7
  • 50
  • 89
Frankenstein
  • 15,732
  • 4
  • 22
  • 47
1

You can use switch case on Enum like this:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    switch response.notification.request.identifier {
        case .local:
            print("local")
        case .localWithAction:
            print("localWithAction")
        case .localWithAction:
            print("localWithAction")
        case .localWithContent:
            print("localWithContent")
        case .pushWithAPNs:
            print("pushWithAPNs")
        case .pushWithFirebase:
            print("pushWithFirebase")
    default:
        print("pushWithContent")
    }
    completionHandler()
}
Keshu R.
  • 5,045
  • 1
  • 18
  • 38
1

You can avoid switch and if else statements like this... you get NotificationIdentifier case with this piece of code ...

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {


    if let notificationIdentifier =   NotificationIdentifier(rawValue: response.notification.request.identifier) {

        print("Handling notification with the \(notificationIdentifier.rawValue)")

        switch notificationIdentifier {
        case .local:
            print("Handle local")
        case .localWithAction:
            print("Handle localWithAction")
        case .localWithContent:
            print("Handle localWithContent")
        case .pushWithAPNs:
            print("Handle pushWithAPNs")
        case .pushWithFirebase:
            print("Handle pushWithFirebase")
        case .pushWithContent:
            print("Handle pushWithContent")

        }


    } else {
        print("cant convert to NotificationIdentifier")
    }

    completionHandler()
}
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
1

If you want to just print out an identifier you can use a single print statement:

let notificationIdentifier = NotificationIdentifier.init(rawValue: response.notification.request.identifier) ?? .pushWithContent)
print("Handling notification with the \(notificationIdentifier.rawValue)")

This will cover the default value (e.g. .pushWithContent) as well.

pawello2222
  • 46,897
  • 22
  • 145
  • 209