FCM Token Issue in some android device
<p>FCM Expert Please Help me out,I have to integrate notification for the android using FCM, I am trying the demo given by Firebase, I tried many logic but it's giving first token for the some devices:
I used three device, Moto E (2nd Gen)(android version 5.1.1), Samsung GT-S7562(A.V 4.1.2) and Karbon Titanium (A.V 4.2.2), I am receiving the token only for Karbon Titanium ,don't know why I am not receiving the token for Moto E(2 Gen)and Samsung GT-S7562 but receiving the token from karbon Titanium device :</p>

<p>Here is the code I tried :</p>

<p>Logcat Moto E2 :</p>

<pre><code>07-07 15:58:08.776 24152-24152/? I/art: Late-enabling -Xcheck:jni
07-07 15:58:08.802 24152-24162/? E/art: Failed sending reply to debugger: Broken pipe
07-07 15:58:08.802 24152-24162/? I/art: Debugger is no longer active
07-07 15:58:08.848 24152-24152/? D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
07-07 15:58:08.859 24152-24152/? D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
07-07 15:58:08.872 24152-24152/? I/FA: App measurement is starting up, version: 9256
07-07 15:58:08.872 24152-24152/? I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
07-07 15:58:09.027 24152-24152/? I/FirebaseInitProvider: FirebaseApp initialization successful
07-07 15:58:09.196 24152-24152/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-07 15:58:09.361 24152-24192/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-07 15:58:09.378 24152-24152/? D/Atlas: Validating map...
07-07 15:58:09.564 24152-24192/? I/Adreno-EGL: &lt;qeglDrvAPI_eglInitialize:379&gt;: EGL 1.4 QUALCOMM build:  (Ibddc658e36)
                                               OpenGL ES Shader Compiler Version: E031.25.03.04
                                               Build Date: 08/10/15 Mon
                                               Local Branch: workspace
                                               Remote Branch: 
                                               Local Patches: 
                                               Reconstruct Branch: 
07-07 15:58:09.567 24152-24192/? I/OpenGLRenderer: Initialized EGL, version 1.4
07-07 15:58:09.609 24152-24192/? D/OpenGLRenderer: Enabling debug mode 0
07-07 15:58:12.860 24152-24242/com.shubhank.fcm_tutorial W/InstanceID/Rpc: Found 10016
07-07 15:58:13.049 24152-24242/com.shubhank.fcm_tutorial D/FirebaseInstanceId: background sync failed: PHONE_REGISTRATION_ERROR, retry in 10s
07-07 15:58:19.256 24152-24178/com.shubhank.fcm_tutorial I/FA: Tag Manager is not found and thus will not be used
07-07 15:58:23.194 24152-24448/com.shubhank.fcm_tutorial D/FirebaseInstanceId: background sync failed: PHONE_REGISTRATION_ERROR, retry in 20s
</code></pre>

<p>Logcat of Karbon Mobile:</p>

<pre><code>07-07 16:38:03.964 3939-3939/com.shubhank.fcm_tutorial I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
07-07 16:38:04.004 3939-3939/com.shubhank.fcm_tutorial I/FirebaseInitProvider: FirebaseApp initialization successful
07-07 16:38:04.017 3939-3939/com.shubhank.fcm_tutorial W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
07-07 16:38:04.017 3939-3939/com.shubhank.fcm_tutorial I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
07-07 16:38:04.017 3939-3939/com.shubhank.fcm_tutorial W/dalvikvm: VFY: unable to resolve interface method 19193: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
07-07 16:38:04.017 3939-3939/com.shubhank.fcm_tutorial I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
07-07 16:38:04.017 3939-3939/com.shubhank.fcm_tutorial W/dalvikvm: VFY: unable to resolve interface method 19197: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
07-07 16:38:04.049 3939-3939/com.shubhank.fcm_tutorial I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
07-07 16:38:04.049 3939-3939/com.shubhank.fcm_tutorial W/dalvikvm: VFY: unable to resolve virtual method 577: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
07-07 16:38:04.050 3939-3939/com.shubhank.fcm_tutorial I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
07-07 16:38:04.050 3939-3939/com.shubhank.fcm_tutorial W/dalvikvm: VFY: unable to resolve virtual method 599: Landroid/content/res/TypedArray;.getType (I)I
07-07 16:38:04.092 3939-3939/com.shubhank.fcm_tutorial I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable
07-07 16:38:04.092 3939-3939/com.shubhank.fcm_tutorial W/dalvikvm: VFY: unable to resolve virtual method 540: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-07 16:38:04.092 3939-3939/com.shubhank.fcm_tutorial I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity
07-07 16:38:04.092 3939-3939/com.shubhank.fcm_tutorial W/dalvikvm: VFY: unable to resolve virtual method 542: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-07 16:38:04.096 3939-3939/com.shubhank.fcm_tutorial W/notification: faQNkJ65F5c:APA91bFHH5rVOhIlX5k7Pxo-sbZPwZplWqBGLVOjlG8G4NwoMqaKZQRYWMMjliYXb0gokiuJPBYpWY0G7L-FEbgAv7LfKcuR0j7Sr6ATxb1j2UR7A
07-07 16:38:04.148 3939-3939/com.shubhank.fcm_tutorial I/MaliEGL: [Mali]window_type=1, is_framebuffer=0, errnum = 0
07-07 16:38:04.148 3939-3939/com.shubhank.fcm_tutorial I/MaliEGL: [Mali]surface-&gt;num_buffers=4, surface-&gt;num_frames=3, win_min_undequeued=1
07-07 16:38:04.148 3939-3939/com.shubhank.fcm_tutorial I/MaliEGL: [Mali]max_allowed_dequeued_buffers=3
07-07 16:38:04.159 3939-3939/com.shubhank.fcm_tutorial I/[MALI][Gralloc]: dlopen libsec_mem.so fail
</code></pre>

<p>MainActivity:</p>

<pre><code>    public class MainActivity extends AppCompatActivity {
    Tracker mTracker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String token = FirebaseInstanceId.getInstance().getToken();
        if (token != null) {
            Log.w("notification", token);
            Toast.makeText(MainActivity.this,""+token,Toast.LENGTH_SHORT).show();
        }

    }


}
</code></pre>

<p>app gradle :</p>

<pre><code>apply plugin: 'com.android.application'
   android {
        compileSdkVersion 23
        buildToolsVersion "23.0.3"

        defaultConfig {
            applicationId "com.shubhank.fcm_tutorial"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.google.android.gms:play-services-analytics:9.2.0'

        compile 'com.android.support:appcompat-v7:23.3.0'
        compile 'com.google.firebase:firebase-core:9.0.1'
        compile 'com.google.firebase:firebase-messaging:9.0.1'
        compile 'com.android.support:design:23.3.0'
    }


    apply plugin: 'com.google.gms.google-services'
</code></pre>

<p>top level gradle :</p>

<pre><code>buildscript {
    repositories {
        jcenter()
        mavenLocal()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.google.gms:google-services:3.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        mavenLocal()
    }
}
</code></pre>

<p>TokenService:</p>

<pre><code>public class TokenService extends FirebaseInstanceIdService {

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.w("notification", refreshedToken);
    sendRegistrationToServer(refreshedToken);
}

private void sendRegistrationToServer(String token) {
}
}
</code></pre>

<p>FCMMessageReceiverService:</p>

<pre><code>public class FCMMessageReceiverService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.w("fcm", "received notification");
    sendNotification(remoteMessage.getNotification().getTitle());
}

private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(messageBody)
            .setAutoCancel(false)
            .setSound(defaultSoundUri);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(1, notificationBuilder.build());
}
}
</code></pre>

<p>Manifest File:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.shubhank.fcm_tutorial"&gt;
    &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
    &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt;
    &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt;

    &lt;application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:launchMode="standard"
        android:theme="@style/AppTheme"&gt;
        &lt;activity android:name=".MainActivity"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;

                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;

        &lt;service android:name=".TokenService"
            android:exported="true"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="com.google.firebase.INSTANCE_ID_EVENT" /&gt;
            &lt;/intent-filter&gt;
        &lt;/service&gt;
        &lt;service android:name=".FCMMessageReceiverService"
            android:exported="true"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="com.google.firebase.MESSAGING_EVENT" /&gt;
            &lt;/intent-filter&gt;
        &lt;/service&gt;
        &lt;/activity&gt;
        &lt;meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/&gt;
    &lt;/application&gt;

&lt;/manifest&gt;
</code></pre>