Find out how to get ios lively notification payload in flutter?

0
4
Find out how to get ios lively notification payload in flutter?


.payload on ActiveNotification is barely set for notifications that your app confirmed through flutter_local_notifications.present(..., payload: '...').
It does not learn the APNs/FCM payload of a distant push that iOS displayed for you. So for a push coming from FCM/APNs, activeNotifications[i].payload will probably be null.

Why? Within the plugin, payload is a comfort string that the plugin shops contained in the iOS userInfo when it creates the notification. Distant pushes proven by the OS don’t undergo the plugin, so there’s nothing to map into that discipline.

What to do as an alternative

Choice A (really useful): carry knowledge through FCM knowledge and skim it with firebase_messaging.

{
  "notification": { "title": "title", "physique": "physique" },
  "knowledge": {
    "display screen": "chat",
    "id": "12345"     // your customized fields
  },
  "apns": {
    "payload": { "aps": { "content-available": 1 } }
  }
}
FirebaseMessaging.onMessageOpenedApp.pay attention((RemoteMessage m) {
  remaining knowledge = m.knowledge; // {"display screen":"chat","id":"12345"}
  // navigate utilizing this knowledge
});

remaining preliminary = await FirebaseMessaging.occasion.getInitialMessage();
if (preliminary != null) { /* use preliminary.knowledge */ }

Choice B: Convert the distant push right into a native notification and connect a payload.

  • In foreground or in a background handler, learn RemoteMessage.knowledge, then name:
await flutterLocalNotificationsPlugin.present(
  1001,
  m.notification?.title,
  m.notification?.physique,
  const NotificationDetails(
    iOS: DarwinNotificationDetails(),
    android: AndroidNotificationDetails('default', 'Default'),
  ),
  payload: jsonEncode(m.knowledge),  // <— that is what ActiveNotification.payload reads
);

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

Gotcha to keep away from: Including a payload key inside apns.payload doesn’t populate the plugin’s .payload—that’s a distinct idea. Use RemoteMessage.knowledge or explicitly set the payload while you create an area notification.


Backside line: For FCM/APNs pushes, learn your customized data from RemoteMessage.knowledge (and onMessageOpenedApp/getInitialMessage). When you want .payload from ActiveNotification, you could present the notification domestically and go payload: your self.

LEAVE A REPLY

Please enter your comment!
Please enter your name here