Background Notification OnClick With FCM Flutter app open up an empty activity
try adding this intent tag to your activity tag inside AndroidManifest.xml.
<activity>
....
<intent-filter> // <= add this
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
....
and don't forget to add field 'click_action' to your payload when sending FCM. eg : 'click_action' : 'FLUTTER_NOTIFICATION_CLICK'
.
Kei
Updated on January 04, 2023Comments
-
Kei over 1 year
I followed every step in the flutter documentation on how to implement firebase_messaging with the new flutter android embedding v2
I have no problem with receiving the notification in every app state (Foreground/Background/Terminated or Detached). Clicking on the notification works perfectly on other state except terminated.
In terminated state, when I try to click on the notification that is received while the app is detached, it opens up the application but I do not receive any data in getInitialMessage function which I should (according to the doc). Also, when I try to leave the app by pressing the back button, somehow the app shows an extra empty activity underneath it and I'm not sure why this happen. I want to get rid of it.
These problems only occurs if the notification received in terminated state.
The onBackgroundMessage listener works perfectly even in terminated state.
main.dart :
Future<void> _backgroundHandler(RemoteMessage message) async { await Firebase.initializeApp(); print("Message from background : " + message.notification.body); } void main() async { WidgetsFlutterBinding.ensureInitialized(); //init notification service await NotificationService().initNotification(); await Firebase.initializeApp(); FirebaseMessaging.onBackgroundMessage(_backgroundHandler);
home_page_screen.dart : setupInteractedMessage() is called in initState()
Future<void> setupInteractedMessage() async { //Terminated State //Comes in from terminated app's notification FirebaseMessaging.instance.getInitialMessage().then((value) => { if (value != null) {print("ContentAvailable : " + value.contentAvailable.toString())} }); //Foreground state FirebaseMessaging.onMessage.listen((event) { String title = event.notification.title; String body = event.notification.body; int tag = int.parse(event.data["tag"]); //Dont show notification if inside conversation with the sender if (!Provider.of<ConversationBloc>(context, listen: false) .disableNotification(tag)) { NotificationService() .showNotification(tag, title, body, json.encode(event.data)); } else { soundHandler.playOnNewMessageSound(); } print("Received in foreground"); }); //Opened from background notification trigger and handler //It does not work if the app is detached only works in paused state FirebaseMessaging.onMessageOpenedApp.listen((event) { NotificationService().selectNotification(json.encode(event.data)); print("Received in background while the app is paused and not detached"); }); }
Video : https://drive.google.com/file/d/1-De9Buv6ToLB13hIat7i9uZsQOSS2XJO/view?usp=drivesdk
My reference : https://firebase.flutter.dev/docs/messaging/usage
-
Kei about 2 yearsI did try this before and nothing changed, according to the doc I dont have to change anything in the Manifest file anymore if using the latest firebase_messaging package. We only need to put the intent filter if we still use flutter android embedding v1