Skip to content

Commit

Permalink
feature(sdk): channels support, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
worotyns committed Jun 13, 2023
1 parent f7c05dd commit 424f819
Show file tree
Hide file tree
Showing 19 changed files with 313 additions and 152 deletions.
2 changes: 1 addition & 1 deletion PpgCoreSDK.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |spec|
#

spec.name = "PpgCoreSDK"
spec.version = "0.0.8"
spec.version = "0.0.9"
spec.summary = "PushPushGo Core SDK"

# This description is used to generate tags and improve search results.
Expand Down
14 changes: 11 additions & 3 deletions PpgCoreSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
460B69762A38AE3800FE1D07 /* NotificationChannel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 460B69752A38AE3800FE1D07 /* NotificationChannel.swift */; };
460B69782A38B60000FE1D07 /* NotificationAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 460B69772A38B60000FE1D07 /* NotificationAction.swift */; };
464698DE2A16665700AD81FD /* PpgCoreConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 464698DD2A16665700AD81FD /* PpgCoreConfig.swift */; };
465FEA8D29D58A4400C85F94 /* EventService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 465FEA8B29D58A4400C85F94 /* EventService.swift */; };
465FEA8E29D58A4400C85F94 /* PpgCoreClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 465FEA8C29D58A4400C85F94 /* PpgCoreClient.swift */; };
Expand Down Expand Up @@ -41,13 +43,15 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
464698DD2A16665700AD81FD /* PpgCoreConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PpgCoreConfig.swift; sourceTree = "<group>"; };
460B69752A38AE3800FE1D07 /* NotificationChannel.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = NotificationChannel.swift; sourceTree = "<group>"; tabWidth = 4; };
460B69772A38B60000FE1D07 /* NotificationAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationAction.swift; sourceTree = "<group>"; };
464698DD2A16665700AD81FD /* PpgCoreConfig.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = PpgCoreConfig.swift; sourceTree = "<group>"; tabWidth = 4; };
465FEA8B29D58A4400C85F94 /* EventService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EventService.swift; sourceTree = "<group>"; };
465FEA8C29D58A4400C85F94 /* PpgCoreClient.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PpgCoreClient.swift; sourceTree = "<group>"; };
465FEA9129D58A5700C85F94 /* Data.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Data.swift; sourceTree = "<group>"; };
465FEA9229D58A5700C85F94 /* UNNotificationAttachment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UNNotificationAttachment.swift; sourceTree = "<group>"; };
465FEA9329D58A5700C85F94 /* TemporaryImage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TemporaryImage.swift; sourceTree = "<group>"; };
465FEA9729D58A6400C85F94 /* DataNotification.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataNotification.swift; sourceTree = "<group>"; };
465FEA9729D58A6400C85F94 /* DataNotification.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = DataNotification.swift; sourceTree = "<group>"; tabWidth = 4; };
465FEA9829D58A6400C85F94 /* NotificationDelivered.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotificationDelivered.swift; sourceTree = "<group>"; };
465FEA9929D58A6400C85F94 /* NotificationClosed.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotificationClosed.swift; sourceTree = "<group>"; };
465FEA9A29D58A6400C85F94 /* NotificationClicked.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotificationClicked.swift; sourceTree = "<group>"; };
Expand All @@ -61,7 +65,7 @@
46CB7CA129D322E400D1E118 /* PpgCoreSDK.docc */ = {isa = PBXFileReference; lastKnownFileType = folder.documentationcatalog; path = PpgCoreSDK.docc; sourceTree = "<group>"; };
46CB7CA729D322E400D1E118 /* PpgCoreSDKTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PpgCoreSDKTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
46CB7CAC29D322E400D1E118 /* PpgCoreSDKTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PpgCoreSDKTest.swift; sourceTree = "<group>"; };
46EF4EA32A15271000C5054A /* PpgCoreNotificationServiceExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PpgCoreNotificationServiceExtension.swift; sourceTree = "<group>"; };
46EF4EA32A15271000C5054A /* PpgCoreNotificationServiceExtension.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = PpgCoreNotificationServiceExtension.swift; sourceTree = "<group>"; tabWidth = 4; };
46EF4EA52A15288E00C5054A /* NotificationFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationFactory.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -115,6 +119,8 @@
465FEA9C29D58A6400C85F94 /* Subscription.swift */,
465FEA9729D58A6400C85F94 /* DataNotification.swift */,
465FEA9B29D58A6400C85F94 /* SilentNotification.swift */,
460B69752A38AE3800FE1D07 /* NotificationChannel.swift */,
460B69772A38B60000FE1D07 /* NotificationAction.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -279,7 +285,9 @@
files = (
465FEACA29D5939400C85F94 /* LocalNotifcationService.swift in Sources */,
465FEA9E29D58A6400C85F94 /* DataNotification.swift in Sources */,
460B69762A38AE3800FE1D07 /* NotificationChannel.swift in Sources */,
465FEA8D29D58A4400C85F94 /* EventService.swift in Sources */,
460B69782A38B60000FE1D07 /* NotificationAction.swift in Sources */,
465FEAA129D58A6400C85F94 /* NotificationClicked.swift in Sources */,
465FEA9429D58A5700C85F94 /* Data.swift in Sources */,
465FEAA329D58A6400C85F94 /* Subscription.swift in Sources */,
Expand Down
Binary file not shown.
46 changes: 8 additions & 38 deletions PpgCoreSDK/Models/DataNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,10 @@
import Foundation
import UserNotifications

struct NotificationAction: Codable {
let icon: String
let action: String
let title: String
let url: String

static func fromJson(json: Data) -> [NotificationAction] {
let decoder = JSONDecoder()

guard let deserialized = try? decoder.decode([NotificationAction].self, from: json) else {
fatalError("Failed to decode notification action from JSON")
}

return deserialized
}
}

struct DataNotification: Notification {
var contextId: UUID
var messageId: UUID
var sound: UNNotificationSound?
var channelName: String
var foreignId: String?
var actions: [NotificationAction] = []
var title: String? = nil
Expand All @@ -46,7 +29,7 @@ struct DataNotification: Notification {
self.title = content.title
self.subtitle = content.subtitle
self.body = content.body
self.sound = content.sound
self.channelName = content.userInfo["channelName"] as? String ?? "default"
self.icon = content.userInfo["icon"] as? String
self.image = content.userInfo["image"] as? String
self.url = content.userInfo["url"] as? String
Expand Down Expand Up @@ -98,27 +81,14 @@ struct DataNotification: Notification {
if (self.body != nil) {
content.body = self.body!
}


switch(self.actions.count) {
case 1:
content.categoryIdentifier = "PPG_NOTIFICATION_WITH_ACTION"

if (self.actions.count > 0) {
content.userInfo["actions"] = self.serializeActions()
break;
case 2:
content.categoryIdentifier = "PPG_NOTIFICATION_WITH_ACTIONS"
content.userInfo["actions"] = self.serializeActions()
break;
default:
content.categoryIdentifier = "PPG_NOTIFICATION"
break
}

if (self.sound != nil) {
content.sound = self.sound
} else {
content.sound = UNNotificationSound.default
}

let channel = PpgCoreConfig.shared.getChannel(notification: self)
content.categoryIdentifier = channel.name
content.sound = channel.sound

if let image = self.image {
if let attachment = try? UNNotificationAttachment(url: image) {
Expand Down
25 changes: 25 additions & 0 deletions PpgCoreSDK/Models/NotificationAction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// NotificationAction.swift
// PpgCoreSDK
//
// Created by Mateusz Worotyński on 13/06/2023.
//

import Foundation

struct NotificationAction: Codable {
let icon: String?
let action: String
let title: String
let url: String

static func fromJson(json: Data) -> [NotificationAction] {
let decoder = JSONDecoder()

guard let deserialized = try? decoder.decode([NotificationAction].self, from: json) else {
fatalError("Failed to decode notification action from JSON")
}

return deserialized
}
}
22 changes: 22 additions & 0 deletions PpgCoreSDK/Models/NotificationChannel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Channel.swift
// PpgCoreSDK
//
// Created by Mateusz Worotyński on 13/06/2023.
//

import Foundation
import UserNotifications

public enum NotificationChannelType: String {
case DEFAULT
case DEFAULT_WITH_ACTION
case DEFAULT_WITH_ACTIONS
case CUSTOM
}

struct NotificationChannel {
var name: String
var sound: UNNotificationSound
var actions: [String]
}
77 changes: 5 additions & 72 deletions PpgCoreSDK/PpgCoreClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public enum SubscriptionActionResult {
}

public class PpgCoreClient: NSObject {
let eventService: EventService = EventService(config: PpgCoreConfig())
let eventService: EventService = EventService(config: PpgCoreConfig.shared)

@available(iOSApplicationExtension, unavailable)
private func setBadge(num: Int) {
Expand All @@ -26,46 +26,11 @@ public class PpgCoreClient: NSObject {
private func getBadge() -> Int {
return UIApplication.shared.applicationIconBadgeNumber
}

private func createCategoryAction(label: String, index: Int) -> UNNotificationAction {
return UNNotificationAction(
identifier: "action_\(index)",
title: label,
options: [.foreground]
)
}

public func initialize(actionLabels: [String]) {
let firstAction = actionLabels.first ?? "Open"
let secondAction = actionLabels.last ?? "Show more"
UNUserNotificationCenter.current()
.setNotificationCategories([
UNNotificationCategory(
identifier: "PPG_NOTIFICATION_WITH_ACTIONS",
actions: [firstAction, secondAction].enumerated().map {( index, item ) in
createCategoryAction(label: item, index: index)
},
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder: "",
options: [.customDismissAction]
),
UNNotificationCategory(
identifier: "PPG_NOTIFICATION_WITH_ACTION",
actions: [firstAction].enumerated().map {( index, item ) in
createCategoryAction(label: item, index: index)
},
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder: "",
options: [.customDismissAction]
),
UNNotificationCategory(
identifier: "PPG_NOTIFICATION",
actions: [],
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder: "",
options: [.customDismissAction]
)
])
public func initialize(actionLabels: [String] = []) {
UNUserNotificationCenter
.current()
.setNotificationCategories(PpgCoreConfig.shared.getCategories(actionLabels: actionLabels))
}

@available(iOSApplicationExtension, unavailable)
Expand Down Expand Up @@ -185,7 +150,6 @@ public class PpgCoreClient: NSObject {
let dataNotification = NotificationFactory.createData(content: request.content)
let newContent = dataNotification.toUNNotificationMutableContent()
eventService.send(delivered: dataNotification.createDeliveredEvent())
// setActionsOnNotificationCenter(actions: dataNotification.getUNNotificationActions())
contentHandler(newContent);
return newContent

Expand Down Expand Up @@ -214,7 +178,6 @@ public class PpgCoreClient: NSObject {
return;
case .data:
PpgCoreLogger.info("Got background message as data message omit registering event");
// setActionsOnNotificationCenter(actions: self.getActionsFromJSON(jsonString: userInfo["actions"] as! String))
completionHandler(.newData)
return;
case .unknown:
Expand Down Expand Up @@ -245,7 +208,6 @@ public class PpgCoreClient: NSObject {
PpgCoreLogger.info("Got data message from local notifications");
let dataNotification = NotificationFactory.createData(content: notification.request.content)
eventService.send(delivered: dataNotification.createDeliveredEvent())
// setActionsOnNotificationCenter(actions: dataNotification.getUNNotificationActions())
break;
case .silent:
PpgCoreLogger.info("Got silent message from local notifications");
Expand All @@ -263,33 +225,4 @@ public class PpgCoreClient: NSObject {
completionHandler([.alert, .sound])
}
}

/// Method prepared for dynamic actions, but due to iOS platform weak support not used for now
public func setActionsOnNotificationCenter(actions: [UNNotificationAction]) {
UNUserNotificationCenter
.current()
.setNotificationCategories([
UNNotificationCategory(
identifier: "PPG_NOTIFICATION_WITH_ACTIONS",
actions: actions,
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder: "",
options: [.customDismissAction]
),
UNNotificationCategory(
identifier: "PPG_NOTIFICATION_WITH_ACTION",
actions: actions,
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder: "",
options: [.customDismissAction]
),
UNNotificationCategory(
identifier: "PPG_NOTIFICATION",
actions: [],
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder: "",
options: [.customDismissAction]
)
])
}
}
2 changes: 1 addition & 1 deletion PpgCoreSDK/PpgCoreNotificationServiceExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UserNotifications

open class PpgCoreNotificationServiceExtension: UNNotificationServiceExtension {

let eventService: EventService = EventService(config: PpgCoreConfig())
let eventService: EventService = EventService(config: PpgCoreConfig.shared)

var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
Expand Down
Loading

0 comments on commit 424f819

Please sign in to comment.