当新的子项添加到Firebase实时数据库时,如何获取通知?(我想使用android服务而不是FCM)
这就是我所取得的成就!在我的应用程序中,我有一个打开和关闭通知按钮。打开按钮启动服务并为数据库设置侦听器。关闭按钮停止服务并删除数据库侦听器当新的子项添加到Firebase实时数据库时,如何获取通知?(我想使用android服务而不是FCM),android,firebase-realtime-database,service,notifications,Android,Firebase Realtime Database,Service,Notifications,这就是我所取得的成就!在我的应用程序中,我有一个打开和关闭通知按钮。打开按钮启动服务并为数据库设置侦听器。关闭按钮停止服务并删除数据库侦听器 public class NotificationService extends Service { String CHANNEL_ID = "hostelmate_notification"; DatabaseReference databaseIssue; ChildEventListener childEventListener; @Overr
public class NotificationService extends Service {
String CHANNEL_ID = "hostelmate_notification";
DatabaseReference databaseIssue;
ChildEventListener childEventListener;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flag, int startId) {
Toast.makeText(this,"Notifications activated",Toast.LENGTH_LONG).show();
databaseIssue = FirebaseDatabase.getInstance().getReference("issues");
childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
displayNotification();
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
databaseIssue.addChildEventListener(childEventListener);
return START_STICKY;
}
@Override
public void onDestroy() {
databaseIssue.removeEventListener(childEventListener);
Toast.makeText(this,"Notifications deactivated",Toast.LENGTH_LONG).show();
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
public void displayNotification(){
createNotificationChannel();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setSmallIcon(R.drawable.ic_notifications_active_white_24dp);
builder.setContentTitle("Hostel Mate");
builder.setContentText("New issue reported!");
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
NotificationManagerCompat notificationManagerCompat =NotificationManagerCompat.from(this);
int notificationId = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
notificationManagerCompat.notify(notificationId, builder.build());
}
private void createNotificationChannel(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
CharSequence name = "Hostelmate notification";
String description = "Hi";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
notificationChannel.setDescription(description);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
}
}
我的方法有什么问题
我只想在数据库中添加新的子项时显示通知但目前,通知显示在服务开始时。
如何避免这种情况?ChildEventListeners最初会在所选位置触发所有数据。存在于该位置的每个子节点将导致对
onchildaded
的一次调用。没有办法告诉它只给你“新”的孩子。如果基于当前时间,您只需要某些项目,那么您需要做的是创建一个只返回新项目的查询。这可能涉及在子节点中包含一些时间戳,并仅查询超过当前时间的时间戳
相反,您可以使用云函数触发对数据库的新更改,然后在功能代码中,使用Firebase云消息通知对新子项感兴趣的应用程序