Firebase FCM 通知添加操作按钮

vas*_*lph 6 web-push firebase-cloud-messaging firebase-notifications

如何添加操作按钮来推送这样的通知: 在此处输入图片说明 我试过这个,但它不起作用:

=> https://firebase.googleblog.com/2018/05/web-notifications-api-support-now.html

这是我的通知负载:

array
(
    "title" =>  "FCM Message",
    "body" => "This is an FCM Message",
    "click_action" => "http://example.com/",
    "icon" => "/logo.jpg",
    "actions" => array(
        0 => array(
            'title' => 'Like',
            'click_action' => 'http://example.com/?aaa=1',
            'icon' => 'icons/heart.png',
        ),
        1 => array(
            'title' => 'Unsubscribe',
            'click_action' => 'http://example.com/?aaa=2',
            'icon' => 'icons/cross.png',
        ),
    ),
);
Run Code Online (Sandbox Code Playgroud)

我尝试使用消息有效负载也不起作用:

$msg = array
(
    "webpush" =>  array
    (
        "notification" =>  array
        (
            "title" =>  "Fish Photos ",
            "body" =>  "Thanks for signing up for Fish Photos! You now will receive fun daily photos of fish!",
            "icon" =>  "firebase-logo.png",
            "image" =>  "guppies.jpg",
            "data" =>  array
            (
                "notificationType" =>  "fishPhoto",
                "photoId" =>  "123456",
            ),
            "click_action" =>  "https://example.com/fish_photos",
            "actions" =>  array(
                0 => array(
                    'title' => 'Like',
                    'action' => 'like',
                    'icon' => 'icons/heart.png',
                ),
                1 => array(
                    'title' => 'Unsubscribe',
                    'action' => 'unsubscribe',
                    'icon' => 'icons/cross.png',
                ),
            ),
        ),

    ),
);
Run Code Online (Sandbox Code Playgroud)

Suk*_*khi 2

在 Android 上,您必须使用 RemoteInput 并对通知“应用”操作。以下是摘要,以下是详细信息。

public static final String NOTIFICATION_REPLY = "NotificationReply";

RemoveInput removeInput = new RemoteInput.Builder((Notification_REPLY))
    .setLabel("Approve Comments")
    .build();
Run Code Online (Sandbox Code Playgroud)

然后为回复操作创建一个 PendingIntent ,如下所示:

PendingIntent acceptPendingIntent = PendingIntent.getBroadcast(
    context:this,
    REQUEST_CODE_APPROVE,
    new Intent(packageContext:this,NotificationReciver.class)
        .putExtra(KEY_INTENT_APPROVE,REQUEST_CODE_APPROVE),
    PendingIntent.FLAG_UPDATE_CURRENT
);
Run Code Online (Sandbox Code Playgroud)

然后使用 addRemoteInput() 将 RemoteInput 对象附加到操作

NotificationCompat.Action action =
    new NotificationCompat.Action.Builder(ic_delete,
        title:"Approve", acceptPendingIntent)
        .addRemoteInput(remoteInput)
        .build();
Run Code Online (Sandbox Code Playgroud)

最后,您必须将操作应用于通知和显示。

NotificationCompat.builder = notificaitonBuilder = new NotificationCompat.Builder(context:this,channelId:"channel_id")
    .addAction(action)
    // set rest of notification attributes e.g. title, auto cancel, icon etc.
Run Code Online (Sandbox Code Playgroud)

您可以从 Firebase 通知的“data”属性传递所需的信息。您甚至必须使用 onReceive() 在消息底部附加回复/按钮。

是另一个有用的链接。

在此输入图像描述

  • 问题是针对网络推送,而不是android (5认同)