Flutter 通知:使用 flutter_local_notifications 输入操作直接回复聊天通知

问题描述 投票:0回答:1

我正在使用 Flutter 创建一个聊天应用程序。我添加了 flutter_local_notifications 插件以及 Firebase Messaging 来处理通知。我使用 MessagingStyleNotification 来显示聊天通知,并添加了带有输入操作的回复功能。但是当我发送回复时,Person 对象仍然是原始通知中的人。有没有办法在发送回复时将当前用户的详细信息添加为人员(通过通知回复的用户)?根据我对此主题的研究,我可能需要创建一个 BroadCastReceiver 和 PendingIntent 来处理这种行为?但我不知道该由谁来做这件事,因为我之前没有在 Flutter 中使用过原生 android。任何帮助都会很棒。预先感谢!

这是我显示聊天通知的方法:

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),
      );
  }

这是我将回复发送回用户的人:

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;
    }
}

当前的行为是,当用户发送回复时,原始发件人的图标和名称会随回复一起出现,但我想在回复发送时显示当前用户的名称和图标。

flutter broadcastreceiver android-pendingintent flutter-local-notification notification-action
1个回答
0
投票
Future<void> _showNotificationWithActions() async {
const AndroidNotificationDetails androidNotificationDetails =
      AndroidNotificationDetails(
    '...',
    '...',
    '...',
    actions: <AndroidNotificationAction>[
      AndroidNotificationAction('id_1', 'Action 1'),
      AndroidNotificationAction('id_2', 'Action 2'),
      AndroidNotificationAction('id_3', 'Action 3'),
    ],
  );
  const NotificationDetails notificationDetails =
      NotificationDetails(android: androidNotificationDetails);
  await flutterLocalNotificationsPlugin.show(
      0, '...', '...', notificationDetails);
}

这里是有关操作主题的官方文档链接。 https://pub.dev/packages/flutter_local_notifications#notification-actions

© www.soinside.com 2019 - 2024. All rights reserved.