Android Tutorial Integrating Firebase Services

Status
Not open for further replies.

Erel

Administrator
Staff member
Licensed User
Google has acquired a company named Firebase and is now offering many backend services under the Firebase umbrella. Most of them are free.

Integrating the services is relatively simple.

1. Use B4A Sdk Manager tool to install all recommended items.

2. Register with Firebase and create a new project: https://console.firebase.google.com/
Make sure that the package name matches your app's package name.

3. Download google-services.json and put it in the projects folder (folder of the b4a file).

4. Add the manifest snippets to the manifest editor based on the services that you need.

5. Follow the instructions of the specific libraries.

Note that a recent version of Google Play Services should be installed on the device.
The minimum version of most features is 4.0 (API 14).
You can test whether Google Play Services is available with FirebaseAnalytics.IsGooglePlayServicesAvailable or with: https://www.b4x.com/android/forum/threads/check-and-install-google-play-services.84813

Update - If you are using B4A v8+:

Google Play Services Base
- Always required when using Google Play Services or Firebase:
B4X:
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
Firebase - Always required when using Firebase
B4X:
CreateResourceFromFile(Macro, FirebaseAnalytics.Firebase)
Firebase Notifications / Push messages
B4X:
CreateResourceFromFile(Macro, FirebaseNotifications.FirebaseNotifications)
Firebase Analytics
B4X:
CreateResourceFromFile(Macro, FirebaseAnalytics.FirebaseAnalytics)
Firebase Ads
B4X:
CreateResourceFromFile(Macro, FirebaseAdMob.FirebaseAds)
Firebase Auth
B4X:
CreateResourceFromFile(Macro, FirebaseAuth.FirebaseAuth)
Manifest Snippets Repository

Make sure to copy the snippets together with the comments. This will make it easier to update snippets in the future.
There is no need to change anything in the snippets.

Always required when using Google Play Services or Firebase:
B4X:
'************ Google Play Services Base ************
AddApplicationText(
   <activity android:name="com.google.android.gms.common.api.GoogleApiActivity"
  android:theme="@android:style/Theme.Translucent.NoTitleBar"
  android:exported="false"/>
    <meta-data
  android:name="com.google.android.gms.version"
  android:value="@integer/google_play_services_version" />
)
'************ Google Play Services Base (end) ************
Always required when using Firebase:
B4X:
'************ Firebase Base ************
CreateResourceFromFile("google-services", "google-services.json")
AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.WAKE_LOCK)
AddPermission(com.google.android.c2dm.permission.RECEIVE)
AddPermission(${applicationId}.permission.C2D_MESSAGE)
AddPermission(com.google.android.finsky.permission.BIND_GET_INSTALL_REFERRER_SERVICE)
AddManifestText( <permission android:name="${applicationId}.permission.C2D_MESSAGE"
  android:protectionLevel="signature" />)
AddApplicationText(
 <receiver
            android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
            android:enabled="true"
            android:exported="false" >
        </receiver>

  <service
            android:name="com.google.android.gms.measurement.AppMeasurementService"
            android:enabled="true"
            android:exported="false" />
           
 <receiver
            android:name="com.google.android.gms.measurement.AppMeasurementInstallReferrerReceiver"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.INSTALL_PACKAGES" >
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
</receiver>
<service
            android:name="com.google.android.gms.measurement.AppMeasurementJobService"
            android:enabled="true"
            android:exported="false"
            android:permission="android.permission.BIND_JOB_SERVICE" />

 <service android:name="com.google.firebase.components.ComponentDiscoveryService" >
            <meta-data
                android:name="com.google.firebase.components:com.google.firebase.analytics.connector.internal.AnalyticsConnectorRegistrar"
                android:value="com.google.firebase.components.ComponentRegistrar" />
            <meta-data
                android:name="com.google.firebase.components:com.google.firebase.iid.Registrar"
                android:value="com.google.firebase.components.ComponentRegistrar" />
             <meta-data
                android:name="com.google.firebase.components:com.google.firebase.auth.FirebaseAuthRegistrar"
                android:value="com.google.firebase.components.ComponentRegistrar" />
</service>
   <provider
  android:authorities="${applicationId}.firebaseinitprovider"
  android:name="com.google.firebase.provider.FirebaseInitProvider"
  android:exported="false"
  android:initOrder="100" />
 
   <receiver
  android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
  android:exported="true"
  android:permission="com.google.android.c2dm.permission.SEND" >
  <intent-filter>
  <action android:name="com.google.android.c2dm.intent.RECEIVE" />
  <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
  <category android:name="${applicationId}" />
  </intent-filter>
  </receiver>
    <receiver
  android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
  android:exported="false" />


  <service
  android:name="com.google.firebase.iid.FirebaseInstanceIdService"
  android:exported="true">
  <intent-filter android:priority="-500">
  <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
  </intent-filter>
  </service>
)

'************ Firebase Base (end) ************
Notifications / FCM (Firebase Cloud Messaging) / push messages
B4X:
'************ Firebase Notifications ************
AddApplicationText(
    <service
  android:name="com.google.firebase.messaging.FirebaseMessagingService"
  android:exported="true">
  <intent-filter android:priority="-500">
  <action android:name="com.google.firebase.MESSAGING_EVENT" />
  </intent-filter>
  </service>
   <service android:name="anywheresoftware.b4a.objects.FirebaseNotificationsService">
     <intent-filter>
  <action android:name="com.google.firebase.MESSAGING_EVENT"/>
     </intent-filter>
   </service>
)
'************ Firebase Notifications (end)************
Analytics and crash reports:

Nothing to add.

Ads (AdMob)
B4X:
'************ Firebase Ads ************
AddApplicationText(
  <activity
  android:name="com.google.android.gms.ads.AdActivity"
  android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
  android:theme="@android:style/Theme.Translucent" />
  <activity android:name="com.google.android.gms.ads.purchase.InAppPurchaseActivity"
  android:theme="@style/Theme.IAPTheme"/>
)
'************ Firebase Ads (end) ************
Auth
B4X:
'************ Firebase Auth ************
AddApplicationText(
  <activity
            android:name="com.google.firebase.auth.internal.FederatedSignInActivity"
            android:excludeFromRecents="true"
            android:exported="true"
            android:launchMode="singleInstance"
            android:permission="com.google.firebase.auth.api.gms.permission.LAUNCH_FEDERATED_SIGN_IN"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

            <activity
            android:name="com.google.android.gms.auth.api.signin.internal.SignInHubActivity"
            android:excludeFromRecents="true"
            android:exported="false"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

        <!--
            Service handling Google Sign-In user revocation. For apps that do not integrate with
            Google Sign-In,, this service will never be started.
        -->
        <service
            android:name="com.google.android.gms.auth.api.signin.RevocationBoundService"
            android:exported="true"
            android:permission="com.google.android.gms.auth.api.signin.permission.REVOCATION_NOTIFICATION" />
'************ Firebase Auth (end) ************
 
Last edited:
Status
Not open for further replies.
Top