Skip to content

Commit

Permalink
modify sample app to show local pushes
Browse files Browse the repository at this point in the history
This sets up app to test local pushes as well as registering at least 2 other push event handlers in app besides CIO.
  • Loading branch information
levibostian committed Jun 18, 2024
1 parent a02d216 commit be9cc28
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
11 changes: 7 additions & 4 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 @@ -42,7 +45,7 @@ target 'Runner' do
# Uncomment only 1 of the lines below to install a version of the iOS SDK
pod 'CustomerIO/MessagingPushFCM', '~> 2.11' # 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
Expand All @@ -52,7 +55,7 @@ target 'NotificationServiceExtension' do
# Uncomment only 1 of the lines below to install a version of the iOS SDK
pod 'CustomerIO/MessagingPushFCM', '~> 2.11' # 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
18 changes: 18 additions & 0 deletions 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 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
35 changes: 35 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,8 @@ import FirebaseCore
config.logLevel = .debug
}
MessagingPushFCM.initialize(configOptions: nil)

UNUserNotificationCenter.current().delegate = self

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

// Function called when a push notification is clicked or swiped away.
override func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// Track a Customer.io event for testing purposes to more easily track when this function is called.
CustomerIO.shared.track(
name: "push clicked",
data: ["push": [
"title": response.notification.request.content.title,
"body": response.notification.request.content.body,
"userInfo": response.notification.request.content.userInfo
]]
)

completionHandler()
}

override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// Track a Customer.io event for testing purposes to more easily track when this function is called.
CustomerIO.shared.track(
name: "push should show app in foreground",
data: ["push": [
"title": notification.request.content.title,
"body": notification.request.content.body,
"userInfo": notification.request.content.userInfo
]]
)

if #available(iOS 14.0, *) {
completionHandler([.banner, .badge, .sound])
} else {
completionHandler([.alert, .badge, .sound])
}
}
}

extension AppDelegate: MessagingDelegate {
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 be9cc28

Please sign in to comment.