FCM notification on locked screen Flutter

1,301

What i can recommend you is change the function, you can implement something like this:

void subscribeChannel(String data) async =>
    await FirebaseMessaging.instance.subscribeToTopic(data);

void unsubscribeChannel(String channelId) async =>
    await FirebaseMessaging.instance.unsubscribeFromTopic(channelId);

void listenNotificationEvents() {
  FirebaseMessaging.instance.getInitialMessage().then(
    (value) async {
      if (value != null) setLocalNotification(value);
    },
  );
  FirebaseMessaging.onMessage.listen((event) {
    if (event.data.isNotEmpty) setLocalNotification(event);
  });
  FirebaseMessaging.onMessageOpenedApp.listen((event) {
    if (event.data.isNotEmpty) changeScreen(event.data);
  });
}

Those functions are basically to let firebase setup everything and enable also receive notifications even when the app has been killed or is on background.

and on your MaterialApp class you need to use this.

FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
try {
  FirebaseMessaging.onBackgroundMessage(
      (message) => onBackgroundMessage(message.data));
} catch (_) {}

finally, if you receive a notification, and the app is open, firebase will execute everything in the block code, so the final change is to setup inside the onMessage the setup of your local notification.

Please let me know if this help you.

Share:
1,301
Niraj Savaliya
Author by

Niraj Savaliya

Updated on November 20, 2022

Comments

  • Niraj Savaliya
    Niraj Savaliya over 1 year

    I developing one app in that I not able to receive FCM notification when phone is locked.

    _fcm.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        AwesomeNotifications().createNotification(
            content: NotificationContent(
                id: 100,
                channelKey: "basic_channel",
                title: message['notification']['title'],
                body: message['notification']['body'],
                showWhen: true,
                autoCancel: true));
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );
    

    I used firebase and Awesome Notification plugin to show notification.

    Package Used : firebase_messaging: ^7.0.3

    Below is the code how I showing

       class _MyAppState extends State<MyApp> {
      @override
        Widget build(BuildContext context) {
          final pushNotificationService = PushNotificationService(_firebaseMessaging);
          pushNotificationService.initialise();
      }
    }
    

    Can anyone please help me, I stuck here Thank you

    • Dinkar Kumar
      Dinkar Kumar about 3 years
      can you try to set the priority as high and see if that helps
    • Quick learner
      Quick learner about 3 years
      post appropriate code like library used for FCM, class where you have initialised fcm receiver etc
    • Niraj Savaliya
      Niraj Savaliya about 3 years
      @Quicklearner I updated the question, let me know if it will help