在实时数据库中添加新孩子或更改某些数据库时收到新通知

时间:2021-01-04 08:28:24

标签: android firebase firebase-realtime-database push-notification android-notifications

我使用的是安卓 4.1.1 版本 如果任何孩子退出,我在启动 android 应用程序时会收到通知 我只需要在新子项添加到我的数据库或现有子项更改某些信息时通知。

这是通知代码

private void notification() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel channel = new NotificationChannel("n", "n", NotificationManager.IMPORTANCE_DEFAULT);
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);

        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "n")
                .setContentText("My Channel")
                .setSmallIcon(R.drawable.icon_service)
                .setAutoCancel(true)
                .setContentText("New Service is Added");
        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
        managerCompat.notify(1064, builder.build());


    }

这里我在创建方法中调用了这个通知函数:

databaseRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
            notification();
    }

    @Override
    public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
        notification();
    }

    @Override
    public void onChildRemoved(@NonNull DataSnapshot snapshot) {
    }

    @Override
    public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
    }
}); 

先谢谢你

0 个答案:

没有答案