我想重新加载我的Web视图,当收到通知并点击通知时,它会在我的MainActivity中从FirebaseMessagingService。
这是我到目前为止的代码。
FirebaseMessagingService.javainonMessageReceived方法我有
if(MainActivity.isAppRunning){
Delegate.theMainActivity.onNotificationRefresh();
}
委托.java
package ###@@##@@;
public class Delegate {
static MainActivity theMainActivity;
}
在我的主活动中,有我试图调用的这种方法
public void onNotificationRefresh() {
webView.loadUrl("www.google.com");
}
现在,当每个我收到通知而不是调用重新加载应用程序崩溃时。
错误日志
FATAL EXCEPTION: pool-3-thread-1
Process: ###@@##@@, PID: 11092
java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'pool-3-thread-1'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {e88a3d1} called on null, FYI main Looper is Looper (main, tid 1) {e88a3d1})
at android.webkit.WebView.checkThread(WebView.java:2588)
at android.webkit.WebView.loadUrl(WebView.java:1005)
at com.###@@##@@.MainActivity.onNotificationRefresh(MainActivity.java:293)
at com.###@@##@@.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:88)
at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(Unknown Source)
at com.google.firebase.iid.zzc.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
试试这样,你可以使用BroadcastReceiver
示例代码
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.e("NOTIFICATION_DATA", remoteMessage.getData() + "");
Intent new_intent = new Intent();
Bundle bundle = new Bundle();// use bundle if you want to pass data
bundle.putString("msgBody", remoteMessage.getData().toString());
new_intent.putExtra("msg", bundle);
new_intent.setAction("ACTION_ACTIVITY");
sendBroadcast(new_intent);
}
}
比像这样在您的活动中使用
public class MyActivity extends AppCompatActivity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
@Override
protected void onResume() {
super.onResume();
// registering BroadcastReceiver
if (activityReceiver != null) {
IntentFilter intentFilter = new IntentFilter("ACTION_ACTIVITY");
registerReceiver(activityReceiver, intentFilter);
}
}
@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(activityReceiver);
}
private BroadcastReceiver activityReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// reload your webview here
webView.loadUrl("https://stackoverflow.com/users/7666442/nilesh-rathod?tab=profile");
}
};
}