0

首先,我不知道如何识别被触发的特定 remoteInput,
我尝试使用 if 逻辑来解决问题,但没有像 if(!remoteInput.getCharSequence(String.valueOf(key)) 这样的好结果。 toString().isEmpty()) 返回异常错误

请我需要立即帮助。

    
package com.myapp.app;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.Person;
import androidx.core.app.RemoteInput;
import androidx.core.graphics.drawable.IconCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.myapp.app.App.CHANNEL_1_ID;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    static List<Message> MESSAGES = new ArrayList<>();
    static Map<Integer, List> databaseChat =  new HashMap<>();
    public static Bitmap receiver_pic;
    public static Bitmap sender_pic;


    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if (remoteMessage.getData().size() > 0) {
            //Log.d(TAG, "Message data payload: " + remoteMessage.getData());
            String title, Message, picture,name,receiver_pic_url,sender_pic_url, Style;
            title = remoteMessage.getData().get("title");
            Message = remoteMessage.getData().get("message");
            Style = remoteMessage.getData().get("style");
            name = remoteMessage.getData().get("name");
            receiver_pic_url = remoteMessage.getData().get("receiver_pic");
            sender_pic_url = remoteMessage.getData().get("sender_pic");
            Integer chat_id = Integer.valueOf(Objects.requireNonNull(remoteMessage.getData().get("chat_id")));

            if(Style.equals("messagingStyle")){
                try {
                    if (sender_pic_url != null && !"".equals(sender_pic_url) && receiver_pic_url != null && !"".equals(receiver_pic_url)) {
                        URL url1 = new URL(sender_pic_url);
                        URL url2 = new URL(receiver_pic_url);
                        sender_pic = BitmapFactory.decodeStream(url1.openConnection().getInputStream());
                        receiver_pic = BitmapFactory.decodeStream(url2.openConnection().getInputStream());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Person you = new Person.Builder().setIcon(IconCompat.createWithBitmap(sender_pic)).setName(name).build();
                databaseChat.put(chat_id, MESSAGES);
                databaseChat.get(chat_id).add(new Message(Message, you));
                sendChannel1Notification(MyFirebaseMessagingService.this,databaseChat.get(chat_id),receiver_pic,chat_id);
            }


        }

    }

    public static void sendChannel1Notification(Context context,List<Message> MESSAGES_ARR, Bitmap receiver_pic,Integer chat_id) {
        Intent activityIntent = new Intent(context, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context,
                0, activityIntent, 0);
        RemoteInput remoteInput = new RemoteInput.Builder(String.valueOf(chat_id))
                .setLabel("Your answer...")
                .build();
        Intent replyIntent;

        Intent broadcastIntent = new Intent(context, DirectReplyReceiver.class);
        broadcastIntent.setAction("MarkAsSeen");
        broadcastIntent.putExtra("MarkAsSeen", "Yes");
        PendingIntent actionIntent = PendingIntent.getBroadcast(context,
                0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        PendingIntent replyPendingIntent = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            replyIntent = new Intent(context, DirectReplyReceiver.class);
            replyPendingIntent = PendingIntent.getBroadcast(context,
                    0, replyIntent, 0);
        } else {
            //start chat activity instead (PendingIntent.getActivity)
            //cancel notification with notificationManagerCompat.cancel(id)
        }
        NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
                R.drawable.ic_reply,
                "Reply",
                replyPendingIntent
        ).addRemoteInput(remoteInput).build();

        Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_fibi_trans);

        Person user = new Person.Builder().setIcon(IconCompat.createWithBitmap(receiver_pic)).setName("Me").build();
        NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(user);
        for (Message chatMessage : MESSAGES_ARR) {
            if(chatMessage.getSender()!= null){
                String chatWith= (String) chatMessage.getSender().getName();
                messagingStyle.setConversationTitle("Chat with "+chatWith);
                break;
            }
        }
        for (Message chatMessage : MESSAGES_ARR) {
            NotificationCompat.MessagingStyle.Message notificationMessage =
                    new NotificationCompat.MessagingStyle.Message(
                            chatMessage.getText(),
                            chatMessage.getTimestamp(),
                            chatMessage.getSender()
                    );
            messagingStyle.addMessage(notificationMessage);
        }
        Intent fullScreenIntent = new Intent(context, MainActivity.class);
        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 0,
                fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification = new NotificationCompat.Builder(context, CHANNEL_1_ID)
                .setSmallIcon(R.drawable.ic_stat_notificaation)
                .setStyle(messagingStyle)
                .addAction(replyAction)
                .addAction(R.mipmap.ic_fibi_trans , "Mark as seen", actionIntent)
                .setColor(Color.GREEN)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setContentIntent(contentIntent)
                .setAutoCancel(true)
                .setOnlyAlertOnce(true)
                .setFullScreenIntent(fullScreenPendingIntent, true)
                .build();
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(chat_id, notification);
    }

}

    

package com.myapp.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.Toast;

import androidx.core.app.RemoteInput;

import java.util.Set;

public class DirectReplyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bitmap receiver_pic= MyFirebaseMessagingService.receiver_pic;
        Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
        Set<Integer> keys = MyFirebaseMessagingService.databaseChat.keySet();
        for (Integer key: keys) {
            if (remoteInput != null) {
                CharSequence replyText = remoteInput.getCharSequence(String.valueOf(key));
                try {
                        Message answer = new Message(replyText, null);

                    if(!remoteInput.getCharSequence(String.valueOf(key)).toString().isEmpty() && replyText!= null){
                        MyFirebaseMessagingService.databaseChat.get(key).add(answer);
                        Toast.makeText(context, replyText.toString(), Toast.LENGTH_SHORT).show();
                    }
                }
                catch (Exception e){
                    e.printStackTrace();
                }
                MyFirebaseMessagingService.sendChannel1Notification(context,
                        MyFirebaseMessagingService.databaseChat.get(key),
                        receiver_pic,key);
            }
        }
        String MarkAsSeen=intent.getStringExtra("MarkAsSeen");

        if (MarkAsSeen.equals("Yes")) {
            Toast.makeText(context, "Yes", Toast.LENGTH_SHORT).show();
        }
    }
}
4

0 回答 0