解析函数声明 Javascript

Parse function declaration Javascript

本文关键字:Javascript 声明 函数      更新时间:2023-09-26

我正在尝试定义一个发送推送通知的云代码方法,但是我很难声明和调用这些方法。我需要一种方法来获取 3 个字符串参数trackingslugchannel我知道如何用 Java 或其他一些 OOP 语言编写这些参数。

private void sendNotification(String tracking, String slug, String channel)

并打电话给sendNotification("someTracking", "someSlug", "someChannel");

我将如何在 Parse JS SDK 中编写这些方法?

您可以声明云代码函数,如 Bjorn 发布的指南 (https://www.parse.com/docs/cloud_code_guide#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 语法,您可以遵循云代码指南(前面引用)中概述的以下模式:

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