I'm using flutter to make an android app and one of the features is to show and save push notifications from Firebase.
My question is How can I save the push notification when opening the app without clicking on the notification.
I implemented the fcm and I can get notifications when app is running (onMessage) / in background (onResume) / terminated (onLaunch),
I can save them locally to a repository using the above functions, however those are only called when user click on the notification, and not when the app is started using its icon.
I tried to save in onBackgroundMessage but it's never called, below is the testing function trying to print something when onBackgroundMessage, it is outside the App class:
Future<void> testing(Map<String, dynamic> message) {
print("BACKGROUND MSG CALLED");
return Future.value(null);
}
below is the code I run in the initState(),
ps. _showNotification will also call saveNotifcation() which works well.
firebaseMessaging.configure(
onMessage: (Map<String, dynamic> msg) async {
print("onMessage called $msg");
await _showNotification(msg);
},
onBackgroundMessage:
Platform.isAndroid ? testing : null,
onLaunch: (Map<String, dynamic> msg) async {
// give time for splashscreen to run and finish before it's done.
SchedulerBinding.instance
.addPostFrameCallback((_) => _showNotification(msg));
},
onResume: (Map<String, dynamic> msg) async {
print("onResume called $msg");
await _showNotification(msg);
},
);
firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(
sound: true, badge: true, alert: true, provisional: true));
firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
print("ios registered");
});
firebaseMessaging.getToken().then((){ print("printing Token: {$token}");});
I searched a lot but no solution could be found. any help will be much appreciated.
I'm using firebase_messaging: ^7.0.0
Flutter doctor -v:
[✓] Flutter (Channel stable, 1.22.0, on Mac OS X 10.15.1 19B88, locale en)
• Flutter version 1.22.0 at /Users/mine/flutter
• Framework revision d408d302e2 (9 days ago), 2020-09-29 11:49:17 -0700
• Engine revision 5babba6c4d
• Dart version 2.10.0
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
• Android SDK at /Users/mine/Library/Android/sdk
• Platform android-29, build-tools 29.0.3
• ANDROID_HOME = /Users/mine/Library/Android/sdk
• Java binary at: /Applications/Android
Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build
1.8.0_242-release-1644-b3-6222593)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 11.3.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 11.3.1, Build version 11C504
• CocoaPods version 1.9.3
[✓] Android Studio (version 4.0)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 46.0.2
• Dart plugin version 193.7361
• Java version OpenJDK Runtime Environment (build
1.8.0_242-release-1644-b3-6222593)
[✓] VS Code (version 1.49.3)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.15.0
[✓] Connected device (1 available)
• Mi 9T Pro (mobile) • 83b222f7 • android-arm64 • Android 10 (API 29)
• No issues found!