Received notifications from FirebaseMessaging but the print function inside the OnMessage() is not working

Viewed 693

I am working with flutter project in AndroidStudio and i cant print the content of the notification while receiving notification from firebase messaging. Here's the code for my main.dart and homePage

main.dart code:

void main()async
{
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}
class MyApp extends StatelessWidget
{

  const ConnectionPage({Key key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return
     MaterialApp
            (
              title: "HomeHero",
              theme: ThemeData
                (
                primaryColor: Colors.white,
                backgroundColor: Colors.white,
              ),
          home: homePage(),
    );
   );
  }
}

homePage code:

 class homePage extends StatefulWidget {

  @override
  _homePage createState() => _homePage();
}

class _homePage extends State<homePage> 
{
  void iniState() {
    super.initState();
    FirebaseMessaging.instance.getInitialMessage();
    FirebaseMessaging.onMessage.listen((message) {
      print(message.notification.body);
      print(message.notification.title);
    });

}

Output:

D/FLTFireMsgReceiver(23787): broadcast received for message
W/e.providers_ap(23787): Accessing hidden method Landroid/os/WorkSource;->add(I)Z (unsupported,test-api, reflection, allowed)
W/e.providers_ap(23787): Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (unsupported,test-api, reflection, allowed)
W/e.providers_ap(23787): Accessing hidden method Landroid/os/WorkSource;->get(I)I (unsupported, reflection, allowed)
W/e.providers_ap(23787): Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (unsupported, reflection, allowed)

Logcat:

2022-03-22 14:14:20.150 23787-23787/com.example.providers_app D/FLTFireMsgReceiver: broadcast received for message
2022-03-22 14:14:20.193 23787-23920/com.example.providers_app W/e.providers_ap: Accessing hidden method Landroid/os/WorkSource;->add(I)Z (unsupported,test-api, reflection, allowed)
2022-03-22 14:14:20.193 23787-23920/com.example.providers_app W/e.providers_ap: Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (unsupported,test-api, reflection, allowed)
2022-03-22 14:14:20.193 23787-23920/com.example.providers_app W/e.providers_ap: Accessing hidden method Landroid/os/WorkSource;->get(I)I (unsupported, reflection, allowed)
2022-03-22 14:14:20.194 23787-23920/com.example.providers_app W/e.providers_ap: Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (unsupported, reflection, allowed)

Another things to mention is when i created a new project just for testing the print function while receiving notification, it works (re-write all the code). But when i copy all the lib files from the project which fail to run the print function and paste it to the lib of the new created project, it fails again.

Code for my re-write project:

void main()async
{
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  runApp(ConnectionPage());
}

class ConnectionPage extends StatelessWidget
{
  const ConnectionPage({Key key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return
      MaterialApp
          (
          title: "HomeHero",
          theme: ThemeData
            (
            primaryColor: Colors.white,
            backgroundColor: Colors.white,
          ),
        home: MyHomePage(),

        );

  }

MyHomePage code:

class MyHomePage extends StatefulWidget {
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    FirebaseMessaging.instance.getInitialMessage();
    FirebaseMessaging.onMessage.listen((message) {
      print(message.notification.body);
      print(message.notification.title);
    });
  }
}

here's the output of the re-write project:

D/FLTFireMsgReceiver(24304): broadcast received for message
W/ication_testin(24304): Accessing hidden method Landroid/os/WorkSource;->add(I)Z (unsupported,test-api, reflection, allowed)
W/ication_testin(24304): Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (unsupported,test-api, reflection, allowed)
W/ication_testin(24304): Accessing hidden method Landroid/os/WorkSource;->get(I)I (unsupported, reflection, allowed)
W/ication_testin(24304): Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (unsupported, reflection, allowed)
I/flutter (24304): Notification Testing
I/flutter (24304): Testing

Logcat:

2022-03-22 14:45:04.205 24304-24304/com.example.notification_testing D/FLTFireMsgReceiver: broadcast received for message
2022-03-22 14:45:04.263 24304-24400/com.example.notification_testing W/ication_testin: Accessing hidden method Landroid/os/WorkSource;->add(I)Z (unsupported,test-api, reflection, allowed)
2022-03-22 14:45:04.263 24304-24400/com.example.notification_testing W/ication_testin: Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (unsupported,test-api, reflection, allowed)
2022-03-22 14:45:04.263 24304-24400/com.example.notification_testing W/ication_testin: Accessing hidden method Landroid/os/WorkSource;->get(I)I (unsupported, reflection, allowed)
2022-03-22 14:45:04.263 24304-24400/com.example.notification_testing W/ication_testin: Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (unsupported, reflection, allowed)
2022-03-22 14:45:04.276 24304-24351/com.example.notification_testing I/flutter: Notification Testing
2022-03-22 14:45:04.278 24304-24351/com.example.notification_testing I/flutter: Testing
1 Answers

Body of FCM

{
    "to": "<DEVICE ID>",
    "notification": {
        "body": "Body of Your Notification",
        "title": "Title of Your Notification A",
        "sound": "default",
        "alert": "New"
    },
    "priority": "high",
    "contentAvailable": true,
    "data": {
        "body": "Body of Your Notification in Data",
        "title": "Title of Your Notification in Title B",
        "key_1": "Value for key_1",
        "key_2": "Value for key_2"
    }
}


FirebaseMessaging.onMessage.listen((message) {
print(' FCM on message  : ${message.data}');
})

Use data field to display the notification

Related