Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not getting refreshed token firebase android

I have trouble in getting refreshed token in firebase. I have gone through documentation and followed the steps fro android exactly. In my log I find firebase connection as successful. Not sure why I am not getting the instance token. I am in initial stage and trying to get the token in logcat.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.project.application"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <application
        android:allowBackup="false"
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:name=".ProjectName"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
            android:icon="@drawable/icon"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Light.NoTitleBar"> 

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />
        </activity>

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

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

</manifest>

    public class MyInstanceIDListenerService  extends FirebaseInstanceIdService{    
    private static final String TAG = "MyFirebaseIIDService";
    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

        Log.d(TAG, "Refreshed token: " + refreshedToken);
        sendRegistrationToServer(refreshedToken);

    }

 public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO(developer): Handle FCM messages here.

    Log.d(TAG, "From: " + remoteMessage.getFrom());
    String messageBody = null;

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }

    if (remoteMessage.getNotification() != null) {
        messageBody = remoteMessage.getNotification().getBody();
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    sendNotification(messageBody);

}
private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, SplashActivity.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());
}

}

like image 691
jenny Avatar asked Feb 16 '17 14:02

jenny


People also ask

How do I get the Firebase refresh token?

You can refresh a Firebase ID token by issuing an HTTP POST request to the securetoken.googleapis.com endpoint. The refresh token's grant type, always "refresh_token". A Firebase Auth refresh token. The number of seconds in which the ID token expires.

How long does FCM token last?

Ensuring registration token freshness Any token older than two months is likely to be an inactive device; an active device would have otherwise refreshed its token.

Does FCM token change on app update?

yes it does not change token upon updating.


2 Answers

The onTokenRefresh() method doesn't trigger the first time an app Is installed. It is only triggered by specific scenarios.

To get your token, you have to call FirebaseInstanceId.getInstance().getToken() at the start of your app (like in onCreate or something).

like image 75
AL. Avatar answered Oct 16 '22 01:10

AL.


From the github project of FCM here

The documentation onTokenRefresh() reads :

  • 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.

You have to Clear Cache of you app or reinstall it in order to call this method.

like image 39
M.Waqas Pervez Avatar answered Oct 16 '22 00:10

M.Waqas Pervez