Skip to content

Commit

Permalink
chore: test multiple push event handlers in sample app (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
levibostian authored Jun 28, 2024
1 parent a02d216 commit 6166311
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apps/amiapp_flutter/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>11.0</string>
<string>12.0</string>
</dict>
</plist>
15 changes: 9 additions & 6 deletions apps/amiapp_flutter/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Import Ruby functions from native SDK to more easily allow installing non-production SDK builds.
# -------------
# This code only used internally for Customer.io testing
require 'open-uri'
IO.copy_stream(URI.open('https://raw.githubusercontent.com/customerio/customerio-ios/main/scripts/cocoapods_override_sdk.rb'), "/tmp/override_cio_sdk.rb")
IO.copy_stream(URI.open('https://raw.githubusercontent.com/customerio/customerio-ios/v2/scripts/cocoapods_override_sdk.rb'), "/tmp/override_cio_sdk.rb")
load "/tmp/override_cio_sdk.rb"
# end of internal Customer.io testing code
# -------------

# Uncomment this line to define a global platform for your project
platform :ios, '13.0'
Expand Down Expand Up @@ -40,19 +43,19 @@ target 'Runner' do
use_modular_headers!

# Uncomment only 1 of the lines below to install a version of the iOS SDK
pod 'CustomerIO/MessagingPushFCM', '~> 2.11' # install production build
pod 'CustomerIO/MessagingPushFCM', '~> 2.13' # 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_git_branch(branch_name: 'name-of-ios-sdk-branch', is_app_extension: false, push_service: "fcm")
# install_non_production_ios_sdk_git_branch(branch_name: 'levi/v2-multiple-push-handlers', is_app_extension: false, push_service: "fcm")

flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end

target 'NotificationServiceExtension' do
use_frameworks!
# Uncomment only 1 of the lines below to install a version of the iOS SDK
pod 'CustomerIO/MessagingPushFCM', '~> 2.11' # install production build
pod 'CustomerIO/MessagingPushFCM', '~> 2.13' # 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_git_branch(branch_name: 'name-of-ios-sdk-branch', is_app_extension: true, push_service: "fcm")
# install_non_production_ios_sdk_git_branch(branch_name: 'levi/v2-multiple-push-handlers', is_app_extension: true, push_service: "fcm")
end

post_install do |installer|
Expand Down
20 changes: 19 additions & 1 deletion apps/amiapp_flutter/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
97C146EB1CF9000F007C117D /* Frameworks */,
97C146EC1CF9000F007C117D /* Resources */,
3B06AD1E1E4923F5004D2608 /* Thin Binary */,
9A84F275275010D6B13C39B0 /* [CP] Copy Pods Resources */,
);
buildRules = (
);
Expand All @@ -240,7 +241,7 @@
attributes = {
BuildIndependentTargetsInParallel = YES;
LastSwiftUpdateCheck = 1420;
LastUpgradeCheck = 1300;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
302D7E30295BEE78005CBB29 = {
Expand Down Expand Up @@ -369,6 +370,23 @@
shellPath = /bin/sh;
shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" build";
};
9A84F275275010D6B13C39B0 /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-resources-${CONFIGURATION}-input-files.xcfilelist",
);
name = "[CP] Copy Pods Resources";
outputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-resources-${CONFIGURATION}-output-files.xcfilelist",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-resources.sh\"\n";
showEnvVarsInLog = 0;
};
FB05793FCEE3100A134F8366 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1300"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
4 changes: 4 additions & 0 deletions apps/amiapp_flutter/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import FirebaseCore
config.logLevel = .debug
}
MessagingPushFCM.initialize(configOptions: nil)

// 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.
UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate

return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
Expand Down
21 changes: 21 additions & 0 deletions apps/amiapp_flutter/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:customer_io/customer_io.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

import 'src/app.dart';
import 'src/auth.dart';
Expand All @@ -8,6 +10,8 @@ import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'firebase_options.dart';

final FlutterLocalNotificationsPlugin localNotificationsPlugin = FlutterLocalNotificationsPlugin();

void main() async {
WidgetsFlutterBinding.ensureInitialized();

Expand All @@ -24,6 +28,23 @@ void main() async {
sound: true,
);

// 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 initSettings = InitializationSettings(
android: initSettingsAndroid,
iOS: initSettingsIOS,
);
await localNotificationsPlugin.initialize(
initSettings,
onDidReceiveNotificationResponse: (NotificationResponse notificationResponse) async {
// Callback from `flutter_local_notifications` plugin for when a local notification is clicked.
// Unfortunately, we are only able to get the payload object for the local push, not anything else such as title or body.
CustomerIO.track(name: "local push notification clicked", attributes: {"payload": notificationResponse.payload});
}
);

// Load SDK configurations
await dotenv.load(fileName: ".env");
// Wait for user state to be updated
Expand Down
17 changes: 16 additions & 1 deletion apps/amiapp_flutter/lib/src/screens/dashboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

import '../auth.dart';
import '../components/container.dart';
Expand Down Expand Up @@ -238,7 +239,7 @@ class _ActionList extends StatelessWidget {
style: FilledButton.styleFrom(
minimumSize: sizes.buttonDefault(),
),
onPressed: () {
onPressed: () async {
switch (item) {
case _ActionItem.randomEvent:
_sendRandomEvent(context);
Expand All @@ -249,6 +250,13 @@ class _ActionList extends StatelessWidget {
case _ActionItem.signOut:
authState.signOut();
break;
case _ActionItem.showLocalPush:
const NotificationDetails notificationDetails =
NotificationDetails();
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin.show(0, 'Show Local Push', 'Show Local Push Button', notificationDetails, payload: 'item x');
break;
default:
final Screen? screen = item.targetScreen();
if (screen != null) {
Expand Down Expand Up @@ -276,6 +284,7 @@ enum _ActionItem {
deviceAttributes,
profileAttributes,
showPushPrompt,
showLocalPush,
signOut,
}

Expand All @@ -292,6 +301,8 @@ extension _ActionNames on _ActionItem {
return 'Set Profile Attribute';
case _ActionItem.showPushPrompt:
return 'Show Push Prompt';
case _ActionItem.showLocalPush:
return 'Show local push';
case _ActionItem.signOut:
return 'Log Out';
}
Expand All @@ -309,6 +320,8 @@ extension _ActionNames on _ActionItem {
return 'Profile Attribute Button';
case _ActionItem.showPushPrompt:
return 'Show Push Prompt Button';
case _ActionItem.showLocalPush:
return 'Show Local Push Button';
case _ActionItem.signOut:
return 'Log Out Button';
}
Expand All @@ -326,6 +339,8 @@ extension _ActionNames on _ActionItem {
return Screen.profileAttributes;
case _ActionItem.showPushPrompt:
return null;
case _ActionItem.showLocalPush:
return null;
case _ActionItem.signOut:
return null;
}
Expand Down
1 change: 1 addition & 0 deletions apps/amiapp_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies:
package_info_plus: ^4.0.2
firebase_core: ^2.24.2
firebase_messaging: ^14.7.9
flutter_local_notifications: ^17.1.2

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 6166311

Please sign in to comment.