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

    Firebase depends on B4A v6+ which was not yet released.

    Google has recently 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. Open Android SDK Manager and install the latest versions of:

    - Android Support Repository
    - Google Repository

    [​IMG]

    2. Register with Firebase and create a new project: https://firebase.google.com/docs/
    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 2.3 (API 9).
    You can test whether Google Play Services is available with FirebaseAnalytics.IsGooglePlayServicesAvailable.

    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) ************
    Please post questions in the questions forum: https://www.b4x.com/android/forum/forums/android-questions.26/
     
    Last edited: Jun 13, 2016
    fdaniele, Beja, f0raster0 and 23 others like this.
Thread Status:
Not open for further replies.
Loading...