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

时间:2021-02-22 16:27:42

I need to send a Push notification out through Parse.com's API using Delphi.

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

I see there is a TParseApi but the documentation is, as usual, rather sparse on the subject.

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

How can I do this?

我怎样才能做到这一点?

2 个解决方案

#1


4  

Drop a TParseProvider and a TBackendPush component onto a form or datamodule. Connect them and enter your credentials in the appropriate properties of the provider. Set the backend Message property to the message to send and call Push.

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

#2


1  

There are at least three ways of doing this:

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

1) A direct method would be to create your own HTTP request with custom headers and JSON

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;

Thus bypassing the need for TParseApi

因此绕过了对TParseApi的需求

2) Based on UweRabbe's answer, you can also do it like this in code:

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) And to round this out into one complete answer (again based on UweRabbe's answer)

3)并将其整理成一个完整的答案(再次基于UweRabbe的答案)

On your form/datamodule:

在您的表单/数据模块上:

  1. Place a TParseProvider
  2. 放置一个TParseProvider

  3. Place a TBackendPush - this should automatically set its Provider filed to the name of the TParseProvider you created in the previous step.
  4. 放置TBackendPush - 这应该自动将其Provider字段设置为您在上一步中创建的TParseProvider的名称。

  5. Set the TBackendPush's ApplicationID, MasterKey, RestApiKey, and Message properties
  6. 设置TBackendPush的ApplicationID,MasterKey,RestApiKey和Message属性

  7. Set the TBackendPush's Push method from code.
  8. 从代码中设置TBackendPush的Push方法。

e.g.,

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

#1


4  

Drop a TParseProvider and a TBackendPush component onto a form or datamodule. Connect them and enter your credentials in the appropriate properties of the provider. Set the backend Message property to the message to send and call Push.

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

#2


1  

There are at least three ways of doing this:

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

1) A direct method would be to create your own HTTP request with custom headers and JSON

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;

Thus bypassing the need for TParseApi

因此绕过了对TParseApi的需求

2) Based on UweRabbe's answer, you can also do it like this in code:

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) And to round this out into one complete answer (again based on UweRabbe's answer)

3)并将其整理成一个完整的答案(再次基于UweRabbe的答案)

On your form/datamodule:

在您的表单/数据模块上:

  1. Place a TParseProvider
  2. 放置一个TParseProvider

  3. Place a TBackendPush - this should automatically set its Provider filed to the name of the TParseProvider you created in the previous step.
  4. 放置TBackendPush - 这应该自动将其Provider字段设置为您在上一步中创建的TParseProvider的名称。

  5. Set the TBackendPush's ApplicationID, MasterKey, RestApiKey, and Message properties
  6. 设置TBackendPush的ApplicationID,MasterKey,RestApiKey和Message属性

  7. Set the TBackendPush's Push method from code.
  8. 从代码中设置TBackendPush的Push方法。

e.g.,

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