Skip to content

Handle Open url notification and change badge count #19

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 1, 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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions Example/BasicExample/BasicExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ProgressWebViewController
import TwilioEngage

extension Analytics {
static var main = Analytics(configuration: Configuration(writeKey: "<Write_Key>")
static var main = Analytics(configuration: Configuration(writeKey: "hhInltnMJvrod9b4ajufhz4k39E5KJXZ")
.flushAt(1)
.trackApplicationLifecycleEvents(true))
}
Expand Down Expand Up @@ -80,6 +80,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
Analytics.main.receivedRemoteNotification(userInfo: userInfo)

completionHandler([.banner, .sound, .badge])

UserDefaults(suiteName: "group.com.segment.twilioEngage")?.set(0, forKey: "Count")
UIApplication.shared.applicationIconBadgeNumber = 0
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
Expand All @@ -93,8 +96,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
}

func applicationWillEnterForeground(_ application: UIApplication) {

UserDefaults(suiteName: "group.com.segment.twilioEngage")?.set(0, forKey: "Count"); UIApplication.shared.applicationIconBadgeNumber = 0
UserDefaults(suiteName: "group.com.segment.twilioEngage")?.set(0, forKey: "Count")
UIApplication.shared.applicationIconBadgeNumber = 0
}

// MARK: UISceneSession Lifecycle
Expand Down
18 changes: 14 additions & 4 deletions Sources/TwilioEngage/TwilioEngage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,25 @@ extension TwilioEngage: RemoteNotifications {
let actionIdentifier = response.actionIdentifier

switch actionIdentifier {
case "com.apple.UNNotificationDefaultActionIdentifier": // Standard tap action
switch identity {
case "open_app":
return
case "deep_link":
Notification.Name.openButton.post(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)
}
case "open_app":
return
case "deep_link":
Notification.Name.openButton.post(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)
}
if let actionLink = userDefaults?.string(forKey: "ActionLink") as? String {
guard let url = URL(string: actionLink) else {return}
UIApplication.shared.open(url, options: [:], completionHandler: nil)
Expand Down
9 changes: 7 additions & 2 deletions Sources/TwilioEngage/TwilioEngageServiceExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class TwilioEngageServiceExtension: UtilityPlugin {
// set badge
let badgeAmount = content.userInfo["badgeAmount"] as? Int ?? 1
let badgeStrategy = content.userInfo["badgeStrategy"] as? String
var currentBadge = 2
var currentBadge = userDefaults?.value(forKey: "Count") as? Int ?? 0

let userInfo = content.userInfo
let identity = content.categoryIdentifier
Expand All @@ -42,6 +42,7 @@ public class TwilioEngageServiceExtension: UtilityPlugin {
currentBadge = badgeAmount
}

currentBadge = max(currentBadge, 0)
userDefaults?.set(currentBadge, forKey: "Count")
content.badge = NSNumber(value: currentBadge)

Expand Down Expand Up @@ -77,8 +78,12 @@ public class TwilioEngageServiceExtension: UtilityPlugin {
var buttons: [UNNotificationAction] = []

for actionButton in actionButtons {
if let actionIdentifier = actionButton["onTap"] as? String,
if var actionIdentifier = actionButton["onTap"] as? String,
let actionName = actionButton["text"] as? String {
let action = actionButton["link"] as? String ?? ""
if actionIdentifier == "open_app" && action != "" {
actionIdentifier = "deep_link"
}
let actionBtn = UNNotificationAction(identifier: actionIdentifier, title: actionName, options: [UNNotificationActionOptions.foreground])

buttons.append(actionBtn)
Expand Down