0
votes

Hey fellow flutter devs,

My app has some chat functionality and I am sending push notifications via Firebase Cloud Functions and then handle them in onMessage, onLaunch and onResume. It works all as intended.

But what my problem is: When the user is currently viewing the Chat, I dont want the user to see a notififcation, since I am already using a stream listener and the user sees the new messages popping up! So no need to get notified here.

My Code for onMessage in CloudMessaging.configure():

onMessage: (Map<String, dynamic> message) async {
      print(message);
      String chatID = message['data']['chatID'].toString();
      //I want to do something like this:
      //var routeArguments = ModalRoute.of(naviagtorkey.getCurrentContext).settings.arguments;
      //if (routeArguments.contains(chatID)){return;}

      String id = message['data']['id'].toString();
      if (id != _auth.currentUser.uid) { //this makes sure user who sends message doesnt see get notfied
        showOverlayNotification((context) {
          return Card(//removed for clarification)
);
}

My Chat class is constructed with a chatID, so if I could somehow access the current Route and see its arguments (its chatID), then I should be able to NOT show any notification when current route arguments equal message['data']['chatID']. Am I wrong? How could I do that. My commented idea just fails quietly.

1

1 Answers

0
votes

Ok I was thinking way to complicated. I now just used my instance of shared preferences to keep track of currently opened chat:

onMessage: (Map<String, dynamic> message) async {
      SharedPreferences prefs;
      prefs = await SharedPreferences.getInstance();
      String chatID = prefs.getString('currentChat');
      if(chatID==message['data']['chatID'].toString()){
        print(chatID);
        print(message['data']['chatID'].toString());
        return;
      }
      
      String id = message['data']['id'].toString();
      if (id != _auth.currentUser.uid) { //this makes sure user who sends message doesnt see get notfied
        showOverlayNotification((context) {
          return Card(//removed for clarification)
);
}

In my Chat in initState() I add the chatID to my SharedPrefs and in dispose() I set it to 'x'. That way, whenever I am currently viewing the chat, SharedPreferences hold the chatID. Maybe that helps someone in the future!!