forked from grijjy/GrijjyFoundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrijjy.RemotePush.Receiver.pas
206 lines (181 loc) · 5.92 KB
/
Grijjy.RemotePush.Receiver.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
unit Grijjy.RemotePush.Receiver;
{$I Grijjy.inc}
interface
uses
SysUtils,
DateUtils,
Messaging,
System.PushNotification,
{$IFDEF ANDROID}
FMX.PushNotification.Android,
{$ENDIF}
{$IFDEF IOS}
FMX.PushNotification.iOS,
FMX.Helpers.iOS,
iOSAPI.UIKit,
{$ENDIF}
FMX.Platform;
type
{ Remote notification message }
TgoRemoteNotificationMessage = class(TMessage)
private
FDataKey: String;
FJson: String;
FActivated: Boolean;
public
constructor Create(const ADataKey, AJson: String; const AActivated: Boolean);
property DataKey: String read FDataKey;
property Json: String read FJson;
{ Whether message is activated by the user (tapping in it) }
property Activated: Boolean read FActivated;
end;
{ Device token change message }
TgoDeviceTokenChangeMessage = class(TMessage)
private
FDeviceToken: String;
public
constructor Create(const ADeviceToken: String);
property DeviceToken: String read FDeviceToken;
end;
type
{ Remote push receiver instance }
TgoRemotePushReceiver = class(TObject)
private
FPushService: TPushService;
FPushServiceConnection: TPushServiceConnection;
FDeviceToken: String;
FForeground: Boolean;
procedure CheckDeviceToken;
private
procedure OnChange(Sender: TObject; AChange: TPushService.TChanges);
procedure OnReceiveNotificationEvent(Sender: TObject; const ANotification: TPushServiceNotification);
procedure ApplicationEventHandler(const Sender: TObject;
const Msg: TMessage);
protected
function GetNumber: Integer;
procedure SetNumber(const AValue: Integer);
public
constructor Create(const AGCMAppId: UnicodeString);
destructor Destroy; override;
public
property DeviceToken: String read FDeviceToken;
property Number: Integer read GetNumber write SetNumber;
end;
implementation
{ TRemoteNotificationMessage }
constructor TgoRemoteNotificationMessage.Create(const ADataKey, AJson: String; const AActivated: Boolean);
begin
inherited Create;
FDataKey := ADataKey;
FJson := AJson;
FActivated := AActivated;
end;
{ TDeviceTokenChangeMessage }
constructor TgoDeviceTokenChangeMessage.Create(const ADeviceToken: String);
begin
inherited Create;
FDeviceToken := ADeviceToken;
end;
{ TgoRemotePushReceiver }
constructor TgoRemotePushReceiver.Create(const AGCMAppId: UnicodeString);
begin
FDeviceToken := '';
FForeground := True;
TMessageManager.DefaultManager.SubscribeToMessage(TApplicationEventMessage, ApplicationEventHandler);
{$IFDEF ANDROID}
FPushService := TPushServiceManager.Instance.GetServiceByName(TPushService.TServiceNames.GCM);
{ Android: When you call register, it’s often going to fail with an IOException
containing the message SERVICE_NOT_AVAILABLE. }
{$ENDIF}
{$IFDEF IOS}
FPushService := TPushServiceManager.Instance.GetServiceByName(TPushService.TServiceNames.APS);
{$ENDIF}
if FPushService <> nil then
begin
{$IFDEF ANDROID}
FPushService.AppProps[TPushService.TAppPropNames.GCMAppID] := AGCMAppId;
{$ENDIF}
FPushServiceConnection := TPushServiceConnection.Create(FPushService);
if FPushServiceConnection <> nil then
begin
FPushServiceConnection.OnChange := OnChange;
FPushServiceConnection.OnReceiveNotification := OnReceiveNotificationEvent;
FPushServiceConnection.Active := True;
end;
end;
end;
destructor TgoRemotePushReceiver.Destroy;
begin
TMessageManager.DefaultManager.Unsubscribe(TApplicationEventMessage, ApplicationEventHandler);
if FPushServiceConnection <> nil then
FPushServiceConnection.Free;
if FPushService<> nil then
FPushService.Free;
inherited;
end;
procedure TgoRemotePushReceiver.CheckDeviceToken;
var
DeviceTokenChangeMessage: TgoDeviceTokenChangeMessage;
DeviceToken: String;
begin
DeviceToken := FPushService.DeviceTokenValue[TPushService.TDeviceTokenNames.DeviceToken];
if DeviceToken <> '' then
begin
FDeviceToken := DeviceToken;
DeviceTokenChangeMessage := TgoDeviceTokenChangeMessage.Create(FDeviceToken);
TMessageManager.DefaultManager.SendMessage(Self, DeviceTokenChangeMessage);
end;
end;
procedure TgoRemotePushReceiver.OnChange(Sender: TObject; AChange: TPushService.TChanges);
begin
CheckDeviceToken;
end;
procedure TgoRemotePushReceiver.OnReceiveNotificationEvent(Sender: TObject; const ANotification: TPushServiceNotification);
var
RemoteNotificationMessage: TgoRemoteNotificationMessage;
Activated: Boolean;
{$IFDEF IOS}
UIApp: UIApplication;
{$ENDIF}
begin
{$IFDEF IOS}
UIApp := TUIApplication.Wrap(TUIApplication.OCClass.sharedApplication);
Activated := Boolean(UIApp.applicationState);
{$ELSE}
Activated := not FForeground;
{$ENDIF}
{ This event is received with the notification immediately if the app is already running
or delayed when the notification is clicked and the app is launched }
RemoteNotificationMessage := TgoRemoteNotificationMessage.Create(ANotification.DataKey, ANotification.Json.ToString, Activated);
TMessageManager.DefaultManager.SendMessage(Self, RemoteNotificationMessage);
end;
procedure TgoRemotePushReceiver.ApplicationEventHandler(const Sender: TObject;
const Msg: TMessage);
begin
Assert(Assigned(Msg));
Assert(Msg is TApplicationEventMessage);
{ For Android platforms we track the active state of the application
to determine whether the user pressed triggered the notification. A possible
better solution is to implement ParsePushBroadcastReceiver }
case TApplicationEventMessage(Msg).Value.Event of
TApplicationEvent.WillBecomeInactive:
FForeground := False;
TApplicationEvent.BecameActive:
FForeground := True;
end;
end;
function TgoRemotePushReceiver.GetNumber: Integer;
begin
{$IFDEF IOS}
Result := SharedApplication.ApplicationIconBadgeNumber;
{$ELSE}
Result := 0;
{$ENDIF}
end;
procedure TgoRemotePushReceiver.SetNumber(const AValue: Integer);
begin
{$IFDEF IOS}
SharedApplication.setApplicationIconBadgeNumber(AValue);
{$ENDIF}
end;
end.