当数据从 Firestore 更改时不显示通知

Notification is not showing when data is changing from firestore

public class ListenOrder extends Service {

    FirebaseFirestore firebaseFirestore;
    FirebaseAuth firebaseAuth;
    String UID;
    CollectionReference requests;

    public ListenOrder() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        firebaseAuth=FirebaseAuth.getInstance();
        UID = Objects.requireNonNull(firebaseAuth.getCurrentUser()).getPhoneNumber();
        firebaseFirestore=FirebaseFirestore.getInstance();
        requests=firebaseFirestore.collection("Requests").document(UID).collection("RequestStatus");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        requests.addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                assert queryDocumentSnapshots != null;
                for(DocumentChange documentChange: queryDocumentSnapshots.getDocumentChanges()){
                    switch (documentChange.getType()) {
                        case ADDED:

                            break;
                        case MODIFIED:
                            String doc= documentChange.getDocument().getString("Status");
                            showNotification(doc);
                            break;
                        case REMOVED:

                            break;
                    }

                }
            }
        });
        return super.onStartCommand(intent, flags, startId);
    }

    private void showNotification(String status) {
        int NOTIFICATION_ID = 1;
        Intent intent=new Intent(this,order_status_activity.class);
        PendingIntent contentIntent=PendingIntent.getActivity(this,0,intent,0);

        NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
        builder.setAutoCancel(true)
                .setColor(ContextCompat.getColor(this, R.color.colorPrimaryDark))
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setContentIntent(contentIntent)
                .setContentText("Order was updated status to "+ConvertCodeToStatus(status))
                .setContentTitle("Notification Actions")
                .setSmallIcon(R.mipmap.ic_launcher);
        builder.addAction(android.R.drawable.ic_menu_view, "VIEW", contentIntent);
        NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID,builder.build());
    }

    private String ConvertCodeToStatus(String status) {
        switch (status) {
            case "0":
                return "Placed";
            case "1":
                return "Processed";
            case "2":
                return "Dispatched";
            default:
                return "Delivered";
        }

    }

}

当我退出应用程序时,应用程序崩溃并显示此错误

java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.google.firebase.firestore.QuerySnapshot.getDocumentChanges()' on a null object reference
        at com.example.businessplan1.ListenOrder.onEvent(ListenOrder.java:81)
        at com.example.businessplan1.ListenOrder.onEvent(ListenOrder.java:77)
        at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal(com.google.firebase:firebase-firestore@@21.4.1:1038)
        at com.google.firebase.firestore.Query$$Lambda.onEvent(Unknown Source:6)
        at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent[=11=](com.google.firebase:firebase-firestore@@21.4.1:42)
        at com.google.firebase.firestore.core.AsyncEventListener$$Lambda.run(Unknown Source:6)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6820)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:922)

当我从 firestore 更改字段(状态)的值时,没有通知 showing.I 我正在更改 firestore.I 中不同文档 ID 下的字段中的数据,认为它们是问题所在从 Firestore 获取数据。

firestore 的屏幕截图

public class ListenOrder extends Service {

    FirebaseFirestore firebaseFirestore;
    FirebaseAuth firebaseAuth;
    String UID;
    CollectionReference requests;

    public ListenOrder() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        firebaseAuth=FirebaseAuth.getInstance();
        UID = Objects.requireNonNull(firebaseAuth.getCurrentUser()).getPhoneNumber();
        firebaseFirestore=FirebaseFirestore.getInstance();
        requests=firebaseFirestore.collection("Requests").document(UID).collection("RequestStatus");
        requests.addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                assert queryDocumentSnapshots != null;
                for(DocumentChange documentChange: queryDocumentSnapshots.getDocumentChanges()){
                    switch (documentChange.getType()) {
                        case ADDED:

                            break;
                        case MODIFIED:
                            String OrderID=documentChange.getDocument().getString("OrderID");
                            String doc= documentChange.getDocument().getString("Status");
                            showNotification(OrderID,doc);
                            break;
                        case REMOVED:

                            break;
                    }

                }
            }
        });
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return super.onStartCommand(intent, flags, startId);
    }

    private void showNotification(String orderid, String status) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel notificationChannel=
                    new NotificationChannel("n","n",NotificationManager.IMPORTANCE_DEFAULT);
            NotificationManager manager=getSystemService(NotificationManager.class);
            manager.createNotificationChannel(notificationChannel);
        }

        Intent intent=new Intent(this,order_status_activity.class);
        PendingIntent contentIntent=PendingIntent.getActivity(this,0,intent,0);

        NotificationCompat.Builder builder=new NotificationCompat.Builder(this,"n");
        builder.setAutoCancel(true)
                .setColor(ContextCompat.getColor(this, R.color.White))
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setContentIntent(contentIntent)
                .setContentText("Order was updated "+orderid+" to "+ConvertCodeToStatus(status))
                .setContentTitle("Notification Actions")
                .setSmallIcon(R.mipmap.ic_launcher);
        builder.addAction(android.R.drawable.ic_menu_view, "VIEW", contentIntent);
        NotificationManagerCompat notificationManagerCompat= NotificationManagerCompat.from(this);
        //NotificationManagerCompat notificationManager= (NotificationManagerCompat) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManagerCompat.notify(999,builder.build());
    }

    private String ConvertCodeToStatus(String status) {
        switch (status) {
            case "0":
                return "Placed";
            case "1":
                return "Processed";
            case "2":
                return "Dispatched";
            default:
                return "Delivered";
        }

    }

}

这就是答案,我只需要创建频道 ID