Firebase Android Support
Firebase Android Support
RAD Studio 10.3 Release 2, includes support for Firebase Android. To enable Firebase Android for your application, follow the steps below:
1. Go to https://console.firebase.google.com/ to Create a Project.
2. Register your FMX Android application in the Firebase console.
3. Download the config file (google-services.json).
4. In the IDE, go to Project > Options... > Application > Services and click Import to import the configuration file (google-services.json).
5. Go to Project > Options... > Application > Entitlement List and set Receive Push Notifications to True.
6. Add a TMemo control to your form for showing the Firebase event logs. Rename the memo control to MemoLog.
7. Go to Object Inspector and initialize the Firebase Push Notification service and connection using an OnCreate event.
uses FMX.PushNotification.Android; procedure TFormMain.FormCreate(Sender: TObject); var PushService: TPushService; ServiceConnection: TPushServiceConnection; Notifications: TArray<TPushServiceNotification>; begin PushService := TPushServiceManager.Instance.GetServiceByName(TPushService.TServiceNames.GCM); ServiceConnection := TPushServiceConnection.Create(PushService); ServiceConnection.Active := True; ServiceConnection.OnChange := OnServiceConnectionChange; ServiceConnection.OnReceiveNotification := OnReceiveNotificationEvent; FDeviceId := PushService.DeviceIDValue[TPushService.TDeviceIDNames.DeviceId]; MemoLog.Lines.Add('DeviceID: ' + FDeviceId); MemoLog.Lines.Add('Ready to receive!'); // Checks notification on startup, if application was launched fromcold start // by tapping on Notification in Notification Center Notifications := PushService.StartupNotifications; if Length(Notifications) > 0 then begin MemoLog.Lines.Add('-----------------------------------------'); MemoLog.Lines.Add('DataKey = ' + Notifications[0].DataKey); MemoLog.Lines.Add('Json = ' + Notifications[0].Json.ToString); MemoLog.Lines.Add('DataObject = ' + Notifications[0].DataObject.ToString); MemoLog.Lines.Add('-----------------------------------------'); end; end;
uses
statement for android push notifications needs to be in an IFDEF if you want to compile the same application for other platforms.This service instance is used to register a device in Firebase and receive a unique device token that is used as a device service of the push message recipient. For this code to compile, you need to add two fields to your form class:
FDeviceId: string; FDeviceToken: string;
8. Implement OnServiceConnectionChange for getting changes , i.e., the device token.
procedure TFormMain.OnServiceConnectionChange(Sender: TObject; PushChanges: TPushService.TChanges); var PushService: TPushService; begin PushService := TPushServiceManager.Instance.GetServiceByName(TPushService.TServiceNames.GCM); if TPushService.TChange.DeviceToken in PushChanges then begin FDeviceToken := PushService.DeviceTokenValue[TPushService.TDeviceTokenNames.DeviceToken]; MemoLog.Lines.Add('FireBase Token: ' + FDeviceToken); Log.d('Firebase device token: token=' + FDeviceToken); end; if (TPushService.TChange.Status in PushChanges) and (PushService.Status = TPushService.TStatus.StartupError) then MemoLog.Lines.Add('Error: ' + PushService.StartupError); end;
9. When you receive the device token from Firebase, implement OnReceiveNotificationEvent to receive push payload data.
var MessageText: string; begin MemoLog.Lines.Add('-----------------------------------------'); MemoLog.Lines.Add('DataKey = ' + ServiceNotification.DataKey); MemoLog.Lines.Add('Json = ' + ServiceNotification.Json.ToString); MemoLog.Lines.Add('DataObject = ' + ServiceNotification.DataObject.ToString); MemoLog.Lines.Add('---------------------------------------'); end;
10. Deploy your FMX application to your Android device. You should now see the device token in the log and that the app was automatically registered in FireBase. To send a push message to the device, copy the Firebase token from the memo.
11. Go to https://console.firebase.google.com/ and select the project you previously created. Then, click Grow> Cloud Messaging> Send your first message from the side menu.
12. Compose your notification by entering a title and message, then click Send test message.
13. Enter the device token you previously copied and click Test. This will push the message to your test device.