Firebase Android Support

From RAD Studio
Jump to: navigation, search

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. Visit https://console.firebase.google.com/, click +Add project, and enter a name for your project, i.e. FirebaseApp.

Screenshot 1.PNG

Screenshot 2.PNG

2. You will be asked to set up Google Analytics for your project, select Not right now, and click Create project.

Screenshot 3.png

3. After you create the project, you need to register the FMX Android Application.

In the left navigation pane, go to the gear icon and select Project settings.

Screenshot 4.png

4. Click on the Android icon to access the Add Firebase to your Android app page.

Screenshot 5.png

5. The default package name for a FireMonkey application is com.embarcadero.FirebaseApp, but you should change it to com.yourcompany.packagename for your own applications.

Screenshot 6.png

Note: For existing applications that are available in the Google Play Store today, you will need to add the package name of that project here, instead of assigning a new name. Click on Register app.
Attention: The FMX project name has to match the project name registered in the Firebase Console. This means that the package value in Project Options > Application > Version info must match the "package name" value registered in the settings for the project in the Firebase Console. Consider that the default name in a Delphi project is: com.embarcadero.$(ModuleName), while it is recommended for developer to use package names that are based on their own domain.

6. Download the config file.

Screenshot 7.png

Click Next and skip this step at this point.

7. In the IDE, go to Project > Options... > Application > Services and click Import to import the configuration file (google-services.json).

Services.png

8. Go to Project > Options... > Application > Entitlement List and set Receive push notifications to true.

EntitlementList.png

9. Add a TMemo control to your form for showing the Firebase event logs. Rename the memo control to MemoLog.

10. Go to Object Inspector and initialize the Firebase Push Notification service and connection using an OnCreate event.

ObjectInspector.png

 uses 
   FMX.PushNotification.Android;
  
 procedure TFormMain.FormCreate(Sender: TObject);
 var 
   PushService: TPushService;
   ServiceConnection: TPushServiceConnection;
   Notifications: TArray<TPushServiceNotification>;
 begin
   PushService :=
 TPushServiceManager.Instance.GetServiceByName(TPushService.TServiceNames.FCM);
   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;
Note: Theusesstatement 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;

11. 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.FCM);
   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;

12. 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;
Note: The push notification title and body are not available in DataObject when the application is launched (coldstart) by tapping the notification in the Notification Center.

13. 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, or as an alternative, you can take it from the adb logcat. See the Log.d method call in the code snippet.

DeviceP1.png

14. 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.

15. Compose your notification by entering a title and message, then click ​Send test message​.

Screenshot 8.png

16. Enter the device token you previously copied and click ​Test​. This will push the message to your test device.

Screenshot 9.png

Attention: The new Firebase uses the Latest version of Google Play Services; hence, old implementation of MapView is not supported.
Tip: Go to ​Project > Options... > Application > Icons ​to specify a custom push notification icon and accent color.

IconsP.png

See Also

Application Options

Object Inspector

Project Menu

FireMonkey