2

I'm trying to migrate Android client app from Google Cloud Messaging to Firebase Cloud Messaging. I strictly followed an official tutorial, but didn't succeed - onMessageReceived() method is not called when app in foreground.

So here are code snippets that I touched.

build.gradle (project level)

dependencies {
    //other stuff
    classpath 'com.google.gms:google-services:3.0.0'
}

build.gradle (app level)

apply plugin: 'com.google.gms.google-services' //this line in the bottom

and

dependencies {
    //other stuff here
    compile 'com.google.firebase:firebase-messaging:9.0.0'
}

AndroidManifest.xml

<service android:name=".service.FirebaseService">
  <intent-filter>
    <action android:name="com.google.firebase.MESSAGING_EVENT" />
  </intent-filter>
</service>

and

<service
  android:name=".service.MyInstanceIDListenerService"
  android:exported="false">
   <intent-filter>
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
   </intent-filter>
</service>

and

<service
     android:name=".service.RegistrationIntentService"
     android:exported="false" />

as childs of <application> tag.

MyInstanceIDListenerService.java

public class MyInstanceIDListenerService extends FirebaseInstanceIdService {

final String TAG = "Firebase instance id";

@Override
public void onTokenRefresh() {

    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);
    sendRegistrationToServer(refreshedToken);
}

private void sendRegistrationToServer(String token) {
    Intent intent = new Intent(RegistrationIntentServiceEvent.TOKEN);
    intent.putExtra(RegistrationIntentServiceEvent.TOKEN, token);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}

FirebaseService.java

public class FirebaseService extends FirebaseMessagingService {


//OtherStuff

@Override
public void onMessageReceived(RemoteMessage message){
    String from = message.getFrom();
    Map data = message.getData();

    Log.d(TAG, "Message: " + from); //Never appears in logcat
    //other stuff
}
}

RegistrationIntentService

public class RegistrationIntentService extends IntentService {

private static final String TAG = "RegIntentService";

public RegistrationIntentService() {
    super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {
    try {
        synchronized (TAG) {
            String token = FirebaseInstanceId.getInstance().getToken();
            Log.i(TAG, "GCM Registration Token: " + token);//This works fine
            //and shows this line - 12-19 17:59:33.295 3146-5386/ru.bpc.mobilebank.bpc I/RegIntentService: GCM Registration Token: *some_token*

            sendRegistrationToServer(token);
        }
    } catch (Exception e) {
        Log.d(TAG, "Failed to complete token refresh", e);
    }
}

private void sendRegistrationToServer(String token) {
    Intent intent = new Intent(RegistrationIntentServiceEvent.TOKEN);
    intent.putExtra(RegistrationIntentServiceEvent.TOKEN, token);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}

Please, tell if i did something wrong and why registrations seems to be done properly, but the onMessageReceived() method is never called even if the app is in foreground. Thanks in advance.

P.S. By the way, is adding SHA-1 keys in Firebase console necessary? maybe this could cause the problem? But Firebase says this action is optional.

Dmitry Smolyaninov
  • 2,159
  • 1
  • 18
  • 32
  • 1
    Have you added google-services.json file in your app? Maybe you need to perform additional setup in Firebase console or configuration. Check here https://firebase.google.com/docs/android/setup if you have everything setup correctly – MatPag Dec 19 '16 at 16:25
  • Ye, sure I added google-services.json. Ye, thanks for advice, I'll go and check setup, problem could be there. – Dmitry Smolyaninov Dec 19 '16 at 16:30
  • By the way is adding SHA-1 keys in Firebase console necessary? maybe this could cause the problem? But Firebase says this is optional. – Dmitry Smolyaninov Dec 19 '16 at 16:38
  • 1
    As in my case, I registered 2 SHA-1 fingerprints since I definitely use 2 different keystores, one for debug and one for production. I don't think that these steps are optional, in fact I think these are required, based on the documentation: https://developers.google.com/cloud-messaging/android/android-migrate-fcm. Can't find any "optional" words there, though, CMIIW. – nic Dec 19 '16 at 16:52
  • Tanks for helping. Ye, maybe this could be the reason. There is no "optional" word in docs, but there is directly in Firebase console, when you add new projects (I have 6 product flavours, that made me add 6 projects, and each of them requires SHA-1). – Dmitry Smolyaninov Dec 19 '16 at 17:03

1 Answers1

1

if you are using FCM then you should replace this

 <service android:name=".service.GcmService">
  <intent-filter>
    <action android:name="com.google.firebase.MESSAGING_EVENT" />
  </intent-filter>
</service>

to

 <service android:name=".service.FirebaseService">
  <intent-filter>
    <action android:name="com.google.firebase.MESSAGING_EVENT" />
  </intent-filter>
</service>

your service not registered properly..

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • 1
    As this guy have posted, @dmitry-smolyaninov class is now called FirebaseService, and not GcmService, the service isn't registered properly in AndroidManifest.xml. Upvoted. – nic Dec 19 '16 at 16:44
  • Oh, sorry. I just forgot to replace all original class names here. I renamed references and names of some classes in question to make in more clear and reasonable. In project classes still have GSM-names, but implement FCM functionality. I just don't make refactor untill solve this issue. – Dmitry Smolyaninov Dec 19 '16 at 16:49