Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive custom data when app is terminated through push notification using firebase_messaging?

As in title, what is the current workaround in order to receive custom data when the user click on a notification when the app is terminated?

Seems like on Android is not possible to receive a data message in the onLaunch (which is the ideal way) On IOS i hadn't tried yet since i'm facing this issue first.

Any clue?

Additional infos: The notification that i'm sending through a firebase cloud function are in this form:

  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    },
    "data" : {
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }
}

https://firebase.google.com/docs/cloud-messaging/concept-options

on the onResume i'm able to perform an action, but the onLaunch seems to not be called.

like image 552
Stefano Saitta Avatar asked Oct 03 '19 08:10

Stefano Saitta


2 Answers

This is the format that your payload should take with data included. It does work as this is how I have gotten it to work in my project. Additionally to get everything to work correctly, there is an answer here that might be helpful for general project setup:

 {
      notification: {
        title: 'A title',
        body: 'The body.'
      },
      data: {
        // your data here
      },
      android: {
        ttl: 3600 * 1000,
        notification: {
          click_action: 'FLUTTER_NOTIFICATION_CLICK'
        }
      },
      // The below is for iOS specific properties
      // apns: {
      //   payload: {
      //     aps: {
      //       badge: 42
      //     }
      //   }
      // },
      tokens: [] // or a single 'token'
    }
like image 59
DF_ Avatar answered Oct 18 '22 23:10

DF_


I think, you already getting data onLaunch, just need to wait a little bit. I wrote it because I came across the same thing.

Can you try this,

firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) async { .. },
  onResume: (Map<String, dynamic> message) async { .. },
  onLaunch: (Map<String, dynamic> message) async {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      Timer(const Duration(seconds: 7), () {
        // your process...
      });
    });
  },
);

Also in my situation, i used it like that, and solved.

onLaunch: (Map<String, dynamic> message) async {
    WidgetsBinding.instance.addPostFrameCallback((ignored) {
      Timer.periodic(const Duration(seconds: 1), (timer) {
        if (CurrentUser.currentUser != null) {
          timer.cancel(); // important **
          // redirect process with post details inside data
          if (message['data']['type'] == NotifTypes.NEW_POST)
            goGroupAndPost(Map.from(message['data'])); 
        }
      });
    });
  },
like image 37
Ibrahim Karahan Avatar answered Oct 19 '22 00:10

Ibrahim Karahan