前回の記事ではデバイストークンの取得方法を紹介しました。今回は HTTP/2 sgcWebSockets クライアントを使用して通知を送信する方法を紹介します(認証部分は除く)。
リモート通知のペイロードとデバイストークン情報を Apple Push Notification サービス(APNs)に送信します。
APNs への接続方法
サーバープロバイダーと以下のサーバーのいずれかとの接続を成功させるには、HTTP/2 プロトコルと TLS 1.2 以上を使用する必要があります。
開発サーバー: https://api.sandbox.push.apple
本番サーバー: https://api.push.apple
Delphi コード
TsgcHTTP2Client の新しいインスタンスを作成し、POST メソッドを呼び出して APNs に通知を送信します。
oHTTP := TsgcHTTP2Client.Create(nil);
Try
// ... requires authorization code
oStream := TStringStream.Create('{"aps":{"alert":"Alert from sgcWebSockets!"}}');
Try
oHTTP.Post('https://api.push.apple/3/device/device_token', oStream);
if oHTTP.Response.Status = 200 then
ShowMessage('Notification Sent Successfully')
else
ShowMessage('Notification error');
Finally
oStream.Free;
End;
Finally
oHTTP.Free;
End;
