android - 从 FirebaseMessagingService 重新加载 Webview

标签 android firebase android-webview firebase-cloud-messaging

每当收到通知并点击通知时,我想从 FirebaseMessagingService 重新加载我的 Webview,它位于我的 MainActivity 中。

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

FirebaseMessagingService.javaonMessageReceived 方法中我有

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

Delegate.java

package ###@@##@@;

public class Delegate {
    static MainActivity theMainActivity;
}

在我的 MainActivity 中有一个我正在尝试调用的方法

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


    }
}

比这样在你的 Activity 中使用

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

}

关于android - 从 FirebaseMessagingService 重新加载 Webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51761347/

相关文章:

android - Android应用程序读钱

java - 通过网络发送文件 - JAVA

android - shouldInterceptRequest 在使用非弃用时为弃用和非弃用变体调用两次

java - 从 Android 应用程序将大数据上传到服务器的最佳方式是什么?

javascript - Firebase自定义身份验证: Security of Firebase Secret in JavaScript

firebase - 通过 Firebase 仪表板(或 cli)禁用 Firebase 的云功能

swift - 新的 FUITableViewDataSource - 如何使用? swift 3

android - Android WebView中拦截HTML5视频源请求

android - 如果网页无法加载,如何在 webview 中显示默认图像

android - 在 Android 上移动到 SD 卡