我创建了一个应用,该应用获取用户的电话令牌,以便能够在应用聊天中相互发送消息。
我注意到打开应用程序后第一次安装时会崩溃。
我得到的错误是:
if request.method == "POST":
bid = Bids.objects.filter(auction=item_id).aggregate(Max('bid_value'))['bid_value__max']
.....
它出现在此行:
E/AndroidRuntime: FATAL EXCEPTION: Firebase-Messaging-Intent-Handle
Process: com.example.app, PID: 9048
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.firestore.CollectionReference com.google.firebase.firestore.FirebaseFirestore.collection(java.lang.String)' on a null object reference
at com.example.app.service.MyFirebaseMessagingService.onNewToken(MyFirebaseMessagingService.java:94)
at com.google.firebase.messaging.FirebaseMessagingService.zzc(com.google.firebase:firebase-messaging@@20.2.1:108)
at com.google.firebase.messaging.zzh.run(com.google.firebase:firebase-messaging@@20.2.1:2)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at com.google.android.gms.common.util.concurrent.zza.run(com.google.android.gms:play-services-basement@@17.1.1:6)
at java.lang.Thread.run(Thread.java:764)
I/Process: Sending signal. PID: 9048 SIG: 9
Disconnected from the target VM, address: 'localhost:8601', transport: 'socket'
据我了解,由于没有public class MyFirebaseMessagingService extends FirebaseMessagingService {
private FirebaseAuth auth;
private FirebaseFirestore db;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
createNotificationChannel();
notifyThis(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"),remoteMessage.getData().get( "uniqueID" ),remoteMessage.getData().get("var1"),remoteMessage.getData().get("var2"),remoteMessage.getData().get("var3"));
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//I CREATE NOTIFICATION HERE
}
}
public void notifyThis(String title, String message, String uniqueID, String var1, String var2, String var3) {
//I CREATE NOTIFICATION HERE
}
@Override
public void onNewToken(@NonNull String token) {
CollectionReference usernamedb = db.collection( "User" ).document( auth.getUid() ).collection( "Data" ); <-----HERE I GET ERROR
usernamedb.get().addOnCompleteListener( task2 -> {
if (task2.isSuccessful()) {
for (QueryDocumentSnapshot document : task2.getResult()) {
Map<String, Object> user_token = new HashMap<>();
user_token.put( "Token", token );
db.collection( "User" ).document( auth.getUid() ).collection( "Data" ).document(document.getId()).update(user_token);
}
}
} );
}
}
,这是崩溃的,因为这是用户第一次登录。但是,如何确保只有在之后才第一次调用auth.getUid()
用户实际上已登录?