Skip to content

Commit

Permalink
Merge pull request #6 from GlobalMessageServices/RDGMS-696-implement-…
Browse files Browse the repository at this point in the history
…action-buttons-in-i-os-sdk

Rdgms 696 implement action buttons in ios sdk
  • Loading branch information
Oleksii Korniienko authored Nov 17, 2022
2 parents eb36090 + 97cc165 commit 4041aa3
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ jobs:
- name: Build and test app
run: |
pod install
xcodebuild build -workspace PushSDK.xcworkspace -scheme PushSDK -destination 'platform=iOS Simulator,OS=15.5,name=iPhone 13 Pro Max'
xcodebuild test -workspace PushSDK.xcworkspace -scheme PushSDKTests -destination 'platform=iOS Simulator,OS=15.5,name=iPhone 13 Pro Max'
xcodebuild build -workspace PushSDK.xcworkspace -scheme PushSDK -destination 'platform=iOS Simulator,OS=16.0,name=iPhone 13 Pro Max'
xcodebuild test -workspace PushSDK.xcworkspace -scheme PushSDKTests -destination 'platform=iOS Simulator,OS=16.0,name=iPhone 13 Pro Max'
- name: Clean up keychain and provisioning profile
if: ${{ always() }}
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.1.1"
s.version = "1.1.2"
s.summary = "SDK for sending push messages to iOS devices."
s.homepage = "https://github.com/GlobalMessageServices/BCS-GMS-SDK-IOS"

Expand Down
13 changes: 12 additions & 1 deletion PushSDK.xcodeproj/xcshareddata/xcschemes/PushSDK.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,18 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES"
onlyGenerateCoverageForSpecifiedTargets = "YES">
<CodeCoverageTargets>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "01FCADD028C0C15E00D77FA4"
BuildableName = "PushSDK.framework"
BlueprintName = "PushSDK"
ReferencedContainer = "container:PushSDK.xcodeproj">
</BuildableReference>
</CodeCoverageTargets>
<Testables>
<TestableReference
skipped = "NO">
Expand Down
Binary file not shown.
18 changes: 18 additions & 0 deletions PushSDK/PushSDKFirebase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class PushSDKFirebase: UIResponder, UIApplicationDelegate {
@IBOutlet weak var sentNotificationLabel: UILabel?

public func registerForPushNotifications() {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current()
.requestAuthorization(options: [.alert, .sound, .badge]) {
[weak self] granted, error in
Expand Down Expand Up @@ -124,6 +125,8 @@ public class PushSDKFirebase: UIResponder, UIApplicationDelegate {
imageUrl: String(parsedMessage.message.image?.url ?? ""),
contentTitle: String(parsedMessage.message.title ?? ""),
contentBody: String(parsedMessage.message.body ?? ""),
btnText: String(parsedMessage.message.button?.text ?? ""),
btnURL: String(parsedMessage.message.button?.url ?? ""),
userInfo: userInfo)

}
Expand Down Expand Up @@ -190,6 +193,19 @@ extension PushSDKFirebase: UNUserNotificationCenterDelegate{
let userInfo = response.notification.request.content.userInfo
PushKConstants.logger.debug("userInfo: \(userInfo)")

switch response.actionIdentifier{
case "pushKNotificationActionId":
let urlS = userInfo["pushKActionButtonURL"]

if let url = URL(string: urlS as! String){
UIApplication.shared.open(url)
}
break

default:
break
}

if let gcmMessageID = userInfo[gcmMessageIDKey] {
PushKConstants.logger.debug("gcm message ID: \(gcmMessageID)")
}
Expand Down Expand Up @@ -220,6 +236,8 @@ extension PushSDKFirebase {
imageUrl: String(parsedMessage.message.image?.url ?? ""),
contentTitle: String(parsedMessage.message.title ?? ""),
contentBody: String(parsedMessage.message.body ?? ""),
btnText: String(parsedMessage.message.button?.text ?? ""),
btnURL: String(parsedMessage.message.button?.url ?? ""),
userInfo: fdf ?? [:])
}

Expand Down
24 changes: 23 additions & 1 deletion PushSDK/core/Notifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class PushNotification {
contentTitle: String = "",
contentSubtitle: String = "",
contentBody: String,
btnText: String,
btnURL: String,
userInfo: [AnyHashable: Any]
) {
PushKConstants.logger.debug("makePushNotification input: imageUrl: \(imageUrl), timeInterval: \(timeInterval), contentTitle: \(contentTitle), contentSubtitle: \(contentSubtitle), contentBody: \(contentBody)")
Expand All @@ -34,7 +36,12 @@ class PushNotification {
content.subtitle = contentSubtitle
}
content.sound = UNNotificationSound.default
content.categoryIdentifier = "pushKActionCategory"
if(btnURL != "" && btnText != ""){
registerNotificationAction(btnText: btnText)
content.categoryIdentifier = "pushKActionCategory"
content.userInfo = ["pushKActionButtonURL" : btnURL]

}

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterval, repeats: false)

Expand Down Expand Up @@ -91,6 +98,21 @@ class PushNotification {
})
}

func registerNotificationAction(btnText: String){
let acceptAction = UNNotificationAction(identifier: "pushKNotificationActionId",
title: btnText,
options: [UNNotificationActionOptions.foreground])

let pushCategory =
UNNotificationCategory(identifier: "pushKActionCategory",
actions: [acceptAction],
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder: "",
options: .customDismissAction)

UNUserNotificationCenter.current().setNotificationCategories([pushCategory])
}

func areNotificationsEnabled(completion:@escaping (Bool)->Swift.Void) {
var notificationStatus: Bool = false
let current = UNUserNotificationCenter.current()
Expand Down
2 changes: 1 addition & 1 deletion PushSDK/settings/PushConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public struct PushKConstants {

let kOSType = "ios"
static let serverSdkVersion = "2.3"
static let sdkVersion = "1.1.1"
static let sdkVersion = "1.1.2"
static let devOSVersion = UIDevice.current.systemVersion
static let deviceType = "\(UIDevice.current.model)"
static let deviceType2 = "\(UIDevice.current.batteryLevel)"
Expand Down
38 changes: 33 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ To open your project run $ open ProjectName.xcworkspace<br>
More about Cocoapods and Podfile here - https://cocoapods.org, https://guides.cocoapods.org/using/the-podfile.html and https://guides.cocoapods.org/using/using-cocoapods.html.

### Add sdk to your project.
Last actual SDK version: 1.1.1<br>
Last actual SDK version: 1.1.2<br>
To integrate PushSDK to your project with COCOAPODS (https://guides.cocoapods.org/using/the-podfile.html) add the next line in Podfile.<br>
pod 'PushSDK', :git => 'https://github.com/GlobalMessageServices/BCS-GMS-SDK-IOS', :branch => 'gmsapi'
pod 'PushSDK', :git => 'https://github.com/GlobalMessageServices/BCS-GMS-SDK-IOS', :branch => 'main'

***
Important ! Before start using SDK, configure firebase project first and create App Id and APNS key.
Expand Down Expand Up @@ -88,7 +88,7 @@ class ViewController: UIViewController {
super.viewDidLoad()
//register in notification center
NotificationCenter.default.addObserver(self, selector: #selector(onReceiveFromPushServer(_:)), name: .receivePushKData, object: nil)
UNUserNotificationCenter.current().delegate = self
//UNUserNotificationCenter.current().delegate = self
}
}

Expand All @@ -114,21 +114,49 @@ extension ViewController: UNUserNotificationCenterDelegate {
}
}

extension AppDelegate: UNUserNotificationCenterDelegate{
extension AppDelegate: UNUserNotificationCenterDelegate{

public func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//If you don't want to show notification when app is open, do something here else and make a return here.
completionHandler([.alert, .sound, .badge])
}


// For handling tap and user actions
public func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {



let userInfo = response.notification.request.content.userInfo
// handling action button click
switch response.actionIdentifier{
case "pushKNotificationActionId":
let urlS = userInfo["pushKActionButtonURL"]

if let url = URL(string: urlS as! String){
UIApplication.shared.open(url)
}
break

default:
break
}

completionHandler()
}
}
```

### !!! In the code above handling of tap and user actions are going in AppDelegate extension.
In case you want to handlig it in ViewController extension - commit the next line in AppDelegate.swift:
```swift
UNUserNotificationCenter.current().delegate = self
```
and uncommit the same line in ViewController.swift.

## An example of registration a device:
* Init pPushSDK
```swift
Expand Down

0 comments on commit 4041aa3

Please sign in to comment.