gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:05:59 24 4
gpt4 key购买 nike

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

这是我的 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());
}

有谁知道确切的实现是什么或这段代码有什么问题吗?

提前致谢。

最佳答案

为了更好地理解这个主题,我建议你阅读这篇文章 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());
}

关于android - FCM 通知 click_action 应该重定向到打开浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54592573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com