Last active
May 21, 2020 06:39
-
-
Save KiranJasvanee/547a879fe55f795eef040e850344418b to your computer and use it in GitHub Desktop.
Includes PubNub push notification configuration & message listener too.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import PubNub | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var pubnub: PubNub! | |
let listener = SubscriptionListener(queue: .main) | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | |
// Override point for customization after application launch. | |
registerForPushNotifications() | |
configurePubNub() | |
return true | |
} | |
// MARK: UISceneSession Lifecycle | |
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { | |
// Called when a new scene session is being created. | |
// Use this method to select a configuration to create the new scene with. | |
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) | |
} | |
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { | |
// Called when the user discards a scene session. | |
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. | |
// Use this method to release any resources that were specific to the discarded scenes, as they will not return. | |
} | |
func registerForPushNotifications() { | |
let notification = UNUserNotificationCenter.current() | |
notification.delegate = self | |
notification.requestAuthorization(options: [.alert, .sound, .badge]) { | |
(granted, error) in | |
print("Permission granted: \(granted)") | |
// 1. Check if permission granted | |
guard granted else { return } | |
// 2. Attempt registration for remote notifications on the main thread | |
DispatchQueue.main.async { | |
UIApplication.shared.registerForRemoteNotifications() | |
} | |
} | |
} | |
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { | |
pubnub.removeAllPushChannelRegistrations(for: deviceToken) { (result) in | |
switch result { | |
case let .success(response): | |
print("all channels removed") | |
case let .failure(error): | |
print("failed to remove channels") | |
} | |
} | |
pubnub.modifyAPNSDevicesOnChannels( | |
byRemoving: [], | |
thenAdding: ["151__1__a067448c-736e-4f9a-ac1e"], | |
device: deviceToken, | |
on: "<Bundle_ID>", | |
environment: .development | |
) { result in | |
switch result { | |
case let .success(response): | |
print("Successful Push Modification Response: \(response)") | |
case let .failure(error): | |
print("Failed Push List Response: \(error.localizedDescription)") | |
} | |
} | |
} | |
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { | |
print("received remote notification") | |
} | |
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { | |
print("failed to register for remote notification") | |
} | |
} | |
extension AppDelegate { | |
func configurePubNub() { | |
let key = "sub-c-bed92252-94e0-11ea-8dc6-429c98eb9bb1" | |
PubNub.log.levels = [.all] | |
PubNub.log.writers = [ConsoleLogWriter(), FileLogWriter()] | |
let config = PubNubConfiguration(publishKey: "", subscribeKey: key) | |
pubnub = PubNub(configuration: config) | |
listener.didReceiveMessage = { message in | |
print("[Message]: \(message)") | |
} | |
listener.didReceiveStatus = { status in | |
switch status { | |
case .success(let connection): | |
if connection == .connected { | |
print("Status Success: \(connection)") | |
} | |
case .failure(let error): | |
print("Status Error: \(error.localizedDescription)") | |
} | |
} | |
pubnub.add(listener) | |
pubnub.subscribe(to: ["151__1__a067448c-736e-4f9a-ac1e"], withPresence: true) | |
} | |
} | |
extension AppDelegate: UNUserNotificationCenterDelegate { | |
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { | |
print("notification: \(response)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment