首页 > 解决方案 > FCM Android - 点击通知 - 打开网页视图

问题描述

我使用的模板:https ://github.com/mgks/Android-SmartWebView 实际上,该模板没有 fcm 功能。我手动添加的。我在这里提到,以便您查看 mainactivity 文件。

当用户单击我的通知时,我想打开特定链接。

我的通知生成器是:

// pending implicit intent to view url
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("LINK",link);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
                .setLargeIcon(BitmapFactory.decodeResource(getBaseContext().getResources(), R.mipmap.ic_launcher))  //set it in the notification
                .setSmallIcon(R.mipmap.ic_launcher)  //a resource for your custom small icon
                .setContentTitle(title) //the "title" value you sent in your notification
                .setContentText(message) //ditto
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)  //dismisses the notification on click
                .setSound(defaultSoundUri)
                .setStyle(new NotificationCompat.BigPictureStyle()
                        .setSummaryText(message)
                        .bigPicture(bitmap)
                        .setBigContentTitle(title));

我在 MainActivity 中试过这个:

Intent intent = getIntent();
        if (getIntent().getExtras() != null && getIntent().getExtras().getString("link", null) != null && !getIntent().getExtras().getString("link", null).equals("")) {
            String url = null;
            if (getIntent().getExtras().getString("link").contains("http")) {
                url = getIntent().getExtras().getString("link");
            } else {
                url = "http://" + getIntent().getExtras().getString("link");
            }
            aswm_view(url, false);          
        } else {
            //Rendering the default URL
            aswm_view(ASWV_URL, false);
        }
        (near lines 250)

但是,没有任何工作。谁能帮帮我吗?

谢谢你。

标签: androidfirebase-cloud-messagingandroid-webview

解决方案


我认为你应该使用intent.getString()而不是intent.getExtras().getString()

Intent intent = getIntent();
String url = getIntent().getString("link", null)
if (url != null) {
    if (!url.startsWith("http")) {
        url = "http://" + url;
    }
    aswm_view(url, false);          
} else {
    //Rendering the default URL
    aswm_view(ASWV_URL, false);
}

根据Intent.getExtrasintent.getExtras()将返回null,因为它从未被分配。

返回之前使用 putExtra() 添加的所有附加项的映射,如果没有添加则返回 null


推荐阅读