From 0dd6c0ef90327f24a1b2285e2a730a45980b10be Mon Sep 17 00:00:00 2001 From: Meena Alfons Date: Mon, 5 Aug 2024 10:11:56 +0200 Subject: [PATCH] example: add subscribedPush --- .../AppDelegate.swift | 40 +++++++++++-------- .../ContentView.swift | 15 ++++--- .../PushNotificationsExampleApp.swift | 2 +- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/Examples/PushNotificationsExample/PushNotificationsExample/AppDelegate.swift b/Examples/PushNotificationsExample/PushNotificationsExample/AppDelegate.swift index 3841357..f0c62a2 100644 --- a/Examples/PushNotificationsExample/PushNotificationsExample/AppDelegate.swift +++ b/Examples/PushNotificationsExample/PushNotificationsExample/AppDelegate.swift @@ -5,31 +5,34 @@ // Created by Meena Alfons on 13/12/2023. // -import os +import BirdKit import Foundation import UIKit - -import BirdKit +import os class AppDelegate: NSObject, UIApplicationDelegate { - + static var bird: Bird! static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "AppDelegate") - - func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { + + func application( + _ application: UIApplication, + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil + ) -> Bool { do { Self.bird = try Bird() } catch { Self.logger.info("failed to create bird \(error)") } - + UNUserNotificationCenter.current().delegate = self return true } - + // This method will be called from Button "Ask for notifications permission" static func requestNotificationsPermission() { - UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { granted, error in + UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) { + granted, error in guard granted else { self.logger.log("Notification permission is not gratned") return @@ -40,27 +43,29 @@ class AppDelegate: NSObject, UIApplicationDelegate { } } - + func application( _ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data ) { Self.bird.notifications.registerDevice(deviceToken: deviceToken) - + Self.logger.log("APNS token: \(deviceToken.map { String(format: "%02x", $0) }.joined())") } - - func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { + + func application( + _ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error + ) { Self.logger.log(#function) } - - + func application( _ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void ) { - Self.bird.notifications.handleBackgroundNotification(userInfo: userInfo, completionHandler: completionHandler) + Self.bird.notifications.handleBackgroundNotification( + userInfo: userInfo, completionHandler: completionHandler) } } @@ -68,7 +73,8 @@ extension AppDelegate: UNUserNotificationCenterDelegate { func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, - withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void + withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> + Void ) { Self.logger.log(#function) let options = Self.bird.notifications.willPresentNotification(notification) diff --git a/Examples/PushNotificationsExample/PushNotificationsExample/ContentView.swift b/Examples/PushNotificationsExample/PushNotificationsExample/ContentView.swift index 8ca797a..8cabe0f 100644 --- a/Examples/PushNotificationsExample/PushNotificationsExample/ContentView.swift +++ b/Examples/PushNotificationsExample/PushNotificationsExample/ContentView.swift @@ -5,8 +5,8 @@ // Created by Meena Alfons on 12/12/2023. // -import SwiftUI import BirdKit +import SwiftUI struct ContentView: View { @State var sourceUrl = "" @@ -16,7 +16,7 @@ struct ContentView: View { if sourceUrl != "" { Text("Opened from \(sourceUrl)") } - + let signedIdentity = "change-me-with-sample-signed-identity" Text("Push notifications") Button("Ask for notifications permission") { @@ -29,7 +29,8 @@ struct ContentView: View { ) } Button("Set Signed Identity") { - let signedIdentity = "change-me-with-sample-signed-identity" + let signedIdentity = + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Im1ybjp2MTphcHBsaWNhdGlvbjppZGVudGl0eS1jbGFpbXMtaXNzdWVyOjAxZWNlODAxLWVmNTctNGQwNC1hZGFlLWJjN2U0YWQwOGFmZS8yZmQ5NTdlOS1jNTQwLTRhM2UtYjA5My0wZWE5NmU3NDY1ZjY6MSJ9.eyJpZGVudGlmaWVycyI6W3sia2V5IjoiZW1haWxhZGRyZXNzIiwidmFsdWUiOiJtZWVuYS50ZXN0MkBlbWFpbC5jb20ifV19.in94rLJe1X_7MejgnmV8f284Z8YPxRZcJV8LG5cTbVk" AppDelegate.bird?.contact.identify( signedIdentity: signedIdentity ) @@ -40,10 +41,12 @@ struct ContentView: View { } Text("Contact") Button("Put Attributes") { - let attributes = BirdKit.Attributes().put("displayName", "iOS: push notifications example") - AppDelegate.bird?.contact.putAttributes(attributes: attributes ) + let attributes = BirdKit.Attributes() + .put("displayName", "iOS: push notifications example") + .put("subscribedPush", true) + AppDelegate.bird?.contact.putAttributes(attributes: attributes) } - + } .padding() .onOpenURL(perform: { url in diff --git a/Examples/PushNotificationsExample/PushNotificationsExample/PushNotificationsExampleApp.swift b/Examples/PushNotificationsExample/PushNotificationsExample/PushNotificationsExampleApp.swift index db9953a..29c567a 100644 --- a/Examples/PushNotificationsExample/PushNotificationsExample/PushNotificationsExampleApp.swift +++ b/Examples/PushNotificationsExample/PushNotificationsExample/PushNotificationsExampleApp.swift @@ -10,7 +10,7 @@ import SwiftUI @main struct PushNotificationsExampleApp: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate - + var body: some Scene { WindowGroup { ContentView()