1

Please tell me what I'm doing wrong. I send a Cloud Message to my device, and when the application is closed, I want to check how it was launched from Push Notification or not. Also, if the application was opened from Push Notifications, I need to get the data in order to open the corresponding application page.

I found a method that checks whether the application was started by clicking on Push Notification FirebaseMessaging.instance.getInitialMessage(). But the answer to this call is always null, I closed the application completely, sent a notification, launched it and always the value = null. I also use a package to track and display messages: awesome_notification

This is what my code looks like:

FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage? message) async {
      if(message != null){
        Get.snackbar('Title', '$message');
        auth.testMessage.value = message;
      }
      });

Request to send a message:

{
    "to" : "my_token",
    "mutable_content" : true,
    "content_available" : true,
   "priority" : "high",
   "data" : {
        "content": {
            "id": 100,
            "channelKey": "basic_channel",
            "title": "Huston!\nThe eagle has landed!",
            "body": "A small step for a man, but a giant leap to Flutter's community!",
            "notificationLayout": "BigPicture",
            "largeIcon": "https://media.fstatic.com/kdNpUx4VBicwDuRBnhBrNmVsaKU=/full-fit-in/290x478/media/artists/avatar/2013/08/neil-i-armstrong_a39978.jpeg",
            "bigPicture": "https://www.dw.com/image/49519617_303.jpg",
            "showWhen": true,
            "autoDismissible": true,
            "privacy": "Private"
        },
        "actionButtons": [
            {
                "key": "REPLY",
                "label": "Reply",
                "autoDismissible": true,
                "buttonType":  "InputField"
            },
            {
                "key": "ARCHIVE",
                "label": "Archive",
                "autoDismissible": true
            }
        ]
    }
}
che4len
  • 63
  • 1
  • 7

1 Answers1

0

I hope it will be helpful for any one who have the same problem. First in method to show push notification add reader of a payload.

void showFlutterNotification(RemoteMessage message) {
  RemoteNotification? notification = message.notification;
  AndroidNotification? android = message.notification?.android;
  if (notification != null && android != null && !kIsWeb) {
    flutterLocalNotificationsPlugin.show(
        notification.hashCode,
        notification.title,
        notification.body,
        NotificationDetails(
          android: AndroidNotificationDetails(
            channel.id,
            channel.name,
            channelDescription: channel.description,
            // TODO add a proper drawable resource to android, for now using
            // one that already exists in example app.
            icon: 'launch_background',
          ),
        ),
        // Add reading of a payload based on the contents of notification
        // In my case:
        payload: notification.data['params']);
  }
}

Then somewhere in the application for example in initState method try to call the payload.

NotificationAppLaunchDetails? notificationAppLaunchDetails = await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
    if (notificationAppLaunchDetails != null && notificationAppLaunchDetails.didNotificationLaunchApp) {
      var payload = notificationAppLaunchDetails.notificationResponse?.payload;
      if (payload != null && payload.isNotEmpty) {
        // Your code to handle the payload.
        // For example to navigate.
      }
    }

Be aware that flutterLocalNotificationsPlugin is properly initialized in main().