11

I need to build a provider server in Delphi to send push notification messages to my iPhone app via APNS.

I have read that this is possible to do through Indy components. It is also required to install an SSL certificate (.p12) provided by apple.

I'm looking for some pointers to get started with this in Delphi. What would be a good library to use, and does anyone know of any example code to do something like this?

Here are samples for Ruby & PHP, C# and JAVA

Community
  • 1
  • 1
navid jamshidi
  • 173
  • 1
  • 2
  • 9
  • Why don't you port the java/php or ruby code? Its been a while i used delphi? Another question is delphi a client app? In that case why not use a central server running php and just call the URL with some query parameters, then let php push the message to the APN. You also need to keep track of unsubscribe feed (something you want to run in a cron or so once a day). – Roger Jun 19 '12 at 07:09
  • Have you thought about using the excellent push notification provider Urban Airship. If you needs are fairly limited in number of pushes I would suggest that you take a look at that http://urbanairship.com/ – EsbenB Jul 08 '12 at 15:48

2 Answers2

5

OK I managed this as follows:
Add an indy TidTCPClient and TIdSSLIOHandlerSocket on your form and link them. Set the SSL options in the TIdSSLIOHandlerSocket, set CertFile and KeyFile to the appropriate .pem files. Set method to sslvSSLv23 and mode to sslmClient.
In the IOHandler's OnGetPassword event set your key's password.

Useful URLs: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12

On the coding front:

N.b. HexData is the ID sent from the IPhone App

function SendNotification(HexData: String; Count: Integer): Boolean;
  var
    p_DataSize,
    I: Integer;
    p_payllen: Byte;
    p_json   : String;
    p_sndmsg : String;
  begin
// Delphi 6 so needed to create JSON by hand<br>
    p_json := '{"aps":{';
    if (Count > 0) then
    begin
      p_json := p_json + '"alert":"You Have ' + IntToStr(Count);
      if (count = 1) then
        p_json := p_json + ' Reminder'
      else<br>
        p_json := p_json + ' Reminders';
      p_json := p_json + '","sound": "default",';
    end;
    p_json := p_json + '"badge":' + inttostr(Count) + '}}';
    p_payllen := length(p_json);
    // Hard code the first part of message as it never changes
    p_sndmsg :=  chr(0) + chr(0) + chr(32);
    // Convert hex string to binary data 
    p_DataSize := Length(HexData) div 2;
    for I := 0 to p_DataSize-1 do
      p_sndmsg := p_sndmsg + char(Byte(StrToInt('$' + Copy(HexData, (I*2)+1,
        2))));
    //Now need to add length of json string and string itself
    p_sndmsg := p_sndmsg + chr(0) + Char(p_payllen) + p_json;
    try
    // According to Apple can't connect/disconnect for each message so leave open for later
      if (not PushClient.Connected) then
        PushClient.Connect;
      PushClient.IOHandler.Send(p_sndmsg[1], length(p_sndmsg));
    except
      on e : exception do
        Log_Error(e.message);
    end;
  end;
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Paul
  • 66
  • 3
  • I'm not able to make this work, APNS server doesn't return anything even if I use the extended message syntax ( 1 + identifier + timeout + 0 + 32 ) at the beginning. How to debug it ? Indy10 doesn't have Send method anymore. – Rafael Nobre Mar 21 '13 at 13:10
  • Also, sometimes the algorithm that makes the hexstring binary will fail for a few bytes, it won't reconstruct properly – Rafael Nobre Mar 21 '13 at 13:23
  • 1
    I got it to work, the problem was that you are building a binary buffer in a string, and some bytes won't work, they'll map to 3F (textual "?"), it must be built in a TBytes or TMemoryStream with raw bytes. – Rafael Nobre Mar 21 '13 at 14:43
2

you can try porting java/php or ruby code as Rogier said. mean while have a look at his http://www.pushwoosh.com/ some thing similar to http://urbanairship.com/ .

rashii
  • 500
  • 7
  • 17