Android Tutorial Integrating Firebase Services

Discussion in 'Tutorials & Examples' started by Erel, Jun 8, 2016.

Thread Status:
Not open for further replies.
  1. Erel

    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:
    Code:
    CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
    Firebase - Always required when using Firebase
    Code:
    CreateResourceFromFile(Macro, FirebaseAnalytics.Firebase)
    Firebase Notifications / Push messages
    Code:
    CreateResourceFromFile(Macro, FirebaseNotifications.FirebaseNotifications)
    Firebase Analytics
    Code:
    CreateResourceFromFile(Macro, FirebaseAnalytics.FirebaseAnalytics)
    Firebase Ads
    Code:
    CreateResourceFromFile(Macro, FirebaseAdMob.FirebaseAds)
    Firebase Auth
    Code:
    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:
    Code:
    '************ 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:
    Code:
    '************ 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)
    AddManifestText( <permission android:name="$
    {applicationId}.permission.C2D_MESSAGE"
      android:protectionLevel="signature" />)
    AddApplicationText(
    <receiver
      android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
      android:enabled="true">
      <intent-filter>
      <action android:name="com.google.android.gms.measurement.UPLOAD"/>
      </intent-filter>
      </receiver>

      <service
      android:name="com.google.android.gms.measurement.AppMeasurementService"
      android:enabled="true"
      android:exported="false"/>
       <provider
      android:authorities="$
    {applicationId}.firebaseinitprovider"
      android:name="com.google.firebase.provider.FirebaseInitProvider"
      android:exported="false"
      android:initOrder="100" />
        <receiver
      android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
      android:enabled="true">
      <intent-filter>
      <action android:name="com.google.android.gms.measurement.UPLOAD"/>
      </intent-filter>
      </receiver>

      <service
      android:name="com.google.android.gms.measurement.AppMeasurementService"
      android:enabled="true"
      android:exported="false"/>
       <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
    Code:
    '************ 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:
    Code:
    '************ Firebase Analytics ************
    AddApplicationText(
      <receiver
      android:name=
    "com.google.android.gms.measurement.AppMeasurementReceiver"
      android:enabled=
    "true">
      <
    intent-filter>
      <action android:name=
    "com.google.android.gms.measurement.UPLOAD"/>
      </
    intent-filter>
      </receiver>

      <
    service
      android:name=
    "com.google.android.gms.measurement.AppMeasurementService"
      android:enabled=
    "true"
      android:exported=
    "false"/>

      <
    service android:name="com.google.firebase.crash.internal.service.FirebaseCrashReceiverService"
      android:process=
    ":background_crash"/>

      <
    service android:name="com.google.firebase.crash.internal.service.FirebaseCrashSenderService"
      android:process=
    ":background_crash"/>
    )
    '************ Firebase Analytics (end) ************
    Ads (AdMob)
    Code:
    '************ 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
    Code:
    '************ Firebase Auth ************
    AddApplicationText(
      <
    activity android:name="com.google.android.gms.auth.api.signin.internal.SignInHubActivity"
      android:theme=
    "@android:style/Theme.Translucent.NoTitleBar"
      android:excludeFromRecents=
    "true"
      android:exported=
    "false" />

      <
    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: Mar 14, 2018
  2. Erel

    Erel Administrator Staff Member Licensed User

    Instructions updated with the new macros feature added in B4A v8.0.
     
Thread Status:
Not open for further replies.
Loading...