Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mobile App] Issue 538: Enable push notifications for iOS #1032

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions recipients_app/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
11DBF1B94017AFD6B700EDC9 /* Pods-Runner.profile-dev.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile-dev.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile-dev.xcconfig"; sourceTree = "<group>"; };
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
1795934F2D497A1400356200 /* Runner.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Runner.entitlements; sourceTree = "<group>"; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
4689CB0ACE2A42B4652180C6 /* Pods-Runner.debug-prod.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug-prod.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug-prod.xcconfig"; sourceTree = "<group>"; };
4C7F7F600999F591E8DC71F9 /* Pods-Runner.release-dev.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release-dev.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release-dev.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -124,6 +125,7 @@
97C146F01CF9000F007C117D /* Runner */ = {
isa = PBXGroup;
children = (
1795934F2D497A1400356200 /* Runner.entitlements */,
97C146FA1CF9000F007C117D /* Main.storyboard */,
97C146FD1CF9000F007C117D /* Assets.xcassets */,
97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */,
Expand Down Expand Up @@ -440,6 +442,7 @@
APP_DISPLAY_NAME = "Social Income";
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
Expand Down Expand Up @@ -529,6 +532,7 @@
APP_DISPLAY_NAME = "Social Income";
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
Expand Down Expand Up @@ -615,6 +619,7 @@
APP_DISPLAY_NAME = "Social Income";
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
Expand Down Expand Up @@ -706,6 +711,7 @@
APP_DISPLAY_NAME = "Stage Social Income";
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
Expand Down Expand Up @@ -795,6 +801,7 @@
APP_DISPLAY_NAME = "Stage Social Income";
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
Expand Down Expand Up @@ -881,6 +888,7 @@
APP_DISPLAY_NAME = "Stage Social Income";
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
Expand Down Expand Up @@ -967,6 +975,7 @@
APP_DISPLAY_NAME = "Dev Social Income";
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
Expand Down Expand Up @@ -1111,6 +1120,7 @@
APP_DISPLAY_NAME = "Dev Social Income";
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
Expand Down Expand Up @@ -1147,6 +1157,7 @@
APP_DISPLAY_NAME = "Dev Social Income";
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
Expand Down
173 changes: 89 additions & 84 deletions recipients_app/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
@@ -1,88 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>$(APP_DISPLAY_NAME)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>Social Income</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>$(FLUTTER_BUILD_NAME)</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.387167059331-f39tjvqes9cq3l51lv1mas4te28nct9b</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.51495651779-tge57s75sh38pcd76g03lpfc1prv8rvd</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.51495651779-m8sno9cgf85q5u8pb3t0kkoh4crd2p0a</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>$(FLUTTER_BUILD_NUMBER)</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>MinimumOSVersion</key>
<string>13.0</string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>tel</string>
</array>
<key>UIStatusBarHidden</key>
<false/>
</dict>
<dict>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>$(APP_DISPLAY_NAME)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>Social Income</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>$(FLUTTER_BUILD_NAME)</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.387167059331-f39tjvqes9cq3l51lv1mas4te28nct9b</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.51495651779-tge57s75sh38pcd76g03lpfc1prv8rvd</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.51495651779-m8sno9cgf85q5u8pb3t0kkoh4crd2p0a</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>$(FLUTTER_BUILD_NUMBER)</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>tel</string>
</array>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>MinimumOSVersion</key>
<string>13.0</string>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
<string>fetch</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIStatusBarHidden</key>
<false/>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
</dict>
</plist>
5 changes: 4 additions & 1 deletion recipients_app/ios/Runner/Runner.entitlements
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
<dict>
<key>aps-environment</key>
<string>development</string>
KarinBerg marked this conversation as resolved.
Show resolved Hide resolved
</dict>
</plist>
76 changes: 59 additions & 17 deletions recipients_app/lib/data/repositories/messaging_repository.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// ignore_for_file: unreachable_from_main
import "dart:developer";

import "package:firebase_messaging/firebase_messaging.dart";

/// This must be a top level function in order to work
Future<void> handleBackgroundMessage(RemoteMessage message) async {
log("PUSHNOTIFICATION title: ${message.notification?.title}");
log("PUSHNOTIFICATION body: ${message.notification?.body}");
log("PUSHNOTIFICATION data: ${message.data}");
@pragma("vm:entry-point")
KarinBerg marked this conversation as resolved.
Show resolved Hide resolved
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
log("PUSHNOTIFICATION: Terminated Background Message");
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
//await Firebase.initializeApp();
MessagingRepository.logRemoteMessage(message);
}

class MessagingRepository {
Expand All @@ -16,7 +19,7 @@ class MessagingRepository {
required this.messaging,
}) {
messaging.onTokenRefresh.listen((event) {
log("token refreshed: $event");
log("PUSHNOTIFICATION: FCM Token refreshed: $event");
});
}

Expand All @@ -26,31 +29,70 @@ class MessagingRepository {
);

/// This is needed for testing and specific targeting of devices
await messaging.getToken();
final token = await getToken();
log("PUSHNOTIFICATION: Current FCM token: $token");

/// Get any messages which caused the application to open from a terminated state.
final initialMessage = await messaging.getInitialMessage();
if (initialMessage != null){
_handleInitialMessage(initialMessage);
}

// Set function which is called when the app is in the background or terminated.
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

// Set function which is called to handle any interaction when the app is in the background (not terminated) via a Stream listener
FirebaseMessaging.onMessageOpenedApp.listen(_handleBackgroundMessage);

/// handle background messages when app is not in foreground
FirebaseMessaging.onBackgroundMessage(handleBackgroundMessage);
// Set a function which is called to handle any interaction when the app is in the foreground via a Stream listener
// For showing local notifications another package is needed: flutter_local_notifications => https://pub.dev/packages/flutter_local_notifications#-supported-platforms
FirebaseMessaging.onMessage.listen(_handleForegroundMessage);

/// Notification messages which arrive while the app is in foreground will not display a visible notification by default.
/// To override this behavior on iOS, we update the presentation options for the app to change this behaviour.
/// To override this behavior on Android, you must create a "High Priority" notification channel which can be done
/// with the above mentioned package "flutter_local_notifications".
/// See also blog article: https://medium.com/@ChanakaDev/android-channels-in-flutter-003907b151e5
messaging.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
}

/// handle messages when app is in foreground
/// if we want to display notifications while the app is open
/// we have to handle them ourselfes with for eg using
/// flutter_local_notifications and then we can use it like this:
// final initialMessage = await messaging.getInitialMessage();
// handleMessage(initialMessage);
void _handleForegroundMessage(RemoteMessage message) {
// TODO implement displaying push notifications if app is open
log("PUSHNOTIFICATION: Foreground Message received");
logRemoteMessage(message);
}

void handleMessage(RemoteMessage? message) {
if (message == null) return;
void _handleBackgroundMessage(RemoteMessage message) {
// TODO implement handling of push notifications if app is in background
log("PUSHNOTIFICATION: Background Message received");
logRemoteMessage(message);
}

void _handleInitialMessage(RemoteMessage message) {
// TODO implement displaying push notifications if app is open
log("PUSHNOTIFICATION: Initial Message received");
logRemoteMessage(message);
}

Future<String?> getToken() async {
return await messaging.getToken();
}

static void logRemoteMessage(RemoteMessage message) {
if (message.notification != null) {
log("RemoteMessage message.notification: ${message.notification}");
log('RemoteMessage message.notification?.title: ${message.notification?.title ?? ''}');
log('RemoteMessage message.notification?.body: ${message.notification?.body ?? ''}');
}
log("RemoteMessage message.messageType: ${message.messageType}");
log("RemoteMessage message.messageId: ${message.messageId}");
log("RemoteMessage message.category: ${message.category}");
log("RemoteMessage message.from: ${message.from}");
log("RemoteMessage message.data: ${message.data}");
log("RemoteMessage message.sentTime: ${message.sentTime}");
}
}
2 changes: 1 addition & 1 deletion recipients_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: app
description: Social Income App

publish_to: "none"
version: 1.1.14+33
version: 1.1.14+35

environment:
sdk: ">=3.6.0 <4.0.0"
Expand Down
Loading