简体   繁体   中英

FCM push notifications FLUTTER when app is terminated

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?

first answer, I hope will be good.

As you can seehere 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

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,
    ),
  );
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM