Skip to content

Commit

Permalink
fix badge count logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alanjcharles committed Nov 21, 2023
1 parent 45f0fd2 commit cf190d8
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 235 deletions.
142 changes: 80 additions & 62 deletions Example/BasicExample/BasicExample.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

This file was deleted.

2 changes: 1 addition & 1 deletion Example/BasicExample/BasicExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD

func applicationWillEnterForeground(_ application: UIApplication) {

UserDefaults(suiteName: "group.com.segment.twiliopush")?.set(1, 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
2 changes: 1 addition & 1 deletion Example/BasicExample/BasicExample/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
UserDefaults(suiteName: "group.com.segment.twiliopush")?.set(1, forKey: "Count"); UIApplication.shared.applicationIconBadgeNumber = 0
UserDefaults(suiteName: "group.com.segment.twilioEngage")?.set(0, forKey: "Count"); UIApplication.shared.applicationIconBadgeNumber = 0
}

func sceneDidEnterBackground(_ scene: UIScene) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@
<dict>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>UNNotificationExtensionCategory</key>
<string>open_url</string>
<key>UNNotificationExtensionUserInteractionEnabled</key>
<true/>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.usernotifications.service</string>
<key>NSExtensionPrincipalClass</key>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// NotificationService.swift
// TwilioEngageExampleNotificationService
//
// Created by Alan Charles on 7/29/23.
//

import UserNotifications

class NotificationService: UNNotificationServiceExtension {

var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
let userDefaults = UserDefaults(suiteName: "group.com.segment.twilioEngage")

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

if let bestAttemptContent = bestAttemptContent {
// set badge
let badgeAmount = bestAttemptContent.userInfo["badgeAmount"] as? Int ?? 1
let badgeStrategy = bestAttemptContent.userInfo["badgeStrategy"] as? String
var currentBadge = 2

print("****CURRENTBADGE******\(currentBadge)")
switch badgeStrategy {
case "inc":
currentBadge += badgeAmount
case "dec":
currentBadge -= badgeAmount
case "set":
currentBadge = badgeAmount
default:
currentBadge = badgeAmount
}

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

// handle media
var urlString: String? = nil
if let mediaArray: NSArray = bestAttemptContent.userInfo["media"] as? NSArray {

if let mediaURLString = mediaArray[0] as? String {
urlString = mediaURLString
}

if urlString != nil, let fileURL = URL(string: urlString!){

guard let mediaData = NSData(contentsOf: fileURL) else {
contentHandler(bestAttemptContent)
return
}

guard let mediaAttachment = UNNotificationAttachment.saveImageToDisk(fileIdentifier: "engage-image.png", data: mediaData, options: nil) else {
contentHandler(bestAttemptContent)
return
}

bestAttemptContent.attachments = [ mediaAttachment ]
}
}


contentHandler(bestAttemptContent)
}

}

override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}

}

@available(iOSApplicationExtension 10.0, *)
extension UNNotificationAttachment {

static func saveImageToDisk(fileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
let fileManager = FileManager.default
let folderName = ProcessInfo.processInfo.globallyUniqueString
let folderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(folderName, isDirectory: true)

do {
try fileManager.createDirectory(at: folderURL!, withIntermediateDirectories: true, attributes: nil)
let fileURL = folderURL?.appendingPathComponent(fileIdentifier)
try data.write(to: fileURL!, options: [])
let attachment = try UNNotificationAttachment(identifier: fileIdentifier, url: fileURL!, options: options)
return attachment
} catch let error {
print("error \(error)")
}

return nil
}
}

0 comments on commit cf190d8

Please sign in to comment.