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

时间:2016-01-22 17:57:32

标签: delphi parse-platform delphi-10-seattle

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

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

我该怎么做?

2 个答案:

答案 0 :(得分:4)

TParseProviderTBackendPush组件放到表单或数据模块上。连接它们并在提供程序的相应属性中输入您的凭据。将后端Message属性设置为要发送的邮件并调用Push

答案 1 :(得分:1)

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

1)直接方法是使用自定义标头和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的回答)

在您的表单/ datamodule上:

  1. 放置TParseProvider
  2. 放置TBackendPush - 这会自动将其Provider字段设置为您在上一步中创建的TParseProvider的名称。
  3. 设置TBackendPush' ApplicationIDMasterKeyRestApiKeyMessage属性
  4. 从代码中设置TBackendPush的Push方法。
  5. 如,

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