79750409

Date: 2025-08-29 14:28:24
Score: 0.5
Natty:
Report link

.payload on ActiveNotification is only set for notifications that your app showed via flutter_local_notifications.show(..., payload: '...').
It does not read the APNs/FCM payload of a remote push that iOS displayed for you. So for a push coming from FCM/APNs, activeNotifications[i].payload will be null.

Why? In the plugin, payload is a convenience string that the plugin stores inside the iOS userInfo when it creates the notification. Remote pushes shown by the OS don’t go through the plugin, so there’s nothing to map into that field.

What to do instead

Option A (recommended): carry data via FCM data and read it with firebase_messaging.

{
  "notification": { "title": "title", "body": "body" },
  "data": {
    "screen": "chat",
    "id": "12345"     // your custom fields
  },
  "apns": {
    "payload": { "aps": { "content-available": 1 } }
  }
}
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage m) {
  final data = m.data; // {"screen":"chat","id":"12345"}
  // navigate using this data
});

final initial = await FirebaseMessaging.instance.getInitialMessage();
if (initial != null) { /* use initial.data */ }

Option B: Convert the remote push into a local notification and attach a payload.

await flutterLocalNotificationsPlugin.show(
  1001,
  m.notification?.title,
  m.notification?.body,
  const NotificationDetails(
    iOS: DarwinNotificationDetails(),
    android: AndroidNotificationDetails('default', 'Default'),
  ),
  payload: jsonEncode(m.data),  // <— this is what ActiveNotification.payload reads
);

Now getActiveNotifications() will return an ActiveNotification whose .payload contains your JSON string.

Gotcha to avoid: Adding a payload key inside apns.payload doesn’t populate the plugin’s .payload—that’s a different concept. Use RemoteMessage.data or explicitly set the payload when you create a local notification.


Bottom line: For FCM/APNs pushes, read your custom info from RemoteMessage.data (and onMessageOpenedApp/getInitialMessage). If you need .payload from ActiveNotification, you must show the notification locally and pass payload: yourself.

Reasons:
  • Blacklisted phrase (0.5): Why?
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Code Master