Java 当数据从firestore更改时,不显示通知

Java 当数据从firestore更改时,不显示通知,java,android,google-cloud-firestore,android-notifications,Java,Android,Google Cloud Firestore,Android Notifications,当我从firestore更改字段(状态)的值时,没有显示任何通知。我正在更改firestore中不同文档ID下的字段中的数据。我认为他们在从firestore获取数据时遇到了问题 firestore的截图 公共类ListNorder扩展服务{ FirebaseFirestore FirebaseFirestore; FirebaseAuth FirebaseAuth; 字符串UID; 收集参考请求; 公共列表器(){ } @凌驾 公共IBinder onBind(意向){ 返回null; }

当我从firestore更改字段(状态)的值时,没有显示任何通知。我正在更改firestore中不同文档ID下的字段中的数据。我认为他们在从firestore获取数据时遇到了问题

firestore的截图

公共类ListNorder扩展服务{
FirebaseFirestore FirebaseFirestore;
FirebaseAuth FirebaseAuth;
字符串UID;
收集参考请求;
公共列表器(){
}
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
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(新的EventListener(){
@凌驾
public void OneEvent(@Nullable QuerySnapshot queryDocumentSnapshots,@Nullable FirebaseFirestoreException e){
断言queryDocumentSnapshots!=null;
对于(DocumentChange DocumentChange:queryDocumentSnapshots.getDocumentChanges()){
开关(documentChange.getType()){
案件补充:
打破
案例修改:
String OrderID=documentChange.getDocument().getString(“OrderID”);
String doc=documentChange.getDocument().getString(“状态”);
showNotification(订单ID、单据);
打破
已删除的案例:
打破
}
}
}
});
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
返回super.onStartCommand(intent、flags、startId);
}
私有void showNotification(字符串orderid、字符串状态){
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O){
通知频道通知频道=
新建NotificationChannel(“n”,“n”,NotificationManager.IMPORTANCE\u默认值);
NotificationManager=getSystemService(NotificationManager.class);
manager.createNotificationChannel(notificationChannel);
}
意向意向=新意向(此,订单状态活动类);
PendingEvent contentIntent=PendingEvent.getActivity(this,0,intent,0);
NotificationCompat.Builder=新的NotificationCompat.Builder(此“n”);
builder.setAutoCancel(真)
.setColor(ContextCompat.getColor(this,R.color.White))
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_启动器))
.setContentIntent(contentIntent)
.setContentText(“订单已更新”+orderid+“为”+ConvertCodeToStatus(状态))
.setContentTitle(“通知操作”)
.setSmallIcon(R.mipmap.ic_启动器);
builder.addAction(android.R.drawable.ic_菜单_视图,“视图”,contentIntent);
NotificationManagerCompat NotificationManagerCompat=NotificationManagerCompat.from(此);
//NotificationManagerCompat notificationManager=(NotificationManagerCompat)getSystemService(Context.NOTIFICATION_服务);
notificationManagerCompat.notify(999,builder.build());
}
私有字符串ConvertCodeToStatus(字符串状态){
开关(状态){
案例“0”:
返回“放置”;
案例“1”:
返回“已处理”;
案例“2”:
返回“已发送”;
违约:
返回“已交付”;
}
}
}

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

请编辑您的问题并将您的数据库结构添加为屏幕截图。@AlexMamo我现在已添加了屏幕截图,请给我解决方案。您是否在(DocumentChange DocumentChange:queryDocumentSnapshots.getDocumentChanges()的这一行
中遇到此错误
?是的,我在这一行以及这一行的“requests.addSnapshotListener(new EventListener(){”中得到了错误。您是否尝试移动onCreate中的所有代码以查看它是否有效?
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$1.onEvent(ListenOrder.java:81)
        at com.example.businessplan1.ListenOrder$1.onEvent(ListenOrder.java:77)
        at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2(com.google.firebase:firebase-firestore@@21.4.1:1038)
        at com.google.firebase.firestore.Query$$Lambda$3.onEvent(Unknown Source:6)
        at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0(com.google.firebase:firebase-firestore@@21.4.1:42)
        at com.google.firebase.firestore.core.AsyncEventListener$$Lambda$1.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)
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";
        }

    }

}