如何使用 Delphi 通过 Parse.Com 发送推送通知?

How can I send a Push Notification through Parse.Com using Delphi?

我需要使用 Delphi 通过 Parse.com 的 API 发送推送通知。

我看到有一个 TParseApi,但是像往常一样,文档在这个主题上相当稀疏。

我该怎么做?

至少有三种方法可以做到这一点:

1) 一种直接的方法是使用自定义 headers 和 JSON

创建您自己的 HTTP 请求
Procedure TForm1.ParseDotComPushNotification(pushMessage: string);
var
  parseDotComUrl: string;
  JSON: TStringStream;
  webRequest: TIDHttp;
  response: string;
  whereJson: TJSONObject;
  alertJson: TJSONObject;
  mainJsonObject: TJSONObject;
begin
  parseDotComUrl := 'https://api.parse.com/1/push';

  // Modify the JSON as required to push to whomever you want to. 
  // This one is set up to push to EVERYONE.
  // JSON := TStringStream.Create('{ "where": {}, ' + '"data" : {"alert":"' 
  //           + pushMessage + '"}' + '}', TEncoding.UTF8);

  mainJsonObject := TJSONObject.Create;
  whereJson := TJSONObject.Create;
  mainJsonObject.AddPair(TJSONPair.Create('where', whereJson));
  alertJson := TJSONObject.Create;
  alertJson.AddPair(TJSONPair.Create('alert', pushMessage));
  mainJsonObject.AddPair(TJSONPair.Create('data', alertJson));
  JSON := TStringStream.Create(mainJsonObject.ToJSON);
  mainJsonObject.Free; // free all the child objects.

  webRequest := TIDHttp.Create(nil);
  webRequest.Request.Connection := 'Keep-Alive';
  webRequest.Request.CustomHeaders.Clear;
  webRequest.Request.CustomHeaders.AddValue('X-Parse-Application-Id', 
             'YourApplicationID');
  webRequest.Request.CustomHeaders.AddValue('X-Parse-REST-API-KEY',
             'YourRestApiKey');
  webRequest.Request.ContentType := 'application/json';
  webRequest.Request.CharSet := 'utf-8';
  webRequest.Request.ContentLength := JSON.Size;
  try
    try
      response := webRequest.Post(parseDotComUrl, JSON);
    except
      on E: Exception do
      begin
        showmessage(response);
      end;
    end;
  finally
    webRequest.Free;
    JSON.Free;
  end;
end;

从而绕过了 TParseApi

的需要

2) 根据UweRabbe的回答,你也可以在代码中这样做:

procedure TForm1.parseProviderCodeButtonClick(Sender: TObject);
var
  myParseProvider: TParseProvider;
  myBackendPush: TBackendPush;
  myStrings: Tstrings;
  whereJson: TJSONObject;
  alertJson: TJSONObject;
  mainJsonObject: TJSONObject;
begin
  mainJsonObject := TJSONObject.Create;
  whereJson := TJSONObject.Create;
  mainJsonObject.AddPair(TJSONPair.Create('where', whereJson));
  alertJson := TJSONObject.Create;
  alertJson.AddPair(TJSONPair.Create('alert', pushMessage));
  mainJsonObject.AddPair(TJSONPair.Create('data', alertJson));

  myParseProvider := TParseProvider.Create(nil);
  myParseProvider.ApiVersion := '1';
  myParseProvider.ApplicationID := 'YourApplicationID';
  myParseProvider.MasterKey := 'YourMasterKey';
  myParseProvider.RestApiKey := 'YourRestApiKey';

  myBackendPush := TBackendPush.Create(nil);
  myBackendPush.Provider := myParseProvider;
  // myBackendPush.Message := 'Hello world';

  myStrings := TStringList.Create;
  myStrings.Clear;

  // I like putting the message in when I generate the JSON for the Target
  // (since it seems I have to do it anyways, my not do it all in one place).
  // You could however us TBackendPush.Message as I've commented out above.
  // myStrings.Add('{ "where": {  }, "data" : {"alert":"goodbye world"}}');
  myStrings.Add(mainJsonObject.ToJSON);
  myBackendPush.Target := myStrings;
  myStrings.Free;
  mainJsonObject.Free; // free all the child objects.

  myBackendPush.Push;

  myBackendPush.Free;
  myParseProvider.Free;
end;

3) 并将其四舍五入为一个完整的答案(再次基于 UweRabbe 的答案)

在你的 form/datamodule 上:

  1. 放置一个TParseProvider
  2. 放置一个 TBackendPush - 这应该会自动将其 Provider 文件设置为您在上一步中创建的 TParseProvider 的名称。
  3. 设置 TBackendPushApplicationIDMasterKeyRestApiKeyMessage 属性
  4. 从代码中设置 TBackendPush 的 Push 方法。

例如,

procedure TForm1.Button1(Sender: TObject);
begin
  BackendPush1.Push;
end;

TParseProviderTBackendPush 组件拖放到表单或数据模块上。连接它们并在提供商的适当属性中输入您的凭据。将后端 Message 属性 设置为要发送的消息并调用 Push.