2016-01-22 143 views

回答

1

至少有三种方式这样做的:

1)直接的方法是使用自定义标题创建自己的HTTP请求和JSON

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

需要基于UweRabbe的回答

2),你也可以做这样的代码:

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的答案)

在窗体/数据模块:

  1. 将一个TParseProvider
  2. 将一个TBackendPush - 这应该自动设置其Provider已提交给您在上一步中创建的TParseProvider的名称。
  3. 设置TBackendPushApplicationIDMasterKeyRestApiKey,和Message性质
  4. 设置TBackendPush“从码S Push方法。

例如,

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

你不应该用手建立JSON,XML,...数据结构 –

+0

@SirRufo没错,但这是今天下午的课程。当我解决问题时我会更新。 – BIBD

4

掉落一个TParseProviderTBackendPush组件到表格或数据模块。连接它们并在提供商的适当属性中输入您的凭证。将后端Message属性设置为要发送的消息并呼叫Push