Articles Sending iOS (and Android) remote push notifications from your Delphi service with the HTTP/2 protocol by Allen Drennan

emailx45

Местный
Регистрация
5 Май 2008
Сообщения
3,571
Реакции
2,438
Credits
573
Sending iOS (and Android) remote push notifications from your Delphi service with the HTTP/2 protocol
January 18, 2017 Allen Drennan
[SHOWTOGROUPS=4,20]
In this article we are going to discuss how to make a base unifying class for sending remote push notifications from your service to both the iOS (Apple Push Notification Service) and Android (Firebase Cloud Messaging) as well as covering how to use the newer Apple HTTP/2 protocol interface for sending push notifications from Delphi.

Introduction
If you are authoring a Delphi service for the cloud, you may have the need to send push notifications from your service to your client application. There are various ways to accomplish this including using third-party BAAS providers or rolling your own interface with some of the established libraries.

In the past Apple used a custom TCP binary protocol to send messages to the APNS. This was cumbersome and inflexible and had numerous pitfalls in determining whether your message was actually accepted by the interface. Apple recently released a newer interface that uses HTTP/2. The nice thing about this interface is, just like FCM it utilizes JSON for the payload of the protocol. However, the protocol uses the newer HTTP/2 protocol and ALPN (Application-Layer Protocol Negotiation Extension, defined in RFC 7301) which is required in your SSL handshake.

One other advantage, Apple also increased the maximium size of the payload from 2K to 4K with the new HTTP/2 interface as of iOS 9 devices.

We will show how to combine the Для просмотра ссылки Войди или Зарегистрируйся with the excellent Для просмотра ссылки Войди или Зарегистрируйся to solve both of these issues and transparently send push notifications to either Apple’s APNS or Google’s FCM from your Delphi service.

For more information about us, our support and services visit the Для просмотра ссылки Войди или Зарегистрируйся or the Для просмотра ссылки Войди или Зарегистрируйся.

The example contained here depends upon part of our Для просмотра ссылки Войди или Зарегистрируйся.

The source code and related example repository is hosted on GitHub at Для просмотра ссылки Войди или Зарегистрируйся.
nghttp2
Для просмотра ссылки Войди или Зарегистрируйся for implementing the HTTP/2 protocol. It handles all the compression and bitstreaming for implementing HTTP/2.

The Для просмотра ссылки Войди или Зарегистрируйся and its header compression are rather complex and evolving so it is probably best not to try and implement this ourselves.

One of the great things about nghttp2 is you can utilize it entirely with memory buffers so it is a good match for our own Для просмотра ссылки Войди или Зарегистрируйся for Windows and Linux. This way we get the benefit of HTTP/2 but we don’t have to rely on another implementation of sockets for scaling up our service.

Building nghttp2
If you want to build the nghttp2 library for Windows to use in your Delphi application you will need to download the Для просмотра ссылки Войди или Зарегистрируйся. To build you can use Visual Studio or just Для просмотра ссылки Войди или Зарегистрируйся. You will also need to download and install Для просмотра ссылки Войди или Зарегистрируйся.
  1. Download the latest nghttp2 source from Для просмотра ссылки Войди или Зарегистрируйся
  2. Install CMAKE and the Build Tools for Visual Studio.
  3. Run CMAKE followed by a period
    cmake .
  4. Run CMAKE to build the release version
    cmake --build . --config RELEASE
Delphi header translation
Once completed you will have a nghttp2.dll. We will need our Для просмотра ссылки Войди или Зарегистрируйся so we can use the nghttp2 methods directly.

Apple APNS & iOS Prerequisites
To use the TgoRemotePushSender class with Apple’s Push Notification Service, we need to create a certificate and convert that certificate into 2 PEM files, one for the certificate and one the private key.

There are quite a few primers on the Internet about how to create APNS certificates, so we will be brief here as it applies to our TgoRemotePushSender class.
  1. On iOS you must create a production certificate that is universal by selecting the option “Apple Push Notification service SSL (Sandbox & Production)” under Certificates in the developer portal. This certificate must be matched with the proper AppId.

  2. You need to download and install the certificate into the Mac OSX Keychain.
  3. You need to export the Apple Push Notification service certificate and private key as 2 separate files (cert.p12 and key.p12) from the Keychain application on OSX. These can only be exported as .p12 files from the Keychain application.
To create PEM files we recommend using the OpenSSL binaries to convert as follows:
1openssl pkcs12 -clcerts -nokeys -out APNS-cert.pem -in cert.p12
1openssl pkcs12 -nocerts -out APNS-key-password.pem -in key.p12

To remove the password from the APNS-key-password.pem file:
1openssl rsa -in APNS-key-password.pem -out APNS-key.pem

To test the certificate against the APNS interface to see if it works:
1openssl s_client -connect gateway.push.apple.com:2195 -cert APNS-cert.pem -key APNS-key.pem

TgoRemotePushSender
To use the TgoRemotePushSender class we need to know the Device Token for the target user’s device. You pass this value to the TgoRemotePushSender.Send method when sending your remote push notification. This is true of both iOS and Android targets.

For Android you also need to specify the TgoRemotePushSender.AndroidAPIKey. You can obtain an API Key by setting up cloud messaging on Для просмотра ссылки Войди или Зарегистрируйся. This is also called the Server API Key under Firebase Cloud Messaging.

For iOS you need to specify the TgoRemotePushSender.APNSKey which is essentially your Bundle Identifier (ex: com.mycool.app). You also need to apply the APNS certificate and private key .PEM files you previously created.

1
2
3
TgoRemotePushSender.APNSKey := 'com.mycool.app';
TgoRemotePushSender.APNSCertificate := TFile.ReadAllBytes(PathToCertificate.PEM);
TgoRemotePushSender.APNSKey := TFile.ReadAllBytes(PathToKey.PEM);

The structure of the class is fairly straightforward. To send a remote push notification, call the Send method and provide the DeviceToken and also the Title and Message body for the payload.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
TgoRemotePushSender = class(TObject)
public
constructor Create;
destructor Destroy; override;
public
{ Send push notification }
function Send(const APlatform: TOSVersion.TPlatform; const ADeviceToken: String;
const ATitle, AMessage: String): Boolean;
public
{ Android API Key for your app }
property AndroidAPIKey: String read FAndroidAPIKey write FAndroidAPIKey;

{ iOS Certificate }
property APNSCertificate: TBytes read FAPNSCertificate write FAPNSCertificate;

{ iOS Key }
property APNSKey: TBytes read FAPNSKey write FAPNSKey;

{ iOS Topic }
property APNSTopic: String read FAPNSTopic write FAPNSTopic;
end;
Note: You should create and reuse an instance of this class to avoid creating multiple connections to the push notification host. One model would be to perform notifications in batches based upon time. Apple warns about creating too many connections as a reason for blocking your service.
Для просмотра ссылки Войди или Зарегистрируйся demonstrating how to use this class is available within our repository on GitHub.

Sending custom user data
Now that both Apple and Google use JSON as their payload format, you can easily add your own custom JSON fields to the payload that are consistent with both platforms. While we don’t expose this directly in our class you can add additional content by simply expanding the JSON with your extra custom data.

License
TgoRemotePushSender and DelphiRemotePushSender are licensed under the Simplified BSD License. See License.txt for details.

[/SHOWTOGROUPS]