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

时间:2019-02-08 12:32:20

标签: android firebase android-studio android-intent firebase-cloud-messaging

我对fcm点击操作有问题,在收到通知后,当我单击它时,它应该打开浏览器,该浏览器将转到“ www.google.co.in”,但是当我单击通知时,它将打开默认值MainActivity。

这是我的onMessageReceived():

sudo chown -R www-data:www-data /var/www

有人知道此代码的确切实现是什么吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

为了更好地理解本主题,建议您阅读本文。 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());
}