我有以下代码,它在Android 8.0及更高版本上崩溃,但在较旧的Android版本上正常运行:
package com.myapp.firebase;
import android.os.Handler;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.myapp.utils.SendPushTokenToServer;
public class CloudMessaging extends FirebaseMessagingService {
Handler handler;
@Override
public void onNewToken(final String refreshToken) {
super.onNewToken(refreshToken);
handler = new Handler();
if (refreshToken != null) {
if (!refreshToken.equals("") && !refreshToken.equals("null")) {
// new SendPushTokenToServer(getApplicationContext(), refreshToken).execute();
runOnUiThread(new Runnable() {
@Override
public void run() {
new SendPushTokenToServer(getApplicationContext(), refreshToken).execute();
}
});
}
}
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("MyApp", "Message From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d("MyApp", "Message data payload: " + remoteMessage.getData());
// if (/* Check if data needs to be processed by long running job */ true) {
// // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
// scheduleJob();
// } else {
// // Handle message within 10 seconds
// handleNow();
// }
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d("MyApp", "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
}
private void runOnUiThread(Runnable runnable) {
handler.post(runnable);
}
}
我试图在主线程上运行令牌上传AsyncTask,但也是如此。
对于错误,当我尝试启动AsyncTask时遇到RunTimeException异常,但是我没有得到更详细的错误信息。
编辑:这是(不太有用)日志文件:
java.lang.RuntimeException:在android.os.Handler上。 (Handler.java:203)在android.os.Handler上。 (Handler.java:117)
在com.myapp.firebase.CloudMessaging.onNewToken (CloudMessaging.java:26)在 com.google.firebase.messaging.FirebaseMessagingService.zzd(未知 来源:423),位于com.google.firebase.iid.zzg.run(未知来源:26)
在java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1162)在 java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:636)在 com.google.android.gms.common.util.concurrent.zza.run(未知 来源:7),位于java.lang.Thread.run(Thread.java:764)