2016-01-22 131 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