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