webmethod 不调用 firebase 通知 onMessageReceived 方法
webmethod not call on firebase notification onMessageReceived method
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
服务中创建处理程序。
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
服务中创建处理程序。