Recommand · October 14, 2021 0

Getting null arguement while routing through firebase push notifications flutter

I am trying to route to a certain screen with some argument when clicked on push notifications. So far it is working fine when app is in foreground or in background but open. But when the app is terminated it is routing to the correct screen but there is some issue with the argument , it is null.

const payload = admin.messaging.MessagingPayload = {
  data : {
    'type' : 'msg',
    'route' : 'chat-screen',
    'argument' : sentby,
  },
  notification : {
    title : senderData.user_name,
    body: original.message,  
    image: notificationIcon,
    android_channel_id : "Finiso",
    channel_id : "Finiso",
    clickAction : 'FLUTTER_NOTIFICATION_CLICK',
  }
}

This is the code for the notification which I am triggering through cloud functions.

Future<void> backgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  print(message.data.toString());
  print(message.notification.title);
}

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

added this in main.dart

FirebaseMessaging.instance.getInitialMessage().then((message) async {
  if (message != null) {
    print(message.data);
    routeNotification(message);
  }
});

listening to messages and on app open trigger.

void routeNotification(RemoteMessage message) {
final routeName = message.data['route'];
if (routeName == ActivityFeed.routeName) {
  Navigator.of(context).pushNamed(ActivityFeed.routeName);
} else {
  if (message.data['type'] == 'msg') {
    Navigator.pushNamed(
      context,
      message.data['route'],
      arguments: message.data['argument'],
    );
  }
}

}

this is the routing function I used above

Image showing console logs

It is printing the first two lines from main.dart and "null" is the argument I am trying to get.

Can anyone help me on this. I have no idea what’s going on.
Thankyou in advance.