Skip to content

Commit

Permalink
test: update sample app with 3rd party SDK that handles push
Browse files Browse the repository at this point in the history
  • Loading branch information
levibostian committed Dec 14, 2023
1 parent 6fbd92d commit c4ed7ae
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 54 deletions.
4 changes: 2 additions & 2 deletions apps/amiapp_flutter/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ target 'Runner' do

# Uncomment only 1 of the lines below to install a version of the iOS SDK
pod 'CustomerIO/MessagingPushFCM', '~> 2' # install production build
# install_non_production_ios_sdk_local_path(local_path: '~/code/customerio-ios/', is_app_extension: false, push_service: "fcm")
install_non_production_ios_sdk_local_path(local_path: '~/code/customerio-ios/', is_app_extension: false, push_service: "fcm")
# install_non_production_ios_sdk_git_branch(branch_name: 'name-of-ios-sdk-branch', is_app_extension: false, push_service: "fcm")

flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
Expand All @@ -51,7 +51,7 @@ target 'NotificationServiceExtension' do
use_frameworks!
# Uncomment only 1 of the lines below to install a version of the iOS SDK
pod 'CustomerIO/MessagingPushFCM', '~> 2' # install production build
# install_non_production_ios_sdk_local_path(local_path: '~/code/customerio-ios/', is_app_extension: true, push_service: "fcm")
install_non_production_ios_sdk_local_path(local_path: '~/code/customerio-ios/', is_app_extension: true, push_service: "fcm")
# install_non_production_ios_sdk_git_branch(branch_name: 'name-of-ios-sdk-branch', is_app_extension: true, push_service: "fcm")
end

Expand Down
37 changes: 2 additions & 35 deletions apps/amiapp_flutter/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,13 @@ import FirebaseCore
) -> Bool {
GeneratedPluginRegistrant.register(with: self)

FirebaseApp.configure()
Messaging.messaging().delegate = self

CustomerIO.initialize(siteId: Env.siteId, apiKey: Env.apiKey, region: .US) { config in
config.autoTrackDeviceAttributes = true
config.logLevel = .debug
}

// Set FCM messaging delegate
Messaging.messaging().delegate = self

let center = UNUserNotificationCenter.current()
center.delegate = self

// Register for push notification
UIApplication.shared.registerForRemoteNotifications()
MessagingPushFCM.initialize(configOptions: nil)

return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
Expand All @@ -40,31 +32,6 @@ import FirebaseCore
override func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
MessagingPush.shared.application(application, didFailToRegisterForRemoteNotificationsWithError: error)
}

override func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
let handled = MessagingPush.shared.userNotificationCenter(center, didReceive: response,
withCompletionHandler: completionHandler)

// If the Customer.io SDK does not handle the push, it's up to you to handle it and call the
// completion handler. If the SDK did handle it, it called the completion handler for you.
if !handled {
completionHandler()
}
}

// OPTIONAL: If you want your push UI to show even with the app in the foreground, override this function and call
// the completion handler.
override func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
completionHandler([.alert, .badge, .sound])
}
}

extension AppDelegate: MessagingDelegate {
Expand Down
9 changes: 9 additions & 0 deletions apps/amiapp_flutter/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'src/app.dart';
import 'src/auth.dart';

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'firebase_options.dart';

void main() async {
Expand All @@ -15,6 +16,14 @@ void main() async {
options: DefaultFirebaseOptions.currentPlatform,
);

/// Update the iOS foreground notification presentation options to allow
/// heads up notifications.
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);

// Load SDK configurations
await dotenv.load(fileName: ".env");
// Wait for user state to be updated
Expand Down
27 changes: 10 additions & 17 deletions apps/amiapp_flutter/lib/src/screens/dashboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:developer';

import 'package:customer_io/customer_io.dart';
import 'package:customer_io/customer_io_inapp.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
Expand Down Expand Up @@ -56,34 +57,26 @@ class _DashboardScreenState extends State<DashboardScreen> {
inAppMessageStreamSubscription =
CustomerIO.subscribeToInAppEventListener(handleInAppEvent);

setupFCMListeners();

super.initState();
}


// Setup 3rd party SDK, flutter-fire.
// We install this SDK into sample app to make sure the CIO SDK behaves as expected when there is another SDK installed that handles push notifications.
Future<void> setupFCMListeners() async {
// Important that a 3rd party SDK can receive callbacks when a push is clicked on.
// ...while app was killed.
RemoteMessage? initialMessage = await FirebaseMessaging.instance.getInitialMessage();
if (initialMessage != null) {
log("Got a message while app was terminated. ${initialMessage.data}");
}
// Setup 3rd party SDK, flutter-fire.
// We install this SDK into sample app to make sure the CIO SDK behaves as expected when there is another SDK installed that handles push notifications.
FirebaseMessaging.instance.getInitialMessage().then((initialMessage) {
CustomerIO.track(name: "push clicked", attributes: {"push": initialMessage?.notification?.title, "app-state": "killed"});
});

// ...while app was in the background (but not killed).
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
log("Got a message while app was in the background. ${message.data}");
CustomerIO.track(name: "push clicked", attributes: {"push": message.notification?.title, "app-state": "background"});
});

// Important that a 3rd party SDK can receive callbacks when a push is received while app in background.
//
// Note: A push will not be shown on the device while app is in foreground. This is a FCM behavior, not a CIO SDK behavior.
// If you send a push using Customer.io with the FCM service setup in Customer.io, the push will be shown on the device.
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
log("Got a message while app was in the foreground. ${message.data}");
CustomerIO.track(name: "push received", attributes: {"push": message.notification?.title, "app-state": "foreground"});
});

super.initState();
}

void handleInAppEvent(InAppEvent event) {
Expand Down

0 comments on commit c4ed7ae

Please sign in to comment.