FCM using FirebaseMessagingService

1,174 views
Skip to first unread message

Przemek Lach

unread,
Jul 24, 2018, 8:16:31 PM7/24/18
to bazel-discuss
Hi,

I built a firebase messaging enabled app in Android Studio and FirebaseInitProvider initializes successfully and I get my device token.

When I attempt to do the same thing using Bazel I get 'I/FirebaseInitProvider: FirebaseApp initialization unsuccessful' and not token. I'm using the same google-services.json file.

My build files is as follows:

android_binary(
   name = "FcmTestingApplication",
   srcs = glob(["*.java"]),
   manifest = "AndroidManifest.xml",
   manifest_values = {"applicationId": "com.mycompany.fcmtestingapplication"},
   resource_files = glob([
       "res/**/*.png",
       "res/**/*.jpg",
       "res/**/*.xml",
   ]),
   visibility = ["//visibility:public"],
   deps = [
       ":maven",
   ],
)

android_library(
   name = "maven",
   exports = [
       gmaven_artifact("com.android.support:appcompat:aar:v7.27.1.1"),
       gmaven_artifact("com.android.support:constraint-constraint-layout:aar:1.1.2"),
       gmaven_artifact("com.android.support:localbroadcastmanager:aar:28.0.0-alpha3"),
       gmaven_artifact("com.android.support:support.annotations:jar:27.1.1"),
       gmaven_artifact("com.google.android.gms:play-services-location:aar:15.0.1"),
       gmaven_artifact("com.google.firebase:firebase.messaging:aar:17.1.0"),
   ],
)

My manifest is as follows:

<?xml version="1.0" encoding="utf-8"?>
          package="com.mycompany.fcmtestingapplication"
          android:versionName="V1.0">
  <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="27" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />
  <application
      android:allowBackup="false"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme"
      android:name="com.mycompany.fcmtestingapplication.FcmTestingApplicationApplication">
    <activity
        android:name=".LaunchActivity"
        android:screenOrientation="sensor"
        android:configChanges="orientation|screenSize" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <service android:name=".FcmTestingApplicationFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>
  </application>
</manifest>


And finally my service implemented:

package com.mycompany.fcmtestingapplication;

import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class ApiIntegrationFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Log.e("##?", "RM from=" + remoteMessage.getFrom());
        Log.e("##?", "RM message_id=" + remoteMessage.getMessageId());

        for (String key : remoteMessage.getData().keySet()) {
            Log.e("##?", "RM data_key=" + key + " value=" + remoteMessage.getData().get(key));
        }
    }

    @Override
    public void onNewToken(String s) {
        // super.onNewToken(s);
        Log.e("##?", "token=" + s);
    }
}

I have a feeling this is some kind of authentication error. Any thoughts?

Alex Humesky

unread,
Jul 24, 2018, 8:28:34 PM7/24/18
to Przemek Lach, bazel-discuss
Is your integration with FCM / google services based on the example from this thread?

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/a577ee3e-8a02-44ff-9454-11d3ad784e45%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alex Humesky

unread,
Jul 24, 2018, 8:29:53 PM7/24/18
to Przemek Lach, bazel-discuss
(note also that we're reworking that example into a proper tool + rules hosted under https://github.com/bazelbuild/)

Przemek Lach

unread,
Jul 24, 2018, 8:31:39 PM7/24/18
to bazel-discuss
Hi Alex,

Yes it is but it has been so long I'm not sure that thread is applicable anymore. I'm also using the FirebaseMessagingService so that part is different as well.

Alex Humesky

unread,
Jul 24, 2018, 8:41:22 PM7/24/18
to Przemek Lach, bazel-discuss
The example should still basically work. I think the only difference today would be what deps the android_binary needs and how they're specified (basically what you're already doing with the android_binary you've listed).

I think what you can try is:
1. copy the gson dep from the WORKSPACE in the example
2. copy the tools directory
3. in your BUILD file add:

load("//tools/googleservices:defs.bzl", "google_services_xml")

GOOGLE_SERVICES_XML = google_services_xml(
    packageName = "com.example.myapplication",
    google_services_json = "google-services.json")

4. add the GOOGLE_SERVICES_XML to your resources:

resource_files = glob([
        "res/**/*.png",
        "res/**/*.jpg",
        "res/**/*.xml",
    ]) + GOOGLE_SERVICES_XML,

We're reworking this into something easier and cleaner to use, but the above should at least get it working. 

Przemek Lach

unread,
Jul 30, 2018, 1:20:43 PM7/30/18
to bazel-discuss
Hi Alex,

So that worked. I had also had to add android.permission.WAKE_LOCK for some reason to my manifest otherwise it would not give me a token.

Alex Humesky

unread,
Jul 30, 2018, 2:17:04 PM7/30/18
to Przemek Lach, bazel-discuss
Great, glad to hear it.

Regarding the permissions, there's probably a library in the dependencies somewhere that requires it to run. Bazel's android rules don't merge permissions from dependencies. More info about that is here: https://github.com/bazelbuild/bazel/issues/5411

Alex Humesky

unread,
Aug 3, 2018, 7:20:22 PM8/3/18
to bazel-discuss, Przemek Lach
Hi all,
We pushed the tooling and examples here to the tools_android and examples repos so that it's easier to integrate with Google Services / FCM. See:

Reply all
Reply to author
Forward
0 new messages