-
Notifications
You must be signed in to change notification settings - Fork 8
/
DW.PushClient.pas
228 lines (199 loc) · 7.03 KB
/
DW.PushClient.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
unit DW.PushClient;
(*
DelphiWorlds PushClient project
------------------------------------------
A cross-platform method of using Firebase Cloud Messaging (FCM) to receive push notifications
This project was inspired by the following article:
http://thundaxsoftware.blogspot.co.id/2017/01/firebase-cloud-messaging-with-delphi.html
*)
{$I DW.GlobalDefines.inc}
interface
uses
// To Share Variables between C++ And Delphi
SharedVariables,
// RTL
System.PushNotification,
// DW
DW.RegisterFCM;
type
TPushSystem = (APS, GCM);
TRegistrationErrorEvent = procedure(Sender: TObject; const Error: string) of object;
TPushClient = class(TObject)
private
FBundleID: string;
FDeviceID: string;
FDeviceToken: string;
FPushService: TPushService;
FPushSystem: TPushSystem;
FRegisterFCM: TRegisterFCM;
FServerKey: string;
FServiceConnection: TPushServiceConnection;
FUseSandbox: Boolean;
FOnChange: TPushServiceConnection.TChangeEvent;
FOnReceiveNotification: TPushServiceConnection.TReceiveNotificationEvent;
FOnRegistrationError: TRegistrationErrorEvent;
procedure ActivateAsync;
procedure ClearDeviceInfo;
procedure CreatePushService;
procedure DoChange(AChange: TPushService.TChanges);
procedure DoRegistrationError(const AError: string);
function GetActive: Boolean;
function GetGCMAppID: string;
procedure ServiceConnectionChangeHandler(Sender: TObject; AChange: TPushService.TChanges);
procedure ServiceConnectionReceiveNotificationHandler(Sender: TObject; const ANotification: TPushServiceNotification);
procedure SetActive(const Value: Boolean);
procedure SetGCMAppID(const Value: string);
procedure RegisterFCMRequestCompleteHandler(Sender: TObject; const Success: Boolean; const RequestResult: string);
public
constructor Create;
destructor Destroy; override;
property Active: Boolean read GetActive write SetActive;
property BundleID: string read FBundleID write FBundleID;
property DeviceID: string read FDeviceID;
property DeviceToken: string read FDeviceToken;
property GCMAppID: string read GetGCMAppID write SetGCMAppID;
property PushSystem: TPushSystem read FPushSystem;
property UseSandbox: Boolean read FUseSandbox write FUseSandbox;
property ServerKey: string read FServerKey write FServerKey;
property OnChange: TPushServiceConnection.TChangeEvent read FOnChange write FOnChange;
property OnReceiveNotification: TPushServiceConnection.TReceiveNotificationEvent read FOnReceiveNotification write FOnReceiveNotification;
property OnRegistrationError: TRegistrationErrorEvent read FOnRegistrationError write FOnRegistrationError;
end;
implementation
uses
// RTL
System.SysUtils, System.Threading, System.Classes,
// FMX
{$IF Defined(IOS)}
FMX.PushNotification.iOS, DW.RemoteNotificationsPatch.iOS, DW.iOSapi.UserNotifications;
{$ENDIF}
{$IF Defined(Android)}
//{$IF Defined(__ANDROID__)}
FMX.PushNotification.Android;
{$ENDIF}
{ TPushClient }
constructor TPushClient.Create;
begin
inherited;
CreatePushService;
end;
destructor TPushClient.Destroy;
begin
FServiceConnection.Free;
FPushService.Free;
FRegisterFCM.Free;
inherited;
end;
procedure TPushClient.CreatePushService;
begin
case TOSVersion.Platform of
TOSVersion.TPlatform.pfiOS:
begin
FPushSystem := TPushSystem.APS;
FPushService := TPushServiceManager.Instance.GetServiceByName(TPushService.TServiceNames.APS);
// FCM for iOS requires that the APNs token be "converted" to an FCM token. This is what TRegisterFCM does
FRegisterFCM := TRegisterFCM.Create;
FRegisterFCM.OnRequestComplete := RegisterFCMRequestCompleteHandler;
end;
TOSVersion.TPlatform.pfAndroid:
begin
FPushSystem := TPushSystem.GCM;
FPushService := TPushServiceManager.Instance.GetServiceByName(TPushService.TServiceNames.GCM);
end;
else
raise Exception.Create('Unsupported platform');
end;
FServiceConnection := TPushServiceConnection.Create(FPushService);
FServiceConnection.OnChange := ServiceConnectionChangeHandler;
FServiceConnection.OnReceiveNotification := ServiceConnectionReceiveNotificationHandler;
end;
procedure TPushClient.DoChange(AChange: TPushService.TChanges);
begin
if FServiceConnection.Active then
FDeviceID := FPushService.DeviceIDValue[TPushService.TDeviceIDNames.DeviceID];
if Assigned(FOnChange) then
TThread.Synchronize(nil,
procedure
begin
FOnChange(Self, AChange);
end
);
end;
procedure TPushClient.DoRegistrationError(const AError: string);
begin
if Assigned(FOnRegistrationError) then
FOnRegistrationError(Self, AError);
end;
function TPushClient.GetActive: Boolean;
begin
Result := FServiceConnection.Active;
end;
function TPushClient.GetGCMAppID: string;
begin
Result := FPushService.AppProps[TPushService.TAppPropNames.GCMAppID];
end;
procedure TPushClient.RegisterFCMRequestCompleteHandler(Sender: TObject; const Success: Boolean; const RequestResult: string);
begin
// FCM token registration has completed
if Success then
begin
FDeviceToken := RequestResult;
DoChange([TPushService.TChange.DeviceToken]);
end
else
DoRegistrationError(RequestResult);
end;
procedure TPushClient.ServiceConnectionChangeHandler(Sender: TObject; AChange: TPushService.TChanges);
var
LTokenChange: Boolean;
LDeviceToken: string;
begin
LTokenChange := TPushService.TChange.DeviceToken in AChange;
if LTokenChange then
begin
LDeviceToken := FPushService.DeviceTokenValue[TPushService.TDeviceTokenNames.DeviceToken];
// If the token needs registration with FCM, FRegisterFCM will be non-nil
if FRegisterFCM <> nil then
FRegisterFCM.RegisterAPNToken(FBundleID, FServerKey, LDeviceToken, FUseSandbox)
else
FDeviceToken := LDeviceToken;
// Copy Apple Token in APNSToken to be shared in c++
APNSToken := LDeviceToken;
end;
// If it's not a token change, or registration is not required, call DoChange immediately
if not LTokenChange or (FRegisterFCM = nil) then
DoChange(AChange);
end;
procedure TPushClient.ServiceConnectionReceiveNotificationHandler(Sender: TObject; const ANotification: TPushServiceNotification);
begin
if Assigned(FOnReceiveNotification) then
FOnReceiveNotification(Self, ANotification);
end;
procedure TPushClient.SetActive(const Value: Boolean);
begin
if Value = FServiceConnection.Active then
Exit; // <=======
if Value then
ActivateAsync
else
ClearDeviceInfo;
end;
procedure TPushClient.ActivateAsync;
begin
TTask.Run(
procedure
begin
FServiceConnection.Active := True;
end
);
end;
procedure TPushClient.SetGCMAppID(const Value: string);
begin
FPushService.AppProps[TPushService.TAppPropNames.GCMAppID] := Value;
end;
procedure TPushClient.ClearDeviceInfo;
begin
FDeviceID := '';
FDeviceToken := '';
end;
end.