您的通知可以包含操作按钮。对于 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) {
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上粘贴了文档。