0

I am using Flutter to create a chat app. I have added flutter_local_notifications plugin along with Firebase Messaging to handle the notifications. I am using MessagingStyleNotification for showing chat notifications and have added a reply feature with Input action. But when i send the reply the Person object remains as the person from original notification. Is there a way to add current user's details as Person (user who replied from notification) when they send the reply? As per my research on this topic, I probably need to create a BroadCastReceiver and PendingIntent to handle this behaviour? But I am not sure who to do this as I haven't worked with native android in Flutter before. Any help would be great. Thanks in advance!

this is my method to show chat notification:

void showRecievedChatNotification(RemoteMessage message, {bool fromAction = false}) async {
    ChatMsgNotif notifData = ChatMsgNotif.fromJson(message.data);
    ProfileModel senderUser = notifData.senderUser;
    bool isGroup = notifData.dbCollection == DBCollections.groups.name;

    if((isGroup && notifData.senderUser.profileId == auth.currentUser!.uid && !fromAction)
    || (!isGroup && notifData.senderUser.profileId == auth.currentUser!.uid)
    ) return;
    
    final http.Response resPerson = await http.get(Uri.parse(senderUser.profilePic));
    
    Person person = Person(
      key: senderUser.profilePic,
      name: senderUser.name, 
      icon: ByteArrayAndroidIcon.fromBase64String(base64Encode(resPerson.bodyBytes)),
    );

    Message newNotifMsg = Message(
      notifData.body, 
      notifData.timeSent, 
      person
    );

    http.Response response;
    if(isGroup){
      response = await http.get(Uri.parse(notifData.receiverProfile.profilePic));
    }
    else{
      response = await http.get(Uri.parse(fromAction ? notifData.receiverProfile.profilePic : notifData.senderUser.profilePic));
    }

    int convertedIdForNotif = convertedIdToIntForNotifId(isGroup ? notifData.receiverProfile.profileId : senderUser.profileId);

    List<Message> notifMsgs = [];

    final MessagingStyleInformation? activeMessageNotification = await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
      ?.getActiveNotificationMessagingStyle(convertedIdForNotif);

    // if there are previous notifs from this group:
    if(activeMessageNotification != null 
      && activeMessageNotification.messages != null
      && activeMessageNotification.messages!.isNotEmpty
    ){ 
      notifMsgs = activeMessageNotification.messages!; 
    }

    notifMsgs.add(newNotifMsg);

    flutterLocalNotificationsPlugin.show(
        convertedIdForNotif,
        null,
        null,
        NotificationDetails(
          android: AndroidNotificationDetails(
            chatNotificationChannelId,
            chatNotificationChannelName,
            channelDescription: chatNotificationChannelDesc,
            groupKey: isGroup? notifData.receiverProfile.profileId : notifData.senderUser.profileId,
            importance: Importance.max,
            priority: Priority.max,
            color: colorPrimary,
            largeIcon: ByteArrayAndroidBitmap.fromBase64String(base64Encode(response.bodyBytes)),
            //setAsGroupSummary:
            autoCancel: false,
            styleInformation: MessagingStyleInformation(
                person,
                conversationTitle: isGroup ? notifData.receiverProfile.name : notifData.senderUser.name,
                groupConversation: true,
                messages: notifMsgs
              ),
            actions: <AndroidNotificationAction>[
              // Reply Action:
              const AndroidNotificationAction(
                actionReplyToMsg, 
                'Reply', 
                allowGeneratedReplies: true,
                inputs: <AndroidNotificationActionInput>[
                  AndroidNotificationActionInput(
label: 'Reply',
                  )
                ]
              ),
            ],
          ),
        ),
        payload: jsonEncode(message.data),
      );
  }

this is who I send the reply back to the user:

void handleNotificationResponse(NotificationResponse notificationResponse, WidgetRef ref){
    switch (notificationResponse.notificationResponseType){
        // A Notification Action is selected:
        case NotificationResponseType.selectedNotificationAction:
          switch (notificationResponse.actionId) {
            // Like Msg:
            case actionLikeMsg:
              
              break;
            // Reply to Msg:
            case actionReplyToMsg:
              String replyTxt = notificationResponse.input!;
              ChatMsgNotif notifData = ChatMsgNotif.fromJson(jsonDecode(notificationResponse.payload!));
              //UserModel? user = ref.read(userDataProvider);
              // send to DB:
              ref.read(chatsApiProvider).sendMessage(
                receiverProfile: notifData.dbCollection == DBCollections.groups.name ? notifData.receiverProfile : notifData.senderUser, 
                text: replyTxt, 
                dbCollectionName: notifData.dbCollection
              );
  
              break;
    }
}

Current behaviour is that when the user sends the reply, the icon and the name of the original sender appears with the reply, but I want to display current user's name and icon on reply send instead.

Ali99
  • 3
  • 7

0 Answers0