1

I am integrating firebase to my app but the problem is that I am receiving the token below the lollipop, don't figure out why token is coming null for the lollipop. Here is my code.

Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.sixmod.com.client">
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <!--GCM amitranjan@sixmod5 id-->
   <!-- AIzaSyBo-v2RUKKoe37pZwYeurtoDFw1egxy7fM-->
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="22" />
    <uses-permission
        android:name="android.permission.READ_EXTERNAL_STORAGE"
        android:maxSdkVersion="22" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
    <uses-permission android:name="android.permission.GET_TASKS"/>
    <uses-permission android:name="android.permission.READ_CONTACTS" />



    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />


    <uses-permission
        android:name="com.google.android.c2dm.permission.RECEIVE" />
    <permission
        android:name="com.sixmod.com.client.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission
        android:name="com.sixmod.com.client.permission.C2D_MESSAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/truck"
        android:label="@string/app_name"
        android:name="com.sixmod.com.sixmod.MyLocalytic.MyApplication"

        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="android:icon">



        <service android:name="com.sixmod.com.sixmod.MyReceiver.BroadcastService" />



    <activity

            android:name="com.sixmod.FragmentContainerActivity"
            android:windowSoftInputMode="adjustPan"
        android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <!--<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyCsguzmCQrHlTaBQ_6ixFyQUk" />-->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBo-37pZwYeurtoDFw1egxy7fM " />


        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.bugsnag.android.API_KEY"
            android:value="221c5b778e92fdd345fa63e09"/>


        <meta-data android:name="LOCALYTICS_APP_KEY" android:value="7f652c5db10aa2dbd542-d159-11e5-6885-002dea3c3994"/>        <receiver
            android:name="com.sixmod.com.sixmod.MyLocalytic.LocalyticCustomReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
                <action android:name="com.google.android.c2dm.intent.RECEIVE"/>

                <category android:name="com.sixmod.com.sixmod.MyLocalytic"/>
            </intent-filter>
        </receiver>

        <service
            android:name="com.sixmod.com.sixmod.service.MyOrderService"
            android:exported="false">
        </service>

        <!--<meta-data
            android:name="LOCALYTICS_APP_KEY"
            android:value="YOUR-LOCALYTICS-APP-KEY"/>-->
       <!-- <receiver
            android:name="com.localytics.android.ReferralReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
        </receiver>-->

        <intent-filter>
            <data android:scheme="7f652c5db10aa2dbd5b87f7-80b0ac42-d159-11e5-6885-002dea3c3994" />
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
        </intent-filter>

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

        <service
            android:name="com.sixmod.com.sixmod.notificationFireBase.MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

        <service
            android:name="com.sixmod.com.sixmod.notificationFireBase.RegistrationIntentService">

        </service>


    </application>

</manifest>

MyFirebaseInstanceIDService:

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    // [START refresh_token]
    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // TODO: Implement this method to send any registration to your app's servers.
       // sendRegistrationToServer(refreshedToken);
    }
    // [END refresh_token]

    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
        OkHttpClient client= new OkHttpClient();
        RequestBody body=new FormBody.Builder().add("token", token).build();
        Request request=new Request.Builder().url("https://logizoapp-cf159.firebaseio.com/").build();// enter php url
        try {
            client.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

MyFirebaseMessagingService:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    // [START receive_message]
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO(developer): Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
        sendNotification(remoteMessage.getNotification().getBody());
    }
    // [END receive_message]

    /**
     * Create and show a simple notification containing the received FCM message.
     *
     * @param messageBody FCM message body received.
     */
    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, FragmentContainerActivity.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.drawable.ic_stat_ic_notification)
                .setContentTitle("FCM Message")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

From Fragment Class

String token = FirebaseInstanceId.getInstance().getToken();
Log.i("regToken", "FCM Registration Token: " + token);
Cœur
  • 32,421
  • 21
  • 173
  • 232
  • Can you please add the manifest.xml also? – Saini Jul 05 '16 at 17:17
  • the error that you are asking about is because of two API's crossing their ways. – Saini Jul 05 '16 at 18:31
  • Just use one api (FCM) – Saini Jul 05 '16 at 18:31
  • which api i have to remove, if they are crossing then token key is also not generated for the lower version, what you think ? – Barnali Bhattacharjee Jul 05 '16 at 18:33
  • i replaced manifest with your edited manifest but getting same error :07-06 00:10:38.021 10684-10684/com.sixmod.com.client I/regToken: FCM Registration Token: null – Barnali Bhattacharjee Jul 05 '16 at 18:41
  • i also made one separate demo project , same error i am getting , it's working for lower version. – Barnali Bhattacharjee Jul 05 '16 at 18:44
  • When are you calling FirebaseInstanceId.getInstance().getToken()? Could you provide more context around this call? – Arthur Thompson Jul 06 '16 at 17:55
  • @ArthurThompson , i need the token so that i can send it to server side, for the some device i am getting for the some i don't, I done the demo to but still the same issue http://stackoverflow.com/questions/38219294/firebase-token-issue-in-some-device?noredirect=1#comment63864413_38219294 – Barnali Bhattacharjee Jul 07 '16 at 09:10
  • Are you getting the onTokenRefresh callback? If you never get this callback then your token is never generated, be sure to uninstall and reinstall the app to ensure the callback is fired. – Arthur Thompson Jul 07 '16 at 16:55
  • @ArthurThompson for the Karbon mobile it's also not calling onToken Refresh but i am getting the token when i print on the mainActivity and for the MotoE2 device neither token is generated from the main activity nor token refresh is called . Many times i rebooted my device as well as uninstall and install the app. – Barnali Bhattacharjee Jul 07 '16 at 17:44
  • sry .. @ArthurThompson for the Karbon mobile it's calling onToken Refresh aswell as i am getting the token when i print on the mainActivity and for the MotoE2 device neither token is generated from the main activity nor token refresh is called . Many times i rebooted my device as well as uninstall and install the app. – Barnali Bhattacharjee Jul 07 '16 at 17:54
  • If the onTokenRefresh callback is not fired then anywhere you call getToken you will get null, what version of Google Play Services is running on the MotoE2? – Arthur Thompson Jul 07 '16 at 18:49
  • @ArthurThompson on MotoE2 it's 9.2.56 same in Karbon – Barnali Bhattacharjee Jul 07 '16 at 19:27

1 Answers1

0

1) Just use Firebase Cloud Messaging(FCM) API replacing the GCM.

2) Demo Project: https://github.com/firebase/quickstart-android/tree/master/messaging

3) For more information : https://firebase.google.com/docs/cloud-messaging/android/client

update:

Your project is not right. It has many error possibilities.

The reason for this is you are using 2 api's (GCM and FCM in one project.)

Don't use GCM because it is only the older version of FCM. Therfore, use only FCM. The c2dm function you using here is not now in the FCM verison. It is old.

But you may try the following manifest code: Just Copy and Paste

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.sixmod.com.client">
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <!--GCM amitranjan@sixmod5 id-->
   <!-- AIzaSyBo-v2RUKKoe37pZwYeurtoDFw1egxy7fM-->
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission
        android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
    <uses-permission android:name="android.permission.GET_TASKS"/>
    <uses-permission android:name="android.permission.READ_CONTACTS" />



    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />



    <application
        android:allowBackup="true"
        android:icon="@mipmap/truck"
        android:label="@string/app_name"
        android:name="com.sixmod.com.sixmod.MyLocalytic.MyApplication"

        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="android:icon">



        <service android:name="com.sixmod.com.sixmod.MyReceiver.BroadcastService" />



    <activity

            android:name="com.sixmod.FragmentContainerActivity"
            android:windowSoftInputMode="adjustPan"
        android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <!--<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyCsguzmCQrHlTaBQ_6ixFyQUk" />-->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBo-37pZwYeurtoDFw1egxy7fM " />


        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.bugsnag.android.API_KEY"
            android:value="221c5b778e92fdd345fa63e09"/>


        <meta-data android:name="LOCALYTICS_APP_KEY" android:value="7f652c5db10aa2dbd542-d159-11e5-6885-002dea3c3994"/>        <receiver
            android:name="com.sixmod.com.sixmod.MyLocalytic.LocalyticCustomReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
                <action android:name="com.google.android.c2dm.intent.RECEIVE"/>

                <category android:name="com.sixmod.com.sixmod.MyLocalytic"/>
            </intent-filter>
        </receiver>

        <service
            android:name="com.sixmod.com.sixmod.service.MyOrderService"
            android:exported="false">
        </service>

        <!--<meta-data
            android:name="LOCALYTICS_APP_KEY"
            android:value="YOUR-LOCALYTICS-APP-KEY"/>-->
       <!-- <receiver
            android:name="com.localytics.android.ReferralReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
        </receiver>-->

        <intent-filter>
            <data android:scheme="7f652c5db10aa2dbd5b87f7-80b0ac42-d159-11e5-6885-002dea3c3994" />
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
        </intent-filter>

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

        <service
            android:name="com.sixmod.com.sixmod.notificationFireBase.MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>


    </application>

</manifest>

Old Answer:

Do not use :

> getActivity().startService(new
> Intent(getActivity(),RegistrationIntentService.class));

This is automatically registered from the Manifest file. You don't need the RegistrationIntentService.java . remove It

1) First thing is use: *(For this update the Google Repository and Google Play Service from the Android SDK Manager.)*

compile 'com.google.firebase:firebase-messaging:9.2.0'

2) Uncomment it:

sendRegistrationToServer(refreshedToken);

3) You don't need the RegistrationIntentService.java . Remove this. It can cause your registration token not to be received.

Saini
  • 686
  • 3
  • 8