Apple Push Notifications - 发送 HTTP/2 通知(第 2/4 部分)

· 功能

在上一篇博文中,我介绍了如何获取设备 token,本文将介绍如何使用 sgcWebSockets HTTP/2 客户端发送通知(不含身份验证部分)。

将远程通知负载和设备 token 信息发送至 Apple 推送通知服务(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;