推送通知发送给发送消息的用户而不是接收者

如果其他用户向您发送消息,我已经创建了一种向其他用户发送通知的方法。我当前遇到的问题是,发送消息的用户就是接收通知的用户。显然,我要其他用户(该通知用于接收该通知的用户)。

因此,我正在模拟器和实际设备上进行测试,当我发送通知时,我会收到通知,然后在与另一台设备一起查看消息时,我会收到另一条通知发送到该设备。

因此,从本质上讲,我收到两个通知,它们都发送到了发送消息的同一设备上。我该如何解决它,以便只有通知的用户才能收到它,而没有人收到,我也不会收到它?

I have been trying to change my if statement, I think that's the key here, but haven't been able to figure it out. Suggestions?

In my sendNotification(); method I have the line if(mFirebaseUser.getUid.equals(receiver)) {...} so shouldn't that make it show up on the device who the notification is for?

MessageActivity

public class MessageActivity extends AppCompatActivity {

    TextView mUsername;
    TextView mButtonSend;
    EditText mTextSend;

    FirebaseUser mFirebaseUser;
    DatabaseReference mReference;

    MessageAdapter mMessageAdapter;
    List<Chat> mChat;
    RecyclerView mRecyclerView;

    String mId;
    String mMessageId;
    String mUserId;

    String NOTIFICATION_CHANNEL_ID = "e.events.Notifications.test";
    String NOTIFICATION_CHANNEL_NAME = "Events";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);

        mId = getIntent().getStringExtra("id");
        mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        mUserId = getIntent().getStringExtra("userid");

        mButtonSend.setOnClickListener(v -> {
            String message = mTextSend.getText().toString();
            if (!message.equals("")) {
                sendMessage(mFirebaseUser.getUid(), mId, message);
                sendNotification();
            } else {
                Toast.makeText(MessageActivity.this, "Write something", Toast.LENGTH_SHORT).show();
            }
            mTextSend.setText("");
        });

        getUserInfo();
        seenMessage(mId);
    }

    private void sendNotification() {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Chats").child(mMessageId);
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    String receiver = dataSnapshot.child("receiver").getValue(String.class);
                    if (mFirebaseUser.getUId().equals(receiver)) {
                            showNotification();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

    private void showNotification() {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Chats").child(mMessageId);
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    String message = dataSnapshot.child("message").getValue(String.class);

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);

                        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                        notificationManager.createNotificationChannel(notificationChannel);

                        NotificationCompat.Builder builder = new NotificationCompat.Builder(MessageActivity.this, NOTIFICATION_CHANNEL_ID)
                                .setContentTitle("Events")
                                .setContentText(message)
                                .setSmallIcon(R.mipmap.ic_launcher)
                                .setAutoCancel(true);

                        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(MessageActivity.this);
                        notificationManagerCompat.notify(999, builder.build());
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

    private void sendMessage(String sender, final String receiver, String message) {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Chats");

        Date date = Calendar.getInstance().getTime();
        String formattedDate = DateFormat.getDateInstance(DateFormat.FULL).format(date);

        String time = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            time = LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm"));
        }

        mMessageId = reference.push().getKey();

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("sender", sender);
        hashMap.put("receiver", receiver);
        hashMap.put("message", message);
        hashMap.put("isseen", false);
        hashMap.put("date", formattedDate);
        hashMap.put("time", time);
        hashMap.put("timestamp", ServerValue.TIMESTAMP);
        hashMap.put("messageId", mMessageId);

        reference.child(mMessageId).setValue(hashMap);

        final DatabaseReference chatReference = FirebaseDatabase.getInstance().getReference("Chatlist").child(mFirebaseUser.getUid()).child(mId);
        chatReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (!dataSnapshot.exists()) {
                    chatReference.child("receiver").setValue(mId);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        final DatabaseReference chatReferenceReceiver = FirebaseDatabase.getInstance().getReference("Chatlist").child(mId).child(mFirebaseUser.getUid());
        chatReferenceReceiver.child("receiver").setValue(mFirebaseUser.getUid());
    }

    private void readMessages(final String myid, final String userid, final String imageurl) {
        mChat = new ArrayList<>();
        mReference = FirebaseDatabase.getInstance().getReference("Chats");
        mReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mChat.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Chat chat = snapshot.getValue(Chat.class);
                    if (chat != null) {
                        if (chat.getReceiver().equals(myid) && chat.getSender().equals(userid) || chat.getReceiver().equals(userid) && chat.getSender().equals(myid)) {
                            mChat.add(chat);
                        }
                    }

                    mMessageAdapter = new MessageAdapter(MessageActivity.this, mChat, imageurl);
                    mRecyclerView.setAdapter(mMessageAdapter);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
评论