Cordova 推送通知操作

4
Cordova肯定有发送推送通知的选项,但是是否有插件包括直接在通知中添加操作的功能呢?
我尝试搜索“交互式通知”和“通知操作”插件,但没有结果。我还尝试查看phonegap-plugin-push的API参考文档,但没有找到合适的选项或方法。
我要寻找的一个例子是当iMessage允许用户在不打开应用程序的情况下回复短信时发生的事情:

iOS push notification action

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

1个回答

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
                }
            }
        }
    }
});

你会注意到,在我们的init代码的iOS对象中添加了一个名为“categories”的新参数。每个类别都是invite和delete这样的命名对象。如果您想显示操作按钮,这些名称需要与您通过负载发送的名称匹配。每个类别最多可以有三个按钮,必须标记为yes、no和maybe。每个按钮又有四个属性:回调JavaScript函数、按钮标签、前景色(是否将应用程序带到前景)和destrutive(并不真正执行破坏性操作,只是将按钮标红作为对用户的警告,表明该操作可能具有破坏性)。
就像后台通知一样,当您成功处理按钮回调时,调用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方法现在需要传入success、failure和id参数。id参数让操作系统知道要停止哪个后台进程。你将在下一步中设置它。
然后,你需要将aps负载中的category值设置为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上粘贴了文档。

1
太棒了,这正是我想要的。感谢您提供如此详细的解释。 - Morgan Delaney
我刚刚复制了文档。我将在插件存储库上开一个问题,指出这应该在API.md上提到,而不仅仅是在PAYLOAD.md上。 - jcesarmobile

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接