您的通知可以包含操作按钮。对于 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
}
}
}
}
});
您会注意到,我们在我们的初始化代码的 iOS 对象中添加了一个新参数,称为类别。每个类别都是一个命名对象,在这种情况下是邀请和删除。如果您希望显示操作按钮,这些名称将需要与您通过有效负载发送到 APNS 的名称相匹配。每个类别最多可以有三个按钮,必须标记为是、否和可能。反过来,这些按钮中的每一个都有四个属性,回调您要调用的 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 参数让操作系统知道要停止哪个后台进程。您将在下一步中设置它。
然后您需要在您的应用程序负载中设置类别值以匹配类别对象中的对象之一。您还应该在有效负载对象的根目录中设置一个 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