From 80f0fd0dfd8f7fd28674bfed4c0b5222a14a6950 Mon Sep 17 00:00:00 2001 From: Rehan Date: Thu, 21 Nov 2024 11:23:35 +0500 Subject: [PATCH] chore: init messaging push module --- .github/workflows/build-sample-apps.yml | 3 +-- apps/amiapp_flutter/ios/Env.swift.example | 3 +-- .../NotificationService.swift | 12 +++++------- apps/amiapp_flutter/ios/Runner/AppDelegate.swift | 8 ++++---- apps/amiapp_flutter/lib/main.dart | 6 +++++- apps/amiapp_flutter/lib/src/screens/settings.dart | 2 +- lib/customer_io.dart | 10 +++++++++- 7 files changed, 26 insertions(+), 18 deletions(-) diff --git a/.github/workflows/build-sample-apps.yml b/.github/workflows/build-sample-apps.yml index 04031b8..5669fe3 100644 --- a/.github/workflows/build-sample-apps.yml +++ b/.github/workflows/build-sample-apps.yml @@ -117,8 +117,7 @@ jobs: - name: Setup workspace credentials in iOS environment files run: | cp "ios/Env.swift.example" "ios/Env.swift" - sd 'siteId: String = ".*"' "siteId: String = \"${{ secrets[format('CUSTOMERIO_{0}_WORKSPACE_SITE_ID', matrix.sample-app)] }}\"" "ios/Env.swift" - sd 'apiKey: String = ".*"' "apiKey: String = \"${{ secrets[format('CUSTOMERIO_{0}_WORKSPACE_API_KEY', matrix.sample-app)] }}\"" "ios/Env.swift" + sd 'cdpApiKey: String = ".*"' "cdpApiKey: String = \"${{ secrets[format('CUSTOMERIO_{0}_WORKSPACE_CDP_API_KEY', matrix.sample-app)] }}\"" "ios/Env.swift" # Make sure to fetch dependencies only after updating the version numbers and workspace credentials diff --git a/apps/amiapp_flutter/ios/Env.swift.example b/apps/amiapp_flutter/ios/Env.swift.example index a18f7b4..e94cf0e 100644 --- a/apps/amiapp_flutter/ios/Env.swift.example +++ b/apps/amiapp_flutter/ios/Env.swift.example @@ -1,6 +1,5 @@ import Foundation class Env { - static let siteId: String = "siteid" - static let apiKey: String = "apikey" + static let cdpApiKey: String = "cdpApiKey" } diff --git a/apps/amiapp_flutter/ios/NotificationServiceExtension/NotificationService.swift b/apps/amiapp_flutter/ios/NotificationServiceExtension/NotificationService.swift index fc7aaca..96274b5 100644 --- a/apps/amiapp_flutter/ios/NotificationServiceExtension/NotificationService.swift +++ b/apps/amiapp_flutter/ios/NotificationServiceExtension/NotificationService.swift @@ -13,13 +13,11 @@ class NotificationService: UNNotificationServiceExtension { override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { print("NotificationService didReceive called") - // TODO: Fix SDK initialization - /* - CustomerIO.initialize(siteId: Env.siteId, apiKey: Env.apiKey, region: .US) { config in - config.autoTrackDeviceAttributes = true - config.logLevel = .debug - } - */ + MessagingPushFCM.initializeForExtension( + withConfig: MessagingPushConfigBuilder(cdpApiKey: Env.cdpApiKey) + .logLevel(.debug) + .build() + ) MessagingPush.shared.didReceive(request, withContentHandler: contentHandler) } diff --git a/apps/amiapp_flutter/ios/Runner/AppDelegate.swift b/apps/amiapp_flutter/ios/Runner/AppDelegate.swift index b3f6bfe..0822c94 100644 --- a/apps/amiapp_flutter/ios/Runner/AppDelegate.swift +++ b/apps/amiapp_flutter/ios/Runner/AppDelegate.swift @@ -20,10 +20,10 @@ import FirebaseCore Messaging.messaging().delegate = self - // TODO: Fix MessagingPush initialization - /* - MessagingPushFCM.initialize(configOptions: nil) - */ + MessagingPushFCM.initialize( + withConfig: MessagingPushConfigBuilder() + .build() + ) // Sets a 3rd party push event handler for the app besides the Customer.io SDK and FlutterFire. // Setting the AppDelegate to be the handler will internally use `flutter_local_notifications` to handle the push event. diff --git a/apps/amiapp_flutter/lib/main.dart b/apps/amiapp_flutter/lib/main.dart index 860e2ac..f7cf8d7 100644 --- a/apps/amiapp_flutter/lib/main.dart +++ b/apps/amiapp_flutter/lib/main.dart @@ -31,7 +31,11 @@ void main() async { // Setup flutter_local_notifications plugin to send local notifications and receive callbacks for them. var initSettingsAndroid = const AndroidInitializationSettings("app_icon"); // The default settings will show local push notifications while app in foreground with plugin. - var initSettingsIOS = const DarwinInitializationSettings(); + var initSettingsIOS = const DarwinInitializationSettings( + requestAlertPermission: false, + requestBadgePermission: false, + requestSoundPermission: false, + ); var initSettings = InitializationSettings( android: initSettingsAndroid, iOS: initSettingsIOS, diff --git a/apps/amiapp_flutter/lib/src/screens/settings.dart b/apps/amiapp_flutter/lib/src/screens/settings.dart index a7de9d5..a9d4629 100644 --- a/apps/amiapp_flutter/lib/src/screens/settings.dart +++ b/apps/amiapp_flutter/lib/src/screens/settings.dart @@ -50,7 +50,7 @@ class _SettingsScreenState extends State { @override void initState() { - CustomerIO.instance.pushMessaging.getRegisteredDeviceToken().then((value) => + CustomerIO.pushMessaging.getRegisteredDeviceToken().then((value) => setState(() => _deviceTokenValueController.text = value ?? '')); final cioConfig = widget._customerIOSDK.sdkConfig; diff --git a/lib/customer_io.dart b/lib/customer_io.dart index c4e444a..5ed4376 100644 --- a/lib/customer_io.dart +++ b/lib/customer_io.dart @@ -61,7 +61,15 @@ class CustomerIO { } /// Access push messaging functionality - CustomerIOMessagingPushPlatform get pushMessaging => _pushMessaging; + static CustomerIOMessagingPushPlatform get pushMessaging { + if (_instance == null) { + throw StateError( + 'CustomerIO SDK must be initialized before accessing push module.\n' + 'Call CustomerIO.initialize() first.', + ); + } + return _instance!._pushMessaging; + } /// Access in-app messaging functionality CustomerIOMessagingInAppPlatform get inAppMessaging => _inAppMessaging;