Search code examples
flutterfirebase-cloud-messagingflutter-local-notification

Flutter FCM / Local notifications: how to access the full original message?


Tried accessing the full message in onDidReceiveNotification but no candy. None of the properties in notificationResponse contain the original full message.

  /// Sets up Local Notifications and its Listeners
  Future<void> _localNotificationsSetup(
      BuildContext context, CashticUser currentUser) async {
    _flutterLocalNotificationsPlugin.initialize(
        const InitializationSettings(
          android: AndroidInitializationSettings(
              '@drawable/ic_notification'),
          iOS: DarwinInitializationSettings(),
        ), onDidReceiveNotificationResponse:
            (NotificationResponse notificationResponse) async {
      {
        log.d(notificationResponse.notificationResponseType);
        log.d(notificationResponse.actionId);
        log.d(notificationResponse.input);
        log.d(notificationResponse.id);
        log.d(notificationResponse.payload);

Solution

  • provide the payload property in the show method:

    import 'dart:convert';
    // ...
    Future<void> showFlutterNotification(RemoteMessage message) async {
      RemoteNotification? notification = message.notification;
      AndroidNotification? android = message.notification?.android;
      if (notification != null && android != null && !kIsWeb) {
    flutterNotificationsPlugin.show(
            notification.hashCode,
            notification?.title,
            notification?.body,
            //...
            // message.data contains the payload
            // If your payload is in json, you might also want to encode it since flutter_local_notification accepts strings
            payload: jsonEncode(message.data),
          );
      }
     }
    // this log should work now
    //  log.d(notificationResponse.payload);