Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build an Apple Push Notification provider server in Delphi

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

like image 847
navid jamshidi Avatar asked Jun 15 '12 22:06

navid jamshidi


1 Answers

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;
like image 103
Paul Avatar answered Oct 07 '22 03:10

Paul