How to run a function when a notification is received with Firebase Messaging ^8.0.0-dev.8 in Flutter?

4,939

Solution 1

A solution I found to get the events to fire was to always call:

await FirebaseMessaging.instance.getToken();

right after the

await Firebase.initializeApp();

Once I call that, the FirebaseMessaging.onMessage.listen catches the event as expected.

Solution 2

  • I was getting the same log when my app is in doze mode for the Data notification with high priority. This is because of some issue in the firebase-messaging plugin.
  • Firebase_messaging plugin internally uses JobIntentService to process background fcm notifications
  • JobIntentService has one constraint in Android O or later versions, when running a Job it will be subject to standard JobScheduler policies. The job will not run immediately while the device is in doze mode. (reference link)
  • The same issue was raised in the firebase_messaging git repository(bug link)

Solution

  • One Signal(another push notification provider) solved this issue by having a modified version of JobIntentService. (OneSignal Solution)
  • At a high level, it uses wake locks for high priority fcm notifications to run service even in Android O and above.
  • Add this Pull Request changes in your ide by editing respective files.

TL;DR

Add this Pull Request changes in your ide by editing respective files. Send Data notification with High priority.

FCM Payload:

{
"message": {
    "token": "fcm_client_token",
    "data": {
        "title": "Hello",
        "body": "Test Message"
    },
    "android": {
        "priority": "high"
    }
}

}

Share:
4,939
Andrej
Author by

Andrej

Hi, I'm Andrej. I'm 16. I live in Modriča, Bosnia and Herzegovina. I'm currently attending high school. After high school, My plan is to study Software Engineering. In 2019 I was in the top 10 U-15 programmers in Bosnia and Herzegovina. I have experience in C++ and Python.

Updated on December 25, 2022

Comments

  • Andrej
    Andrej over 1 year

    Hello I'm using Flutter to build my app and I need to to show an alert whenever a new notification is received.
    I've been using firebase_messaging 7.0.3 but I run into an error with onBackgroundMessage. A quick Google search helped me find out that the error I was getting hasn't been fixed yet. However one of the devs posted an update 20 days ago about a new version of the package which fixed that issue.
    The new version removed the old onMessage handlers and introduced new ones. Now they got new event handlers which return streams, but haven't been able to make them fire by using the .listen() function. Whenever I receive a notification a get a this: D/FLTFireMsgReceiver(22032): broadcast received for message printed in the console but the code in the .listen() doesn't get executed.

    Here is a link to an article on Firebase Flutter that is a guide for using the new version of the package. Here is my code:

    ...
    FirebaseMessaging.onMessage.listen((event) {
     // do something
    });
    FirebaseMessaging.onMessageOpenedApp.listen((event) {
     // do something
    });
    FirebaseMessaging.onBackgroundMessage((message) {
     // do something
     return;
     }
    ...
    
    • lordvcs
      lordvcs over 3 years
      I am facing the same issue too, were u able to solve this??
    • Andrej
      Andrej over 3 years
      I ended up using firebase_messaging: ^7.0.3. I fixed the error I was getting by adding Future<void> myBackgroundMessageHandler(Map<String, dynamic> message) async {} to the top of the file (under the imports), and setting onBackgroundMessage: myBackgroundMessageHandler. It now works perfectly and I haven't come accross any error yet.
  • Vlad
    Vlad about 3 years
    Seems it is not required to await getToken
  • ALEXANDER LOZANO
    ALEXANDER LOZANO almost 3 years
    thanks, this was the solution after days of searching
  • Petro
    Petro over 2 years
    FirebaseMessaging.onMessage.listen is key for this to work, thanks!