通知在从Firestore更改数据时未显示。

huangapple go评论74阅读模式
英文:

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";
        }

    }

}

Note: Please let me know if you need further assistance or if you encounter any issues.

英文:
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(&quot;Requests&quot;).document(UID).collection(&quot;RequestStatus&quot;);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
requests.addSnapshotListener(new EventListener&lt;QuerySnapshot&gt;() {
@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(&quot;Status&quot;);
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(&quot;Order was updated status to &quot;+ConvertCodeToStatus(status))
.setContentTitle(&quot;Notification Actions&quot;)
.setSmallIcon(R.mipmap.ic_launcher);
builder.addAction(android.R.drawable.ic_menu_view, &quot;VIEW&quot;, contentIntent);
NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID,builder.build());
}
private String ConvertCodeToStatus(String status) {
switch (status) {
case &quot;0&quot;:
return &quot;Placed&quot;;
case &quot;1&quot;:
return &quot;Processed&quot;;
case &quot;2&quot;:
return &quot;Dispatched&quot;;
default:
return &quot;Delivered&quot;;
}
}
}

When I signing out the app the app is crashing and showing this error

java.lang.NullPointerException: Attempt to invoke virtual method &#39;java.util.List com.google.firebase.firestore.QuerySnapshot.getDocumentChanges()&#39; 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)

And when I changing the value of field (Status) from firestore no notification is showing.I am changing the data from the field which is under different document ID in firestore.I think their is problem in getting data from firestore.

Screenshots of firestore

通知在从Firestore更改数据时未显示。

通知在从Firestore更改数据时未显示。

答案1

得分: 1

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";
        }

    }

}
英文:
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(&quot;Requests&quot;).document(UID).collection(&quot;RequestStatus&quot;);
requests.addSnapshotListener(new EventListener&lt;QuerySnapshot&gt;() {
@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(&quot;OrderID&quot;);
String doc= documentChange.getDocument().getString(&quot;Status&quot;);
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 &gt;= Build.VERSION_CODES.O){
NotificationChannel notificationChannel=
new NotificationChannel(&quot;n&quot;,&quot;n&quot;,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,&quot;n&quot;);
builder.setAutoCancel(true)
.setColor(ContextCompat.getColor(this, R.color.White))
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentIntent(contentIntent)
.setContentText(&quot;Order was updated &quot;+orderid+&quot; to &quot;+ConvertCodeToStatus(status))
.setContentTitle(&quot;Notification Actions&quot;)
.setSmallIcon(R.mipmap.ic_launcher);
builder.addAction(android.R.drawable.ic_menu_view, &quot;VIEW&quot;, 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 &quot;0&quot;:
return &quot;Placed&quot;;
case &quot;1&quot;:
return &quot;Processed&quot;;
case &quot;2&quot;:
return &quot;Dispatched&quot;;
default:
return &quot;Delivered&quot;;
}
}
}

This the answer, I just have to create channel Id

huangapple
  • 本文由 发表于 2020年8月29日 15:25:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63644523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定