Skip to content

Instantly share code, notes, and snippets.

@funkyboy
Created June 22, 2018 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funkyboy/ab4d755834e55dc3f9faa0f5bfbb93a6 to your computer and use it in GitHub Desktop.
Save funkyboy/ab4d755834e55dc3f9faa0f5bfbb93a6 to your computer and use it in GitHub Desktop.
Example of Ably push notifications
import UIKit
import Ably
import UserNotifications
let notificationPushReceived = Notification.Name(rawValue: "push-received")
let notificationSubscribed = Notification.Name(rawValue: "subscribed")
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, ARTPushRegistererDelegate {
var window: UIWindow?
var realtime: ARTRealtime!
var channel: ARTRealtimeChannel!
var subscribed = false
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
DispatchQueue.main.async() {
print("didRegisterForRemoteNotificationsWithDeviceToken")
ARTPush.didRegisterForRemoteNotifications(withDeviceToken: deviceToken, realtime: self.getAblyRealtime())
}
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
DispatchQueue.main.async() {
print("didFailToRegisterForRemoteNotificationsWithError")
ARTPush.didFailToRegisterForRemoteNotificationsWithError(error, realtime: self.getAblyRealtime())
}
}
func getOptions() -> ARTClientOptions {
let options = ARTClientOptions()
options.clientId = "push-demo"
options.key = "YOUR_KEY"
options.logLevel = .debug
options.useTokenAuth = true
return options
}
func getAblyRealtime() -> ARTRealtime {
if let realtime = realtime {
return realtime
}
realtime = ARTRealtime(options: getOptions())
return realtime
}
// Callback for realtime.activate
func didActivateAblyPush(_ error: ARTErrorInfo?) {
print("======================= didActivateAblyPush()")
if let error = error {
// Handle error
print("push activation failed, err=\(String(describing: error))")
return
}
print("======================= push activated")
channel = self.realtime.channels.get("push:test")
// Attach to channel, then subscribe device, then broadcast a push which we will eventually receive back.
self.channel.attach() { err in
if let err = err {
print("attach failed, err=\(String(describing: err))")
return
}
print("======================= channel attached")
self.channel.push.subscribeDevice { err in
if let err = err {
// Handle error
print("subscribeDevice failed, err=\(String(describing: err))")
} else {
print("======================= app can now receive notifications on channel \(self.channel.name)")
self.subscribed = true
NotificationCenter.default.post(name: notificationSubscribed, object: nil)
}
}
}
}
// Callback for realtime.deactivate
func didDeactivateAblyPush(_ error: ARTErrorInfo?) {
if let error = error {
// Handle error
print("=========== push de-activation failed", error)
return
}
}
func ablyPushCustomRegister(_ deviceDetails: ARTDeviceDetails?, callback: @escaping ((String?, ARTErrorInfo?) -> Void)) {
print("=========== custom register")
callback("update token", nil)
}
// iOS Push notification handler
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print("=========== received notification: \(userInfo)")
NotificationCenter.default.post(name: notificationPushReceived, object: nil)
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().requestAuthorization() { (ok, err) in
DispatchQueue.main.async() {
UIApplication.shared.registerForRemoteNotifications()
print("=========== after registerForRemoteNotifications")
}
}
self.realtime = self.getAblyRealtime()
self.realtime.connection.on { (stateChange) in
print("=========== connection state change: \(String(describing: stateChange))")
}
self.realtime.connection.on(ARTRealtimeConnectionEvent.connected) { (stateChange) in
print("=========== connected, resetting Ably push")
self.realtime.push.deactivate()
}
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment