I am trying to receive push notifications on ANDROID from the Firebase console when the app is terminated but I get nothing (I have onResume, onMessage and onLaunch callback listeners but I don’t even want to handle them in the app yet) I’d just like to get them to show In the tray or lock screen. Is there some Android configuration required?
-
1You can test it from Postman: Check it out Test FCM Notification with PostmanPratik Butani– Pratik Butani2021-03-11 13:22:52 +00:00Commented Mar 11, 2021 at 13:22
-
there is no onResume, onLaunch, i think you are using older version of firebase_messaging, try with latest version. and also firebase recommend to use HTTP V1 instead of legacy api firebase.google.com/docs/cloud-messaging/migrate-v1Jaydeep chatrola– Jaydeep chatrola2022-06-14 10:01:40 +00:00Commented Jun 14, 2022 at 10:01
2 Answers
first answer, I hope will be good.
As you can see here notifications are handled with onBackgroundMessage even if the app is terminated. Maybe your problem is that you are not sending a Notification but a "Data only" message. In that case you have to set the field "priority": "high"
Also, with the latest release of Flutter and firebase_messaging the methods onResume, onMessage and onLaunch are deprecated. You should substitute them with onMessage, onMessageOpened, onBackground, but you can find definitely more info with the official documentation
Comments
Just an addition to @samUser1 answer. When app is terminated this code will handle events.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
And outside main function parse it. The example below uses AwesomeNotifications package. https://github.com/rafaelsetragni/awesome_notifications/blob/master/example/lib/main.dart
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
final res = await AwesomeNotifications().createNotification(
content: NotificationContent(
id: Random().nextInt(1000000),
channelKey: 'channel_name',
title: 'Hello!',
body: 'World!',
notificationLayout: NotificationLayout.Default,
),
);
}