Reputation: 14499
I'm trying to include the Apple Push Notifications
inside my iOS
application following this example.
After some debugging I was able to find out that APushService
is empty when running on iOS
, but not on Android
.
procedure TFormLogin.Button1Click(Sender: TObject);
var
APushService : TPushService;
begin
APushService := TPushServiceManager.Instance.GetServiceByName(TPushService.TServiceNames.APS);
end;
Am I doing something wrong? How can we enable Push Notifications for iOS on Delphi?
Any ideas would be highly appreciated.
Upvotes: 1
Views: 1852
Reputation: 1218
Here is working code that I use for Android and iOS:
const
FAndroidServerKey = '63538920422';
private
{ Private declarations }
FDeviceID: string;
FDeviceToken: string;
FPushService: TPushService;
FPushServiceConnection: TPushServiceConnection;
procedure OnReceiveNotificationEvent(Sender: TObject;
const ANotification: TPushServiceNotification);
procedure OnServiceConnectionChange(Sender: TObject;
AChange: TPushService.TChanges);
procedure PushServiceRegister;
procedure TFormMain.OnReceiveNotificationEvent(Sender: TObject;
const ANotification: TPushServiceNotification);
const
FCMSignature = 'gcm.notification.body';
GCMSignature = 'message';
APNsSignature = 'alert';
var
aText: string;
aObj: TJSONValue;
begin
// this will fire when only when app is opened
{$IFDEF ANDROID}
aObj := ANotification.DataObject.GetValue(GCMSignature);
if aObj <> nil then
aText := aObj.Value
else
aText := ANotification.DataObject.GetValue(FCMSignature).Value;
{$ELSE}
aObj := ANotification.DataObject.GetValue(APNsSignature);
if aObj <> nil then
aText := aObj.Value;
{$ENDIF}
ShowMessage(aText);
end;
procedure TFormMain.OnServiceConnectionChange(Sender: TObject;
AChange: TPushService.TChanges);
begin
if (TPushService.TChange.DeviceToken in AChange) and
Assigned(FPushServiceConnection) then
begin
FDeviceID := FPushService.DeviceIDValue[TPushService.TDeviceIDNames.DeviceID];
FDeviceToken := FPushService.DeviceTokenValue
[TPushService.TDeviceTokenNames.DeviceToken];
// save token and ID to remote db here
end;
end;
procedure TFormMain.PushServiceRegister;
begin
FPushService := nil;
FPushServiceConnection := nil;
{$IF defined(ANDROID)}
FPushService := TPushServiceManager.Instance.GetServiceByName<
(TPushService.TServiceNames.GCM);
FPushService.AppProps[TPushService.TAppPropNames.GCMAppID] := FAndroidServerKey;
{$ENDIF}
{$IF defined(IOS) AND defined(CPUARM)}
FPushService := TPushServiceManager.Instance.GetServiceByName
(TPushService.TServiceNames.APS);
{$ENDIF}
if Assigned(FPushService) then
begin
FPushServiceConnection := TPushServiceConnection.Create(FPushService);
FPushServiceConnection.OnChange := OnServiceConnectionChange;
FPushServiceConnection.OnReceiveNotification := OnReceiveNotificationEvent;
FPushServiceConnection.Active := true; // this will fires OnChange event
end;
end;
Here is details step by step article for android and iOS (use google translate) for C++ and Delphi code with ready to use PHP server code.
http://blog.rzaripov.kz/2017/02/firebase-android-ios.html http://blog.rzaripov.kz/2017/02/firebase-android-ios-2.html
Use this forum to ask questions (you may use English) http://fire-monkey.ru/topic/1809-статья-php-сервер-для-рассылки-push-на-android-и-ios/
Upvotes: 0
Reputation: 14499
The solution was comically simple for the amount of effort we took to solve.
FMX.PushNotification.iOS
import was missing in the code.
Funny thing is: if we remove the PushNotifications
imports, Delphi Seattle
won't show any warnings.
Upvotes: 5