Cordova推送通知行动

时间:2016-03-03 22:51:23

标签: ios cordova notifications

Cordova当然有发送推送通知的选项,但是任何插件都包含直接向通知添加操作的功能吗?

我尝试搜索“交互式通知”'和'通知行动'插件,但没有产生任何结果。我也试过看一下API reference for phonegap-plugin-push,但没有找到合适的选项或方法。

我正在寻找的一个例子就是当iMessage允许用户在不打开应用程序的情况下回复文本时会发生什么:

iOS push notification action

任何Cordova插件都可以添加推送通知的动作吗?

1 个答案:

答案 0 :(得分:5)

您的通知可以包含操作按钮。对于iOS 8+,您必须在初始化插件时设置可能的操作:

var push = PushNotification.init({
    "ios": {
        "sound": true,
        "vibration": true,
        "badge": true,
        "categories": {
            "invite": {
                "yes": {
                    "callback": "app.accept", "title": "Accept", "foreground": true, "destructive": false
                },
                "no": {
                    "callback": "app.reject", "title": "Reject", "foreground": true, "destructive": false
                },
                "maybe": {
                    "callback": "app.maybe", "title": "Maybe", "foreground": true, "destructive": false
                }
            },
            "delete": {
                "yes": {
                    "callback": "app.doDelete", "title": "Delete", "foreground": true, "destructive": true
                },
                "no": {
                    "callback": "app.cancel", "title": "Cancel", "foreground": true, "destructive": false
                }
            }
        }
    }
});

您会注意到我们在名为categories的init代码的iOS对象中添加了一个新参数。在这种情况下,每个类别都是一个命名对象,邀请和删除。如果您希望显示操作按钮,这些名称将需要与您通过有效负载发送的名称相匹配。每个类别最多可以有三个按钮,必须标记为yes,no和maybe。反过来这些按钮中的每一个都有四个属性,回调你要调用的javascript函数,标题按钮的标签,前景是否将你的应用程序带到前台和破坏性实际上没有做任何破坏性的只是颜色红色按钮作为警告用户该动作可能具有破坏性。

就像后台通知一样,当您成功处理按钮回调时,调用push.finish()绝对至关重要。例如:

app.accept = function(data) {
    // do something with the notification data

    push.finish(function() {
        console.log('accept callback finished');
    }, function() {
        console.log('accept callback failed');
    }, data.additionalData.notId);    
};

您可能会注意到finish方法现在需要成功,失败和id参数。 id参数让操作系统知道要停止的后台进程。您将在下一步中进行设置。

然后,您需要在aps有效负载中设置类别值,以匹配categories对象中的一个对象。您还应该在payload对象的根目录中设置notId属性。这是您传递给finish方法的参数,以告诉操作系统推送事件的处理已完成。

{
    "aps": {
        "alert": "This is a notification that will be displayed ASAP.",
        "category": "invite"
    },
    "notId": "1"
}

如果您的用户点击了通知主体,您的应用就会被打开。但是,如果他们单击任一操作按钮,应用程序将打开(或启动),并且将执行指定的JavaScript回调。

Note: Action buttons are only supported on iOS when you send directly to APNS. If you are using GCM to send to iOS devices you will lose this functionality.

我刚刚将文档粘贴到https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#action-buttons-1

相关问题