Skip to content

Commit

Permalink
Updated example project with basic messaging functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
maratal committed Dec 10, 2022
1 parent 2d685ef commit a3e6134
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 32 deletions.
24 changes: 20 additions & 4 deletions Examples/AblyPush/AblyPushExample/AblyHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ import UIKit

class AblyHelper: NSObject {

static let shared = AblyHelper()
static let shared = AblyHelper(clientId: UIDevice.current.name)

private(set) var realtime: ARTRealtime!

private var testChannel: ARTRealtimeChannel {
realtime.channels.get("testChannel")
}

private let key = "" // Your API Key from your app's dashboard

private override init() {
super.init()
private convenience init(clientId: String) {
self.init()
guard key != "" else {
preconditionFailure("Obtain your API key at https://ably.com/accounts/")
}
let options = ARTClientOptions(key: key)
options.clientId = "basic-apns-example"
options.clientId = clientId
options.pushRegistererDelegate = self
self.realtime = ARTRealtime(options: options)
UNUserNotificationCenter.current().delegate = self
Expand Down Expand Up @@ -67,6 +71,18 @@ extension AblyHelper {
print("Publish result: \(error?.localizedDescription ?? "Success")")
}
}

func subscribe(event: String) {
testChannel.subscribe(event) { m in
print("Received a '\(m.name!)' message: '\(m.data!)' from \(m.clientId!)")
}
}

func publish(event: String, message: String) {
testChannel.publish(event, data: message) { _ in
print("Message '\(event)' sent from \(UIDevice.current.name)")
}
}
}

extension AblyHelper: ARTPushRegistererDelegate {
Expand Down
71 changes: 43 additions & 28 deletions Examples/AblyPush/AblyPushExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,54 @@ struct ContentView: View {
var body: some View {
VStack {
Spacer()
Button("Activate") {
AblyHelper.shared.activatePush()
}
.padding()
Button("Dectivate") {
AblyHelper.shared.deactivatePush()
}
.padding()
Button("Print Token") {
AblyHelper.shared.printIdentityToken()
}
.padding()
Button("Device Details") {
AblyHelper.shared.getDeviceDetails { details, error in
deviceDetails = details
deviceDetailsError = error
showDeviceDetailsAlert = true
Group {
Text("Push Notifications")
.padding()
Button("Activate") {
AblyHelper.shared.activatePush()
}
}
.alert(isPresented: $showDeviceDetailsAlert) {
if deviceDetails != nil {
return Alert(title: Text("Device Details"), message: Text("\(deviceDetails!)"))
.padding()
Button("Deactivate") {
AblyHelper.shared.deactivatePush()
}
.padding()
Button("Print Token") {
AblyHelper.shared.printIdentityToken()
}
else if deviceDetailsError != nil {
return Alert(title: Text("Device Details Error"), message: Text("\(deviceDetailsError!)"))
.padding()
Button("Device Details") {
AblyHelper.shared.getDeviceDetails { details, error in
deviceDetails = details
deviceDetailsError = error
showDeviceDetailsAlert = true
}
}
return Alert(title: Text("Device Details Error"), message: Text("Unknown result."))
.alert(isPresented: $showDeviceDetailsAlert) {
if deviceDetails != nil {
return Alert(title: Text("Device Details"), message: Text("\(deviceDetails!)"))
}
else if deviceDetailsError != nil {
return Alert(title: Text("Device Details Error"), message: Text("\(deviceDetailsError!)"))
}
return Alert(title: Text("Device Details Error"), message: Text("Unknown result."))
}
.padding()
Button("Send Push") {
AblyHelper.shared.sendAdminPush(title: "Hello", body: "This push was sent with deviceId")
}
.padding()
}
.padding()
Button("Send Push") {
AblyHelper.shared.sendAdminPush(title: "Hello", body: "This push was sent with deviceId")
Group {
Text("Basic Messaging")
.padding()
Button("Subscribe") {
AblyHelper.shared.subscribe(event: "test")
}
.padding()
Button("Publish") {
AblyHelper.shared.publish(event: "test", message: "Hi there!")
}
}
.padding()
Spacer()
}
}
Expand Down

0 comments on commit a3e6134

Please sign in to comment.