-
Notifications
You must be signed in to change notification settings - Fork 89
/
Grijjy.RemotePush.Sender.pas
193 lines (164 loc) · 5.33 KB
/
Grijjy.RemotePush.Sender.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
unit Grijjy.RemotePush.Sender;
{ Remote push notifications for iOS and Android }
{$I Grijjy.inc}
{ 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. }
interface
uses
Classes,
SysUtils,
Grijjy.Http,
Grijjy.SocketPool.Win,
Grijjy.Bson;
type
{ Remote push sender instance }
TgoRemotePushSender = class(TObject)
protected
FHttp: TgoHTTPClient;
FHttp2: TgoHTTPClient;
private
{ Android }
FAndroidAPIKey: String;
private
{ iOS }
FAPNSCertificate: TBytes;
FAPNSKey: TBytes;
FAPNSTopic: String;
private
{ JSON payload format for Google }
function GoogleCloud_Json_Payload(const ADeviceToken, ATitle, AMessage: String): String;
{ Google cloud push notification }
function GoogleCloud_Send(const AJSON: String;
out AResponse: String; out AStatusCode: Integer): Boolean;
private
{ JSON payload format for Apple/iOS }
function APNs_Json_Payload(const ATitle, AMessage: String;
const ABadge: Integer; const ASound: String): String;
{ Apple/iOS push notification }
function APNs_Send(const AJSON: String; const ADeviceToken: String;
out AResponse: String; out AStatusCode: Integer): Boolean;
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;
implementation
uses
System.SyncObjs,
DateUtils,
System.IOUtils;
function TgoRemotePushSender.GoogleCloud_Json_Payload(const ADeviceToken: String;
const ATitle, AMessage: String): String;
var
Doc, DocData: TgoBsonDocument;
Ids: TgoBsonArray;
begin
DocData := TgoBsonDocument.Create;
DocData['title'] := ATitle.Substring(0, 500);
DocData['message'] := AMessage.Substring(0, 500); { limit to 500 chars }
{ append custom data to json here }
Ids:= TgoBsonArray.Create;
Ids.Add(ADeviceToken);
Doc := TgoBsonDocument.Create;
Doc['to'] := ADeviceToken;
Doc['data'] := DocData;
Result := Doc.ToJson; { cannot exceed 4096 bytes }
end;
function TgoRemotePushSender.GoogleCloud_Send(const AJSON: String; out AResponse: String; out AStatusCode: Integer): Boolean;
begin
if FHttp = nil then
begin
FHttp := TgoHTTPClient.Create;
FHttp.Authorization := 'key=' + FAndroidAPIKey;
FHttp.ContentType := 'application/json';
end;
FHttp.RequestBody := AJSON;
AResponse := FHttp.Post('https://gcm-http.googleapis.com/gcm/send');
AStatusCode := FHttp.ResponseStatusCode;
Result := AStatusCode = 200;
end;
function TgoRemotePushSender.APNs_Json_Payload(const ATitle, AMessage: String;
const ABadge: Integer; const ASound: String): String;
var
Doc, DocAlert, DocPayload: TgoBsonDocument;
begin
DocAlert := TgoBsonDocument.Create;
DocAlert['title'] := ATitle.Substring(0, 500);
DocAlert['body'] := AMessage.Substring(0, 500); { limit to 500 chars }
DocPayload := TgoBsonDocument.Create;
DocPayload['alert'] := DocAlert;
DocPayload['badge'] := ABadge;
DocPayload['sound'] := ASound;
Doc := TgoBsonDocument.Create;
Doc['aps'] := DocPayload;
{ append custom data to json here }
Result := Doc.ToJson; { cannot exceed 4096 bytes for HTTP/2 iOS 9 or later }
end;
function TgoRemotePushSender.APNs_Send(const AJSON: String; const ADeviceToken: String;
out AResponse: String; out AStatusCode: Integer): Boolean;
begin
if FHttp2 = nil then
begin
FHttp2 := TgoHTTPClient.Create(True);
FHttp2.Certificate := FAPNSCertificate;
FHttp2.PrivateKey := FAPNSKey;
FHttp2.RequestHeaders.AddOrSet('apns-topic', FAPNSTopic);
// FHttp2.RequestHeaders.AddOrSet('apns-id', '<guid>');
// FHttp2.RequestHeaders.AddOrSet('apns-expiration', '0');
// FHttp2.RequestHeaders.AddOrSet('apns-priority', '10');
end;
FHttp2.RequestBody := AJSON;
AResponse := FHttp2.Post('https://api.push.apple.com/3/device/' + ADeviceToken);
AStatusCode := FHttp2.ResponseStatusCode;
Result := AStatusCode = 200;
end;
{ TgoRemotePushSender }
constructor TgoRemotePushSender.Create;
begin
FHttp := nil;
FHttp2 := nil;
end;
destructor TgoRemotePushSender.Destroy;
begin
if FHttp <> nil then
FHttp.Free;
if FHttp2 <> nil then
FHttp2.Free;
inherited;
end;
function TgoRemotePushSender.Send(const APlatform: TOSVersion.TPlatform;
const ADeviceToken, ATitle, AMessage: String): Boolean;
var
JSON: String;
Response: String;
StatusCode: Integer;
begin
case APlatform of
TOSVersion.TPlatform.pfiOS:
begin
JSON := APNs_Json_Payload(ATitle, AMessage, 1, 'default');
Result := APNs_Send(JSON, ADeviceToken, Response, StatusCode);
end;
TOSVersion.TPlatform.pfAndroid:
begin
JSON := GoogleCloud_Json_Payload(ADeviceToken, ATitle, AMessage);
Result := GoogleCloud_Send(JSON, Response, StatusCode);
end;
else
Result := False;
end;
end;
end.