|
1 |
| -// Example of SimplePersonalizationSDK initialization with autoSendPushToken flag set to true |
2 |
| - |
3 |
| -let personalizationSDK = SimplePersonalizationSDK(autoSendPushToken: true) |
4 |
| - |
5 |
| -// Example of initializing NotificationService |
6 |
| -let notificationService = NotificationService(sdkInstance: personalizationSDK) |
7 |
| - |
8 |
| -// Example of integrating NotificationService in AppDelegate |
9 |
| -func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { |
10 |
| - // Initialize NotificationService |
11 |
| - let notificationService = NotificationService(sdkInstance: personalizationSDK) |
12 |
| - |
13 |
| - // Request permission for remote notifications |
14 |
| - UIApplication.shared.registerForRemoteNotifications() |
15 |
| - |
16 |
| - return true |
| 1 | +# The first step is setting `autoSendPushToken = true` during SDK initialization |
| 2 | +# `NotificationService` object is initializing in `SimplePersonalizationSDK` |
| 3 | +# `NotificationService` will check `autoSendPushToken ` value. If true - the last sending date check. |
| 4 | +# Check results: nil - token will be sent to the server and the date will be saved. More than a week - the token will be sent and the token sending date will be renewed. |
| 5 | +# Less than a week - token won`t be sent. |
| 6 | +# If `autoSendPushToken` value is false NotificationService won`t send the token to the server |
| 7 | + |
| 8 | +@UIApplicationMain |
| 9 | + |
| 10 | +class AppDelegate:UIResponder,UIApplicationDelegate { |
| 11 | + var window:UIWindow? |
| 12 | + |
| 13 | + let gcmMessageIDKey = "gcm.message_id" |
| 14 | + var sdk:PersonalizationSDK! |
| 15 | + var sdkAdditionalInit:PersonalizationSDK! |
| 16 | + var NotificationService:NotificationServiceProtocol? |
| 17 | + |
| 18 | + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { |
| 19 | + |
| 20 | + print("A. init firebase SDK") |
| 21 | + FirebaseApp.configure() |
| 22 | + Messaging.messaging().delegate = self |
| 23 | + print("======") |
| 24 | + |
| 25 | + print("0 init SDK") |
| 26 | + sdk = createPersonalizationSDK(shopId:String, enableLogs: Boolean, { error in |
| 27 | + didToken = self.sdk.getDeviceId() |
| 28 | + globalSDK = self.sdk |
| 29 | + NotificationCenter.default.post(name: globalSDKNotificationNameMainInit, object: nil) |
| 30 | + }) |
| 31 | + notificationService = NotificationService(sdk: sdk) |
| 32 | + notificationService?.pushActionDelegate = self |
| 33 | + |
| 34 | + } |
17 | 35 | }
|
18 | 36 |
|
19 |
| -// Example of implementing UNUserNotificationCenterDelegate |
20 |
| -class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { |
| 37 | +# During initialization `NotificationService ` will ask permission from the user to send notifications - UIApplication.shared.registerForRemoteNotifications() |
| 38 | +# The client will have to sign protocols - UNUserNotificationCenterDelegate and implement the `application` method and call the `application` method inside of the NotificationService object application |
21 | 39 |
|
22 |
| - func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { |
23 |
| - // Initialize NotificationService |
24 |
| - let notificationService = NotificationService(sdkInstance: personalizationSDK) |
| 40 | +extension AppDelegate: UNUserNotificationCenterDelegate { |
| 41 | + func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){ |
| 42 | + let tokenParts = deviceToken.map { data -> String in String(format: "%02.2hhx",data) } |
25 | 43 |
|
26 |
| - // Request permission for remote notifications |
27 |
| - UIApplication.shared.registerForRemoteNotifications() |
| 44 | + let token = tokenParts.joined() |
| 45 | + pushGlobalToken = token |
| 46 | + print("Push Token", pushGlobalToken) |
| 47 | + userDefaults.standard.set(token,forkey: "pushGlobalToken") |
28 | 48 |
|
29 |
| - return true |
30 |
| - } |
| 49 | + Messaging.messaging().apnsToken = deviceToken |
| 50 | + |
| 51 | + notificationService?didRegisterForRemoteNotificationsWithDeviceToken(deviceToken: deviceToken) |
31 | 52 |
|
32 |
| - // Implement method for receiving device token |
33 |
| - func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { |
34 |
| - // Pass device token to NotificationService |
35 |
| - notificationService.didRegisterForRemoteNotifications(with: deviceToken) |
36 | 53 | }
|
37 | 54 | }
|
38 |
| -//TODO change examples so they would look like code on screenshot attached to the task |
|
0 commit comments