我正在尝试定义发送推送通知的云代码方法,但是我很难声明并调用方法。我需要一个方法来获取3个字符串参数tracking
,slug
和channel
我知道如何用Java或其他OOP语言编写这些参数。
private void sendNotification(String tracking, String slug, String channel)
并致电sendNotification("someTracking", "someSlug", "someChannel");
我如何在Parse JS SDK中编写这些方法?
答案 0 :(得分:0)
您可以声明Bjorn发布的指南(https://www.parse.com/docs/cloud_code_guide#cloud_code)中概述的Cloud Code功能。在这种情况下,为了帮助您,下面是一个方法定义的示例,其行为将如您所愿。
/**
* Send Push Notification
* Required:
* tracking -- Tracking String
* slug -- Slug String
* channel -- Channel String
*/
Parse.Cloud.define("sendNotification", function(request, response) {
// Obviously you don't need to assign these variables, but this is how you access the parameters passed in
var tracking = request.params.tracking;
var slug = request.params.slug;
var channel = request.params.channel;
// Send your push notification here, I'm not gonna write the whole thing for you! :)
// In the callback of your push notification call:
response.success();
// For a successful send and response.error() if it fails or an error occurs
});
要在客户端中调用此方法,假设您使用的是java,因为您在问题语句中使用了java语法,则可以按照“云代码指南”(前面引用)中的说明使用此模式:
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("tracking", "TRACKING_STRING");
params.put("slug", "SLUG_STRING");
params.put("channel", "CHANNEL_STRING");
ParseCloud.callFunctionInBackground("sendNotification", params, new FunctionCallback<Float>() {
void done(ParseException e) {
if (e == null) {
// Your function was successful
} else {
// Your function failed, handle the error here
}
}
});
但是,如果您只是尝试从客户端发送推送通知,则应该只使用此处概述的Parse内置函数:https://parse.com/docs/push_guide#sending/Android