Java Can';t添加操作/按钮以推送通知

Java Can';t添加操作/按钮以推送通知,java,android,firebase,firebase-cloud-messaging,Java,Android,Firebase,Firebase Cloud Messaging,我正在使用Firebase向Android设备发送推送通知。 这一切都很好 我想为通知添加一个操作按钮。 (用于web推送) 问题 我在SDK中找不到任何可以向通知添加操作的内容 是否有人使用操作创建按钮以发送推送通知 谢谢在您的Android应用程序中尝试此功能。如果需要,可以根据通知中的信息创建意图: 对你来说真正重要的是我创建NotificationCompat.Action时的部分 private void showPushNotification(RemoteMessage remot

我正在使用Firebase向Android设备发送推送通知。 这一切都很好

我想为通知添加一个操作按钮。 (用于web推送)

问题

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

是否有人使用操作创建按钮以发送推送通知


谢谢

在您的Android应用程序中尝试此功能。如果需要,可以根据通知中的信息创建意图:

对你来说真正重要的是我创建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();
    }

谢谢你的回答。我正在使用服务器端。试图找出如何从通知到应用程序中执行驱动程序操作(如果可能)。与此类似的是[Web Push[()示例。您的代码正在应用程序上运行,我希望根据服务器端代码显示操作/按钮以生成推送消息。