[英]webview URL not updating via FCM when Splash Screen is added
我按照本教程制作了一個簡單的 webview 應用程序,其中 webview 的 url 可以從 Firebase 通知的鍵和值更新,webview 加載靜態 url,但也加載 FCM 值中指定的新 url通知
onNewIntent(getIntent());
從mRegistrationBroadcastReceiver
獲取包含 key: webURL
和 value: https://www.newurl.com/
存儲在STR_KEY
中的STR_KEY
。
一切正常,當應用程序在后台和前台時,url 會完美更新......直到我在應用程序中添加一個啟動畫面,在這種情況下,當應用程序在后台並且 FCM 通知到達時,url 的webview 不會更新,但是當它在前台時會更新。
我最好的猜測是Intent intent = new Intent(getBaseContext(), MainActivity.class);
丟失是因為啟動畫面首先加載而不是主要活動,但我是一個菜鳥,這超出了我擁有的 3 個神經元。
這是我的清單:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
<service android:name=".MyService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
我的 FCM 服務:
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyService extends FirebaseMessagingService {
public static final String STR_KEY = "webURL";
public static final String STR_PUSH = "pushNotification";
public static final String STR_MESSAGE = "message";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
handleMessage(remoteMessage.getData().get(STR_KEY));
}
private void handleMessage(String message) {
Intent pushNotification = new Intent(STR_PUSH);
pushNotification.putExtra(STR_MESSAGE, message);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
}
@Override
public void onNewToken(String token) {
super.onNewToken(token);
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token) {
Log.d("new Token", token);
}
}
我的主要活動:
public class MainActivity extends AppCompatActivity {
WebView webView;
private BroadcastReceiver mRegistrationBroadcastReceiver;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.getSettings().setPluginState(WebSettings.PluginState.OFF);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setUserAgentString("Android Mozilla/5.0
AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().supportZoom();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.mywebsite.com/");
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(STR_PUSH)) {
String message = intent.getStringExtra(STR_MESSAGE);
showNotification("New URL", message);
}
}
};
onNewIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
webView.loadUrl(intent.getStringExtra(STR_KEY));
}
private void showNotification(String title, String message) {
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.putExtra(STR_KEY, message);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(getBaseContext());
builder.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(contentIntent);
NotificationManager notificationManager = (NotificationManager)
getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = getString(R.string.normal_channel_id);
String channelName = getString(R.string.normal_channel_name);
NotificationChannel channel = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_DEFAULT);
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{100, 200, 200, 50});
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
builder.setChannelId(channelId);
}
if (notificationManager !=null) {
notificationManager.notify("", 0, builder.build());
}
}
@Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver
(mRegistrationBroadcastReceiver);
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver
(mRegistrationBroadcastReceiver, new IntentFilter("registrationComplete"));
LocalBroadcastManager.getInstance(this).registerReceiver
(mRegistrationBroadcastReceiver, new IntentFilter(STR_PUSH));
}
}
還有我的啟動畫面:
public class Splash extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
int SPLASH_TIME_OUT = 4000;
new Handler().postDelayed(() -> {
Intent homeIntent = new Intent(Splash.this, MainActivity.class);
startActivity(homeIntent);
finish();
}, SPLASH_TIME_OUT);
}
}
我認為您應該在不使用布局文件的情況下實現啟動畫面,因為從專用啟動畫面活動啟動第二個活動會出現延遲。
這篇文章可能會有所幫助 - 以正確的方式創建啟動畫面
這樣,啟動畫面只會在應用程序處於加載狀態時顯示。 因此,即使應用程序在后台運行,它也應該可以防止您的問題。
public class MainActivity extends AppCompatActivity {
private WebView webView;
private ProgressDialog dialog;
private BroadcastReceiver mRegistrationBroadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.gsmarena.com/samsung_galaxy_note20-10338.php");
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onPermissionRequest(final PermissionRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
request.grant(request.getResources());
}
// super.onPermissionRequest(request);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
if (dialog.isShowing())
dialog.dismiss();
}
});
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Config.STR_PUSH)) {
String message = intent.getStringExtra(Config.STR_MESSAGE);
showNotification("this is a notification", message);
}
}
};
onNewIntent(getIntent());
}
@SuppressLint("MissingSuperCall")
@Override
protected void onNewIntent(Intent intent) {
dialog = new ProgressDialog(this);
if (intent.getStringExtra(Config.STR_KEY) != null) {
dialog.show();
dialog.setMessage("Please wait loading");
webView.loadUrl(intent.getStringExtra(Config.STR_KEY));
}
}
private void showNotification(String title, String message) {
// Intent intent = new Intent(getBaseContext(), MainActivity.class);
Intent intent = new Intent(this, MainActivity.class);
// intent.setAction(Intent.ACTION_MAIN);
// intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.putExtra(Config.STR_KEY, message);
// intent.setFlags(intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_SINGLE_TOP | intent.FLAG_ACTIVITY_CLEAR_TOP);
// PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
// NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext());
Notification.Builder builder = new Notification.Builder(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(contentIntent)
.setPriority(Notification.PRIORITY_HIGH)
.setVibrate(new long[]{100, 200, 200, 50})
.setColor(Color.rgb(247, 133, 39))
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
}
// NotificationManager notificationManager = (NotificationManager)getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "ChannelID";
String channelName = "ChannelName";
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.setLightColor(Color.RED);
channel.enableVibration(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
channel.setShowBadge(true);
channel.setVibrationPattern(new long[]{100, 200, 200, 50});
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
builder.setChannelId(channelId);
}
if (notificationManager != null) {
notificationManager.notify("", 0, builder.build());
}
// notificationManager.notify(1, builder.build());
}
@Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, new IntentFilter("registrationComplete"));
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, new IntentFilter(Config.STR_PUSH));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.