[英]Android reload WebView URL onNewToken from Firebase Messaging Center
我正在运行一个指向 WebApp 的 WebView Android 应用程序。 一切还好。 但是现在,我在刷新 url 时遇到了问题,该 url 在调用FirebaseMessagingService
的onNewToken
方法时已经加载,并且还向其中注入了一个 JS。 我听说过Intents
并且也发现了这个问题,但无法真正理解如何以及在何处应用Intents
。
这是我的代码目前的样子:
MyFirebaseMessagingService :
package com.example.app;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMessagingService";
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
// I want to call the loadUrl here using a injected JS to send the token to the Web App
}
}
MyWebViewClient :
package com.example.app;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
class MyWebViewClient extends WebViewClient {
private static final String TAG = "MyWebViewClient";
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String hostname;
// YOUR HOSTNAME
hostname = "192.168.0.11";
final Uri uri = request.getUrl();
Log.i(TAG, "Uri =" + uri);
if (uri.getHost() != null && uri.getHost().endsWith(hostname)) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
view.getContext().startActivity(intent);
return true;
}
}
主要活动:
package com.example.app;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends Activity {
public WebView mWebView;
@Override
@SuppressLint("SetJavaScriptEnabled")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setDatabaseEnabled(true);
mWebView.setWebViewClient(new MyWebViewClient());
// REMOTE RESOURCE
mWebView.loadUrl("http://192.168.0.11:3000/");
// LOCAL RESOURCE
// mWebView.loadUrl("file:///android_asset/index.html");
}
@Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
是的,您必须使用 Intents,但首先在“MyFirebaseMessagingService”中使用 LocalBroadcastManager:
public static String REQUEST_ACCEPT="action.my.messaging";
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
//after any refresh you will notice it to your activity (or fragment)
Intent intent = new Intent(REQUEST_ACCEPT);
intent.putExtra("key", "Any Extra Value Here");
LocalBroadcastManager.getInstance(getBaseContext()).sendBroadcast(intent);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Or use it here after any message received
}
然后在您的活动中,您可以收到任何通知并执行刷新或您想要的任何操作:
@Override
public void onCreate(Bundle savedInstanceState) {
// registering an observer (mMessageReceiver) to receive Intents
// with actions named as the value of REQUEST_ACCEPT.
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter(MyFirebaseMessagingService.REQUEST_ACCEPT));
}
// Our handler for received Intents. This will be called whenever an Intent
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String value = intent.getStringExtra("key");
Log.d(TAG, "Got noticed: " + value );
}
};
@Override
protected void onDestroy() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.