FCM 通知 click_action 应重定向到打开浏览器

Posted

技术标签:

【中文标题】FCM 通知 click_action 应重定向到打开浏览器【英文标题】:FCM notification click_action should redirect to open browser 【发布时间】:2019-07-02 16:55:30 【问题描述】:

我的fcm点击操作有问题,收到通知后,当我点击它时,它应该打开浏览器,进入“www.google.co.in”,但是当我点击通知时,它会打开默认主活动。

这是我的 onMessageReceived() :

@Override
public void onMessageReceived(RemoteMessage remoteMessage) 
  Log.d(TAG, "Message Data Payload:" + remoteMessage.getData());       
  Log.d(TAG, "Message notification body: " + remoteMessage.getNotification().getBody());
  Intent intent = new Intent(Intent.ACTION_VIEW);
  intent.setData(Uri.parse("www.google.co.in"));
  PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.ic_stat_ic_notification)
                    .setContentTitle("FCM Message")
                    .setContentText("FCM MessageBody")
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
        NotificationChannel channel = new NotificationChannel(channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    

    notificationManager.notify(0, notificationBuilder.build());

有人知道这段代码的具体实现是什么或有什么问题吗?

提前致谢。

【问题讨论】:

【参考方案1】:

为了更好地理解这个话题,我建议你阅读这篇文章 firebase cloud messaging

@Override
public void onMessageReceived(RemoteMessage remoteMessage) 
  Log.i(TAG, "Message Data Payload:" + remoteMessage.getData());       
  Log.i(TAG, "Message notification body: " + remoteMessage.getNotification().getBody());

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) 
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

            switch (remoteMessage.getNotification().getBody()) 

                case "openUrlGoogle":
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse("www.google.co.in"));
                    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
                    break;

                case "openUrlOtherSite":
                    break;

                default:
                    Log.v("TAG", "error");
                    break;
            

        

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.ic_stat_ic_notification)
                    .setContentTitle("FCM Message")
                    .setContentText("FCM MessageBody")
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
        NotificationChannel channel = new NotificationChannel(channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    

    notificationManager.notify(0, notificationBuilder.build());

【讨论】:

我阅读了 FCM 文档并进行了搜索,上面的代码仅在应用程序处于前台时有效,但如果应用程序处于后台/被杀死,fcm 会将数据消息发送到默认活动,我们必须使用 Intent 的附加功能进行检索,我参考了此链接 - firebase.google.com/docs/cloud-messaging/android/… 所以当应用程序在后台/被杀死时不会调用 onMessageReceived()。 即使代码仅在应用程序处于前台时设置自定义通知,当我点击它并且通知消失时它也不会重定向。 我还面临一个问题,即当应用程序被终止时,当用户点击通知时,没有调用 onMessageReceived()。 @Satish 你找到解决方案了吗? 试试这个@SharathHuddar dontkillmyapp.com

以上是关于FCM 通知 click_action 应重定向到打开浏览器的主要内容,如果未能解决你的问题,请参考以下文章

Firebase FCM通知click_action有效内容

当应用程序处于后台状态时,FCM多次推送通知无法正常工作

FCM通知打开第二个通知时第二次获得相同的数据

HTTP v1 API:用于 webpush 通知的“click_action”

Firebase click_action无效

单击 fcm 通知而不是打开启动器活动时打开浏览器