Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter : How get Json data from firebase notification payload

I need to show custom dialog when i'm receiving notification from firebase. but i don't know how can get my data from notification payload

I have tried below code but it is bot working for me

  Future<void> _handleNotification(
    Map<dynamic, dynamic> message, bool dialog) async {
    final dynamic data = message['data'] ?? message;
    final dynamic notification = message['notification']['body'] ?? message;

    final String itemId = data['body'];

    debugPrint("DATA ->> $data");
    debugPrint("notification ->> $notification");
    message.forEach((k, v) => debugPrint("${k} - ${v}"));
  }

Here is my payload in console

flutter: notification message {gcm.message_id: 1578573493122232, id: 30, google.c.a.e: 1, type: test, aps: {alert: {title: notication title, body: Message of Push notification}, sound: 1}}

Can any body help to get data from notification payload?

like image 598
Goku Avatar asked Jan 09 '20 12:01

Goku


1 Answers

Finally I'm able to solve this issue by my self

If you want get data from notification payload then you need to send your data inside "data" -field of the message.

SAMPLE CODE read data from "data" -field of the message.

Future<void> _handleNotification(
    Map<dynamic, dynamic> message, bool dialog) async {
    var data = message['data'] ?? message;
    String notificationTitle = data['YOUR_KEY']; // here you need to replace YOUR_KEY with the actual key that you are sending in notification  **`"data"`** -field of the message. 
    String notificationMessage = data['YOUR_KEY'];// here you need to replace YOUR_KEY with the actual key that you are sending in notification  **`"data"`** -field of the message. 

    // now show the Dialog
    Utils().showMessageDialog(context, notificationTitle,notificationMessage);
  }

For more information please read Notification messages with additional data

like image 183
Goku Avatar answered Nov 15 '22 11:11

Goku