首页 > 解决方案 > 添加启动画面时,webview URL 未通过 FCM 更新

问题描述

我按照本教程制作了一个简单的 webview 应用程序,其中 webview 的 url 可以从 Firebase 通知的键和值更新,webview 加载静态 url,但也加载在 FCM 的值中指定的新 url通知

onNewIntent(getIntent());从中获取意图,其中mRegistrationBroadcastReceiver包含 key:webURL和 value:https://www.newurl.com/存储在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);
    }
}

标签: javaandroidfirebase-cloud-messaging

解决方案


我认为您应该在不使用布局文件的情况下实现启动屏幕,因为从专用启动屏幕活动启动第二个活动会有延迟。

这篇文章可能会有所帮助-以正确的方式创建启动画面

这样,启动画面只会在应用程序处于加载状态时显示。因此,即使应用程序在后台运行,它也应该可以防止您的问题。


推荐阅读