Skip to content

Commit

Permalink
Merge pull request #5 from GlobalMessageServices/RDGMS-391-create-fun…
Browse files Browse the repository at this point in the history
…ctionality-of-push-notifications-receiving-and-processing-within-i-os-sdk-push-notification-service

Rdgms 391 create functionality of push notifications receiving and processing within i os sdk push notification service
  • Loading branch information
Oleksii Korniienko authored Sep 20, 2022
2 parents 65e1903 + 6f7517e commit eb36090
Show file tree
Hide file tree
Showing 11 changed files with 676 additions and 231 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Build
on:
push:
branches:
- gmsapi
- main
pull_request:
types: [opened, synchronize, reopened]
jobs:
Expand Down
2 changes: 1 addition & 1 deletion PushSDK.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "PushSDK"
s.version = "1.0.0.45"
s.version = "1.1.1"
s.summary = "SDK for sending push messages to iOS devices."
s.homepage = "https://github.com/GlobalMessageServices/BCS-GMS-SDK-IOS"

Expand Down
4 changes: 4 additions & 0 deletions PushSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
016F280528D2063D00F7D1A1 /* Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 016F280428D2063D00F7D1A1 /* Notifications.swift */; };
01FCADDA28C0C15E00D77FA4 /* PushSDK.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 01FCADD128C0C15E00D77FA4 /* PushSDK.framework */; };
01FCADDF28C0C15E00D77FA4 /* PushSDKTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01FCADDE28C0C15E00D77FA4 /* PushSDKTests.swift */; };
01FCADE028C0C15E00D77FA4 /* PushSDK.h in Headers */ = {isa = PBXBuildFile; fileRef = 01FCADD428C0C15E00D77FA4 /* PushSDK.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -36,6 +37,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
016F280428D2063D00F7D1A1 /* Notifications.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notifications.swift; sourceTree = "<group>"; };
01FCADD128C0C15E00D77FA4 /* PushSDK.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = PushSDK.framework; sourceTree = BUILT_PRODUCTS_DIR; };
01FCADD428C0C15E00D77FA4 /* PushSDK.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PushSDK.h; sourceTree = "<group>"; };
01FCADD928C0C15E00D77FA4 /* PushSDKTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PushSDKTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -147,6 +149,7 @@
01FCADF128C0C27D00D77FA4 /* DataSaver.swift */,
01FCADF228C0C27D00D77FA4 /* Parser.swift */,
01FCADF328C0C27D00D77FA4 /* JsonParser.swift */,
016F280428D2063D00F7D1A1 /* Notifications.swift */,
);
path = core;
sourceTree = "<group>";
Expand Down Expand Up @@ -364,6 +367,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
016F280528D2063D00F7D1A1 /* Notifications.swift in Sources */,
01FCAE0128C0C27D00D77FA4 /* DataStructures.swift in Sources */,
01FCADFB28C0C27D00D77FA4 /* PushConstants.swift in Sources */,
01FCADFD28C0C27D00D77FA4 /* AnswerBuilder.swift in Sources */,
Expand Down
Binary file not shown.
58 changes: 57 additions & 1 deletion PushSDK/PushSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class PushSDK {
private let log = SwiftyBeaver.self
private let parserClassAdapter = PusherKParser.init()
private let pushRestServer = PushAPI.init()
//private let funNotificator = PushNotification.init()
private let funNotificator = PushNotification.init()
let answerBuilder = AnswerBuilder.init()


Expand Down Expand Up @@ -303,4 +303,60 @@ public class PushSDK {
}


//check if notification permitted (Sync procedure)
public func areNotificationsEnabled() -> Bool {

let semaphore = DispatchSemaphore(value: 0)
funNotificator.areNotificationsEnabled { (notificationStatus) in
debugPrint(notificationStatus)
PushKConstants.notificationPermission = notificationStatus
semaphore.signal()
}
semaphore.wait()
return PushKConstants.notificationPermission
}

//check if notification permitted (Async procedure)
public func areNotificationsEnabled(completion:@escaping (Bool)->Swift.Void) {
var notificationStatus: Bool = false
let current = UNUserNotificationCenter.current()
current.getNotificationSettings(completionHandler: { permission in
switch permission.authorizationStatus {
case .authorized:
PushKConstants.logger.debug("User granted permission for notification")
notificationStatus = true
completion(notificationStatus)
break
case .denied:
PushKConstants.logger.debug("User denied notification permission")
notificationStatus = false
completion(notificationStatus)
break
case .notDetermined:
PushKConstants.logger.debug("Notification permission haven't been asked yet")
notificationStatus = false
completion(notificationStatus)
break
case .provisional:
// @available(iOS 12.0, *)
PushKConstants.logger.debug("The application is authorized to post non-interruptive user notifications.")
notificationStatus = true
completion(notificationStatus)
break
case .ephemeral:
// @available(iOS 14.0, *)
PushKConstants.logger.debug("The application is temporarily authorized to post notifications. Only available to app clips.")
notificationStatus = false
completion(notificationStatus)
break
@unknown default:
PushKConstants.logger.debug("Unknow Status")
notificationStatus = false
completion(notificationStatus)
break
}
})
}


}
154 changes: 137 additions & 17 deletions PushSDK/PushSDKFirebase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import FirebaseCore
public class PushSDKFirebase: UIResponder, UIApplicationDelegate {

let pushParser = PusherKParser.init()
//let manualNotificator = PushNotification.init()
let manualNotificator = PushNotification.init()
let answerAdapter = PushServerAnswParser.init()
let pushAdapter = PushSDK.init(basePushURL: PushKConstants.basePushURLactive)
let gcmMessageIDKey = "gcm.message_id"
Expand Down Expand Up @@ -43,7 +43,6 @@ public class PushSDKFirebase: UIResponder, UIApplicationDelegate {


public func fbInitApplication(didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?){
// Override point for customization after application launch.

//fbInitApplication0
PushKConstants.logger.debug("Call fbInitApplication: fbInitApplication0")
Expand Down Expand Up @@ -78,22 +77,81 @@ public class PushSDKFirebase: UIResponder, UIApplicationDelegate {

//fbInitApplication2
PushKConstants.logger.debug("Call fbInitApplication: fbInitApplication2")
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.


// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
PushKConstants.logger.debug("Message ID: \(messageID)")
Messaging.messaging().appDidReceiveMessage(userInfo)

if let gcmMessageID = userInfo[gcmMessageIDKey] {
PushKConstants.logger.debug("gcm essage ID: \(gcmMessageID)")
}

// Print full message.
PushKConstants.logger.debug("fb_init_application_fun2 userInfo: \(userInfo)")
}


public func fbInitApplication(didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
//fbInitApplication3
PushKConstants.logger.debug("Call fbInitApplication: fbInitApplication3")


// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)


if let gcmMessageID = userInfo[gcmMessageIDKey] {
PushKConstants.logger.debug("gcm message ID: \(gcmMessageID)")

}

// Print full message.
PushKConstants.logger.debug("userInfo: \(userInfo)")


guard let jsonData = try? JSONSerialization.data(withJSONObject: userInfo, options: []) else { return }
let jsonString = String(data: jsonData, encoding: .utf8)
PushKConstants.logger.debug("jsonString: \(jsonString ?? "empty")")
let newString = String(jsonString ?? "").replacingOccurrences(of: "\\", with: "", options: .literal, range: nil)
PushKConstants.logger.debug("newString: \(newString)")
let parsedMessage = PushServerAnswParser.messageIncomingJson(strResp: newString)
PushKConstants.logger.debug("parsedMessage: \(parsedMessage)")


if (PushKConstants.enableNotificationFlag == true) {
manualNotificator.preparePushNotification(
imageUrl: String(parsedMessage.message.image?.url ?? ""),
contentTitle: String(parsedMessage.message.title ?? ""),
contentBody: String(parsedMessage.message.body ?? ""),
userInfo: userInfo)

}

//here is delivery report sending
let messageId = parsedMessage.message.messageId
PushKConstants.logger.debug("messageId: \(messageId ?? "messageId error")")

if (PushKConstants.enableDeliveryReportAutoFlag == true && PushKConstants.enableNotificationFlag == true) {
if (PushKConstants.deliveryReportLogicFlag == 1) {
let notificationStatus = pushAdapter.areNotificationsEnabled()

if (notificationStatus == true) {
let drAnswer = self.pushAdapter.pushMessageDeliveryReport(messageId: messageId ?? "")
PushKConstants.logger.debug("delivery report answer: \(drAnswer)")
}

} else if (PushKConstants.deliveryReportLogicFlag == 2)
{
let drAnswer = self.pushAdapter.pushMessageDeliveryReport(messageId: messageId ?? "")
PushKConstants.logger.debug("delivery report answer: \(drAnswer)")
}
}

NotificationCenter.default.post(name: .receivePushKData, object: nil, userInfo: userInfo)

completionHandler(UIBackgroundFetchResult.newData)
}

public func fbInitApplication(didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
//fbInitApplication4
Expand All @@ -111,24 +169,31 @@ extension PushSDKFirebase: UNUserNotificationCenterDelegate{
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo

PushKConstants.logger.debug("userInfo: \(userInfo)")
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
PushKConstants.logger.debug("Message ID: \(messageID)")
Messaging.messaging().appDidReceiveMessage(userInfo)

if let gcmMessageID = userInfo[gcmMessageIDKey] {
PushKConstants.logger.debug("gcm message ID: \(gcmMessageID)")
}

if #available(iOS 14.0, *){
completionHandler([.banner, .list, .sound, .badge])
}else{
completionHandler([.alert, .sound, .badge])
}
completionHandler([.alert, .sound, .badge])
}

public func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
PushKConstants.logger.debug("Message ID: \(messageID)")
PushKConstants.logger.debug("userInfo: \(userInfo)")

if let gcmMessageID = userInfo[gcmMessageIDKey] {
PushKConstants.logger.debug("gcm message ID: \(gcmMessageID)")
}

completionHandler()
}
}
Expand All @@ -140,4 +205,59 @@ extension PushSDKFirebase {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: mySpecialNotificationKey), object: self)
}

public func fb_remote_messaging(remoteMessage: NSDictionary) {
let fdf = remoteMessage as NSDictionary as? [String: AnyObject]
guard let jsonData = (try? JSONSerialization.data(withJSONObject: fdf ?? "", options: [])) else { return }
let jsonString = String(data: jsonData, encoding: .utf8)
let parsedMessage = PushServerAnswParser.messageIncomingJson(strResp: jsonString ?? "")
PushKConstants.logger.debug(parsedMessage)
let parsedMessageUserData = pushParser.messIdParser(messageFromPushServer: jsonString ?? "")
PushKConstants.logger.debug("parsedMessageUserData: \(parsedMessageUserData)")


if (PushKConstants.enableNotificationFlag == true) {
manualNotificator.preparePushNotification(
imageUrl: String(parsedMessage.message.image?.url ?? ""),
contentTitle: String(parsedMessage.message.title ?? ""),
contentBody: String(parsedMessage.message.body ?? ""),
userInfo: fdf ?? [:])
}

switch UIApplication.shared.applicationState {
case .active:
PushKConstants.logger.debug("active")
case .background:
PushKConstants.logger.debug("App is backgrounded.")
PushKConstants.logger.debug("Background time remaining = " +
"\(UIApplication.shared.backgroundTimeRemaining) seconds")
case .inactive:
PushKConstants.logger.debug("App is inactive.")
@unknown default:
PushKConstants.logger.debug("Fatal application error for UIApplication.shared.applicationState")
}

if (PushKConstants.enableDeliveryReportAutoFlag == true && PushKConstants.enableNotificationFlag == true) {
if (PushKConstants.deliveryReportLogicFlag == 1) {
let notificationStatus = pushAdapter.areNotificationsEnabled()

if (notificationStatus == true) {
let drAnswer = self.pushAdapter.pushMessageDeliveryReport(messageId: parsedMessageUserData)
PushKConstants.logger.debug("delivery report answer: \(drAnswer)")
}

} else if (PushKConstants.deliveryReportLogicFlag == 2)
{
let drAnswer = self.pushAdapter.pushMessageDeliveryReport(messageId: parsedMessageUserData)
PushKConstants.logger.debug("delivery report answer: \(drAnswer)")
}
}

NotificationCenter.default.post(name: .receivePushKData, object: nil, userInfo: fdf)
}
public func fb_token_messaging(didReceiveRegistrationToken fcmToken: String) {
PushKConstants.logger.debug("Firebase registration token: \(fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
}

}
Loading

0 comments on commit eb36090

Please sign in to comment.