6

I have followed the setup process as mentioned for Firebase Cloud Messaging. But somehow onTokenRefresh is never called.

Following are changes in this regard:

app's build.gradle:

dependencies {
    ...
    compile 'com.google.firebase:firebase-messaging:9.4.0'
}

apply plugin: 'com.google.gms.google-services'

project's build.gradle:

dependencies {
    classpath 'com.android.tools.build:gradle:2.1.2'
    classpath 'com.google.gms:google-services:3.0.0'
}

AndroidManifest.xml

<application
    ...
    <service
        android:name="com.blynq.app.services.MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

    <service
        android:name="com.blynq.app.services.MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

</application>


public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    @Override
    public void onTokenRefresh() {
        String token = FirebaseInstanceId.getInstance().getToken();
        Log.i("FCM", "Token refreshed - " + token);
        registerTokenWithServer(token);
    }
}

Logs state I/FirebaseInitProvider: FirebaseApp initialization successful, but but onTokenRefresh() is not executed.

I sure am missing something, but unable to understand where.

EDIT: Happening only with emulators, android device worked fine with the above settings.

Jatin Balodhi
  • 152
  • 5
  • 18
jay
  • 1,982
  • 2
  • 24
  • 54
  • 1
    try uninstalling the application before installing it again. ps: only the first action "INSTANCE_ID_EVENT" is correct. you can remove the second. – Diego Giorgini Aug 15 '16 at 13:32
  • Tried reinstalling, did not work – jay Aug 15 '16 at 13:37
  • Have a look at it: http://stackoverflow.com/a/37831267/1739882 – Chintan Soni Aug 15 '16 at 13:48
  • @ChintanSoni - I am not using tools:node="replace" anywhere in my application tag. – jay Aug 15 '16 at 13:51
  • 1
    Just found out this is happening only with emulators, android device worked fine with the above settings. – jay Aug 15 '16 at 14:07
  • @jay are you using tools namespace in manifest ? – Chintan Soni Aug 15 '16 at 14:27
  • `onTokenRefresh()` will only get called when the token changes. There is a good chance your app already generated a token. You can either remove the app data/reinstall the app or also read the token in your MainActivity.onCreate with `FirebaseInstanceId.getInstance().getToken()`. – Frank van Puffelen Aug 15 '16 at 14:45
  • Possible duplicate of [Firebase FCM force onTokenRefresh() to be called](http://stackoverflow.com/questions/37454501/firebase-fcm-force-ontokenrefresh-to-be-called) – Machado Aug 15 '16 at 20:39

1 Answers1

1

FCM clients require devices running Android 2.3 or higher that also have the Google Play Store app installed, or an emulator running Android 2.3 with Google APIs.

Jaydev
  • 1,794
  • 20
  • 37