从FirebaseMessagingService重新加载Webview [英] Reloading Webview from FirebaseMessagingService

查看:121
本文介绍了从FirebaseMessagingService重新加载Webview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在通知到达时从FirebaseMessagingService重新加载WebView,并将其从FirebaseMessagingService加载到MainActivity中.

I want to reload my Webview which in in my MainActivity from FirebaseMessagingService when ever a notification arrive and I tap on the notification.

到目前为止,这是我的代码.

Here is my code so far.

FirebaseMessagingService.java

if(MainActivity.isAppRunning){
 Delegate.theMainActivity.onNotificationRefresh();
 }

Delegate.java

package ###@@##@@;

public class Delegate {
    static MainActivity theMainActivity;
}

在我的 MainActivity 中,我正在尝试调用此方法

And in my MainActivity there is this method which i am trying to call

public void onNotificationRefresh() {
        webView.loadUrl("www.google.com");
}

现在,当我收到每条通知而不是调用重新加载应用程序时崩溃.

right now when every i recieve notification instead of calling reload app crashes.

错误日志

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

Try this way you can use 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);      


    }
}

在这样的活动中使用>

Than Use in your activity like this

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");
        }
    };

}

这篇关于从FirebaseMessagingService重新加载Webview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆