Firebase FCM通知添加操作按钮

时间:2019-09-13 04:08:30

标签: firebase-cloud-messaging firebase-notifications web-push

如何添加操作按钮以按如下方式推送通知:

=> DialogFlow documentation

我尝试了这个,但是不起作用:

=> https://i.ytimg.com/vi/CZ575BuLBo4/maxresdefault.jpg

这是我的通知有效载荷:

@app.route("/sms", methods=['POST'])
def sms_reply():
    """Respond to incoming calls with a simple text message."""
    # Fetch the message
    msg = request.form.get('Body')
    phone_no = request.form.get('From')
    replies = fetch_reply(msg, phone_no)

    # Create reply
    resp = MessagingResponse()
    for reply in replies:
        resp.message(reply.text)
    return str(resp)

我尝试使用消息有效负载也无法正常工作

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',
        ),
    ),
);

2 个答案:

答案 0 :(得分:1)

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

public static final String NOTIFICATION_REPLY = "NotificationReply";

RemoveInput removeInput = new RemoteInput.Builder((Notification_REPLY))
    .setLabel("Approve Comments")
    .build();

然后为回复操作创建一个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
);

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

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

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

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.

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

This是另一个有用的链接。

enter image description here

答案 1 :(得分:-1)

当网站在前台运行时,操作按钮可处理数据负载。 tested on lcoalhost 尝试使用下面的代码按预期工作。

 navigator.serviceWorker.ready.then(function (registration) { 
                                var notificationTitle = payload.notification.title;
                                var notificationOptions = {
                                    body: payload.notification.body,
                                    data: payload.data,
                                    icon: payload.notification.icon,
                                    image: payload.data.Image,
                                    requireInteraction: payload.notification.requireInteraction,
                                    tag: payload.notification.tag,
                                    click_action: payload.data.click_action,
                                    requireInteraction: true,
                                    actions: [{
                                        action: "Test",
                                        title: "Test",
                                        icon: payload.data.action_button,
                                        height: "100px"
                                    }]
                                };
                                registration.showNotification(notificationTitle, notificationOptions);
                            },50)
                        });     

在您的javascript中添加以上代码,即。 app.js

我已经从服务器端发送了有效载荷-c#代码。

            {              
                Image = "/image/img1.jpg",
                click_action = "https://secure.mdg.com/",
                action_button = "/image/button.jpg"                
            };

            var notificationModel = new NotificationEntityModel()
            {
                title = "Testing",
                body = "This is fcm push notification demo",
                Vibrate = new[] { 500, 110, 500, 110, 450, 110, 200, 110, 170, 40, 450, 110, 200, 110, 170, 40, 500 },
                icon = "/image/mdglogo.png",                       
                tag = "Test Notify"                
            };