gpt4 book ai didi

java - 无法向推送通知添加操作/按钮

转载 作者:行者123 更新时间:2023-12-01 18:39:38 24 4
gpt4 key购买 nike

我正在使用 Firebase Admin SDK (Java)向 Android 设备发送推送通知。这一切都运行良好。

我正在寻找添加通知操作按钮。 Example from this of desired outcome (用于网络推送)

问题

我在 SDK 中找不到任何可以向通知添加操作的内容。

有人使用操作来创建推送通知按钮吗?

谢谢

最佳答案

在您的 Android 应用程序中尝试一下。如果您愿意,可以根据通知中的信息创建一个 Intent:

对您来说真正重要的是我创建NotificationCompat.Action 的部分。

private void showPushNotification(RemoteMessage remoteMessage) {
int requestCode = (int) (Math.random()*3000);
int id = (int) (Math.random()*300000);
String channelId = "fcm_defaul_channel_id"; // FIXME: set channelId
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Bitmap iconLarge = BitmapFactory.decodeResource(getResources(),
R.drawable.googleg_standard_color_18);

// title and body
title = remoteMessage.getData().get("title"); //or remoteMessage.getNotification().getTitle()
body = remoteMessage.getData().get("body"); or remoteMessage.getNotification().getBody()
intent = new Intent(this, MainActivity.class);

// create action send sms
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action action = new NotificationCompat.Action.Builder(
0, "action test", pendingIntent ).build();

NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.common_full_open_on_phone) // FIXME: change images
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(smsMessageIntent)
.addAction(action);

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

// Since android Oreo notification channel is needed.
if (notificationManager != null) {
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(id, notificationBuilder.build());
} else { // FIXME: eliminare dopo i test
Log.d(TAG, "notificationManager null");
Toast.makeText(this, "notificationManager null", Toast.LENGTH_SHORT).show();
}

关于java - 无法向推送通知添加操作/按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59964513/

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