diff --git a/Example/BasicExample/BasicExample/Tab1ViewController.swift b/Example/BasicExample/BasicExample/Tab1ViewController.swift index bb74c93..fc21240 100644 --- a/Example/BasicExample/BasicExample/Tab1ViewController.swift +++ b/Example/BasicExample/BasicExample/Tab1ViewController.swift @@ -24,14 +24,26 @@ class Tab1ViewController: UITableViewController { self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "pushCell") Notification.Name.openButton.onPost { notification in - let name = notification.name - guard let deeplink = notification.userInfo?["deep_link"] as? String else {return} - print("Deep Link in viewDidLoad() \(deeplink)") - let storyboard = UIStoryboard(name: "Main", bundle: nil) + if let deeplink = notification.userInfo?["deep_link"] as? String { + print("Deep Link in viewDidLoad() \(deeplink)") + let storyboard = UIStoryboard(name: "Main", bundle: nil) - let deepLinkScreen = deeplink.replacingOccurrences(of: "engage://", with: "") - let deepLinkVC = storyboard.instantiateViewController(identifier: deepLinkScreen) - mainView?.navigationController?.pushViewController(deepLinkVC, animated: true) + let deepLinkScreen = deeplink.replacingOccurrences(of: "engage://", with: "") + let deepLinkVC = storyboard.instantiateViewController(identifier: deepLinkScreen) + mainView?.navigationController?.pushViewController(deepLinkVC, animated: true) + } else if let custom_action = notification.userInfo?["custom_action"] as? String { + if custom_action == "open_settings" { + print("Open Settings") + let storyboard = UIStoryboard(name: "Main", bundle: nil) + let settingsVC = storyboard.instantiateViewController(withIdentifier: "settings.vc") + mainView?.navigationController?.pushViewController(settingsVC, animated: true) + } else if (custom_action == "DeepLinkScreen") { + print("Deep Link Screen") + let storyboard = UIStoryboard(name: "Main", bundle: nil) + let deepLinkVC = storyboard.instantiateViewController(identifier: custom_action) + mainView?.navigationController?.pushViewController(deepLinkVC, animated: true) + } + } } } diff --git a/Sources/TwilioEngage/TwilioEngage.swift b/Sources/TwilioEngage/TwilioEngage.swift index 303ff24..4262b8d 100644 --- a/Sources/TwilioEngage/TwilioEngage.swift +++ b/Sources/TwilioEngage/TwilioEngage.swift @@ -185,38 +185,75 @@ extension TwilioEngage: RemoteNotifications { let identity = response.notification .request.content.categoryIdentifier let actionIdentifier = response.actionIdentifier - - switch actionIdentifier { - case "com.apple.UNNotificationDefaultActionIdentifier": // Standard tap action - switch identity { - case "open_app": - return - case "deep_link": - handleDeepLinks(userInfo: userInfo) - case "open_url": - if let urlString = userInfo["link"] as? String { - guard let url = URL(string: urlString) else {return} - UIApplication.shared.open(url, options: [:], completionHandler: nil) - } - default: - Notification.Name.openButton.post(userInfo: userInfo) - } + let actionIdentifierArr = actionIdentifier.components(separatedBy: "-") + let identifier = actionIdentifierArr.count > 0 ? actionIdentifierArr[0] : "" + let id = actionIdentifierArr.count > 1 ? actionIdentifierArr[1] : "" + + switch identifier { + case "com.apple.UNNotificationDefaultActionIdentifier": + handleTapNotification(identity: identity, userInfo: userInfo) case "open_app": return case "deep_link": - handleDeepLinks(userInfo: userInfo) + handleDeepLinksActionButtons(userInfo: userInfo, id: id) case "open_url": - if let actionLink = userDefaults?.string(forKey: "ActionLink") as? String { - guard let url = URL(string: actionLink) else {return} - UIApplication.shared.open(url, options: [:], completionHandler: nil) - } + handleOpenUrlsActionButtons(id: id) + default: + handleCustomActionButtons(userInfo: userInfo, id: id) + } + } + + func handleTapNotification(identity: String, userInfo: [AnyHashable: Any]) { + switch identity { + case "open_app": + return + case "deep_link": + handleDeepLinks(userInfo: userInfo) + case "open_url": + handleOpenUrls(userInfo: userInfo) default: - Notification.Name.openButton.post(userInfo: userInfo) + handleCustomAction(userInfo: userInfo) + } + } + + func handleOpenUrlsActionButtons(id: String) { + if let actionLink = userDefaults?.string(forKey: "ActionLink-\(id)") as? String { + guard let url = URL(string: actionLink) else {return} + UIApplication.shared.open(url, options: [:], completionHandler: nil) + userDefaults?.removeObject(forKey: "ActionLink-\(id)") + } + } + + func handleDeepLinksActionButtons(userInfo: [AnyHashable: Any], id: String) { + if let actionLink = userDefaults?.string(forKey: "ActionDeepLink-\(id)") as? String { + var deepLinkData: [AnyHashable: Any] = [ + "deep_link": actionLink, + ] + + //merge existing userInfo into deepLinkData dictionary + deepLinkData.merge(userInfo) { (current, _) in current } + + Notification.Name.openButton.post(userInfo: deepLinkData) + userDefaults?.removeObject(forKey: "ActionDeepLink-\(id)") + } + } + + func handleCustomActionButtons(userInfo: [AnyHashable: Any], id: String) { + if let customAction = userDefaults?.string(forKey: "CustomAction-\(id)") as? String { + var deepLinkData: [AnyHashable: Any] = [ + "custom_action": customAction, + ] + + //merge existing userInfo into deepLinkData dictionary + deepLinkData.merge(userInfo) { (current, _) in current } + + Notification.Name.openButton.post(userInfo: deepLinkData) + userDefaults?.removeObject(forKey: "CustomAction-\(id)") } } func handleDeepLinks(userInfo: [AnyHashable: Any]) { - if let actionLink = userDefaults?.string(forKey: "ActionLink") as? String { + if let actionLink = userInfo["link"] as? String { var deepLinkData: [AnyHashable: Any] = [ "deep_link": actionLink, ] @@ -227,6 +264,26 @@ extension TwilioEngage: RemoteNotifications { Notification.Name.openButton.post(userInfo: deepLinkData) } } + + func handleOpenUrls(userInfo: [AnyHashable: Any]) { + if let urlString = userInfo["link"] as? String { + guard let url = URL(string: urlString) else {return} + UIApplication.shared.open(url, options: [:], completionHandler: nil) + } + } + + func handleCustomAction(userInfo: [AnyHashable: Any]) { + if let customAction = userInfo["link"] as? String { + var deepLinkData: [AnyHashable: Any] = [ + "custom_action": customAction, + ] + + //merge existing userInfo into deepLinkData dictionary + deepLinkData.merge(userInfo) { (current, _) in current } + + Notification.Name.openButton.post(userInfo: deepLinkData) + } + } } #if os(tvOS) || os(iOS) || targetEnvironment(macCatalyst) diff --git a/Sources/TwilioEngage/TwilioEngageServiceExtension.swift b/Sources/TwilioEngage/TwilioEngageServiceExtension.swift index b0a2173..29d4228 100644 --- a/Sources/TwilioEngage/TwilioEngageServiceExtension.swift +++ b/Sources/TwilioEngage/TwilioEngageServiceExtension.swift @@ -84,7 +84,9 @@ public class TwilioEngageServiceExtension: UtilityPlugin { if actionIdentifier == "open_app" && action != "" { actionIdentifier = "deep_link" } - let actionBtn = UNNotificationAction(identifier: actionIdentifier, title: actionName, options: [UNNotificationActionOptions.foreground]) + let id = actionButton["id"] as? String ?? "" + let identifier = "\(actionIdentifier)-\(id)" + let actionBtn = UNNotificationAction(identifier: identifier, title: actionName, options: [UNNotificationActionOptions.foreground]) buttons.append(actionBtn) @@ -94,13 +96,19 @@ public class TwilioEngageServiceExtension: UtilityPlugin { if actionIdentifier == "open_url" { if let link = actionButton["link"] as? String { - userDefaults?.set(link, forKey: "ActionLink"); + userDefaults?.set(link, forKey: "ActionLink-\(id)"); } } if actionIdentifier == "deep_link" { if let link = actionButton["link"] as? String { - userDefaults?.set(link, forKey: "ActionLink"); + userDefaults?.set(link, forKey: "ActionDeepLink-\(id)"); + } + } + + if actionIdentifier == "custom_action" { + if let link = actionButton["link"] as? String { + userDefaults?.set(link, forKey: "CustomAction-\(id)"); } } }