Skip to content

Manage custom actions and multiple tap button actions of the same type #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions Example/BasicExample/BasicExample/Tab1ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}

Expand Down
103 changes: 80 additions & 23 deletions Sources/TwilioEngage/TwilioEngage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
Expand All @@ -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)
Expand Down
14 changes: 11 additions & 3 deletions Sources/TwilioEngage/TwilioEngageServiceExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)");
}
}
}
Expand Down