我正在制作一个聊天应用程序。我使用此代码检测新消息(在Firebase数据库上),然后通知用户:
package com.tungvuong.dangeralarm;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import com.firebase.client.Firebase;
import com.google.firebase.FirebaseError;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import static com.facebook.login.widget.ProfilePictureView.TAG;
public class receive extends IntentService {
public receive() {
super("receive");
}
@Override
protected void onHandleIntent(Intent intent) {
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String tinmoi = dataSnapshot.child("messageText").getValue(String.class);
Intent intent = new Intent(receive.this, chat.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(receive.this, 0, intent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(receive.this, "M_CH_ID");
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.emergency_icon)
.setContentTitle("Có tin mới!")
.setContentText(tinmoi)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) receive.this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notificationBuilder.build());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Log errors
}
};
rootRef.addValueEventListener(valueEventListener);
}
}
但是当我通过从Firebase控制台下载数据库进行测试时,然后使用Notepad ++将新条目添加到json文件中,然后最终将其上传到控制台(我没有2部手机可以对此进行测试,所以我更改json文件),则该应用未通知用户或其他任何信息。
这是我的Firebase数据库:
我的Firebase数据库
如您所见, MessageText 是侦听器所侦听的内容。该应用程序是一个群聊应用程序,仅允许用户匿名登录。
答案 0 :(得分:0)
FirebaseDatabase.getInstance().getReference()
.child("danger-alram")
.orderByChild("messageTime")
.startAt(timeStampInMill) // last message time stamp goes here for first time it is zero
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// You will get newly added child (messages) here...
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
将消息上传到Firebase时,您将获得onComplete()
方法
FirebaseDatabase.getInstance().getReference()
.child("danger-alram")
.push().setValue(newMessage, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
// send push notification to other group member using their firebase device id.
}
});