Skip to content

fix badge count logic #16

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
Nov 21, 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
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
}
}