android - webmethod 不调用 firebase 通知 onMessageReceived 方法

标签 android android-asynctask firebase firebase-cloud-messaging

public void onMessageReceived(RemoteMessage remoteMessage) {

    try {

        String type=remoteMessage.getData().get("type");
        if(type.equals("1001")) {
            CommonClass common = new CommonClass(getApplication());
            CommonClass.MyTaskSendLog.execute(getApplicationContext(), DeviceDetails,lines);
        }            
    } catch (Exception ex) {
    }
}

此代码给出错误:

Method execute must be called from the main thread, currently inferred thread is worker

最佳答案

我想你的方法在服务中。

要访问服务中的 UI 线程(主线程),您必须创建一个处理程序并在其中调用方法,如下所示:

if(type.equals("1001")) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
                  public void run() {
                          CommonClass common = new CommonClass(getApplication());
                          CommonClass.MyTaskSendLog.execute(getApplicationContext(), DeviceDetails,lines);
                  }
    });
}  

您可以在服务的onCreate 中创建Handler。

关于android - webmethod 不调用 firebase 通知 onMessageReceived 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38454122/

相关文章:

android - 如何从 AsyncTask 更新 ListView?

android - 在 Android 的 webview 中使用后退按钮

java - YouTubePlayer.Provider 初始化方法引起的内存泄漏

android - 如何为 RoboGuice 禁用 RoboBlender

Android:如何将参数传递给 AsyncTask 的 onPreExecute()?

android - AsyncTask 从 doInBackground 保存变量

angular - 将 ID 保存到 Firestore 文档

android - ML Kit - Android - 文本识别 - 文本方向

android - 如何通过编程方式检查android中的移动网络范围

firebase - 使用带有 vuefire 手动绑定(bind)的 Firebase 用户 UID