问题描述
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,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)
当我从Firestore更改字段(Status)的值时没有显示通知。我正在更改来自Firestore中不同文档ID下的字段的数据。我认为这是从Firestore获取数据的问题。 / p>
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,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