1

I am trying to build an app, but I face a problem when I want to generate TOKEN with Firebase, because in the past, I used an old method provided by Google but with the new method I can't generate the token and share.

The previous method that worked for me was to send the token, now deprecated is following:

import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    @Override
    public void onTokenRefresh() {
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);
        storeToken(refreshedToken);
    }

    private void storeToken(String token) {
        //saving the token on shared preferences
        SharedPreference.getInstance(getApplicationContext()).saveDeviceToken(token);
    }
}

And this is the new method, but it doesn't work. My token not is generated and cannot be shared:

import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseInstanceIDService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseIIDService";

    @Override
    public void onNewToken(String token) {
        super.onNewToken(token);
        Log.d("NEW_TOKEN",token );
        storeToken(token);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

    }

    private void storeToken(String token) {
        //saving the token on shared preferences
        SharedPreference.getInstance(getApplicationContext()).saveDeviceToken(token);
    }
}

My AndroidManifest file:

<application
        android:usesCleartextTraffic = "true"
        android:name=".app.AppController"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:fullBackupContent="@xml/backup_descriptor">
        <activity android:name=".ge_inicio"
            android:screenOrientation="portrait">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

Thanks for the help

cameron1024
  • 9,083
  • 2
  • 16
  • 36
Matteo
  • 19
  • 7

3 Answers3

2

Use only this code inside your AndroidManifest

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

and delete below method

<service android:name=".MyFirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
    </intent-filter>
</service>
  1. And Clear your app data or reinstall your app to get the new token

  2. If you want to get id in other activities, read this answer

majurageerthan
  • 2,169
  • 3
  • 17
  • 30
  • im sorry i cant build fragment code with these example, y try any ways, but i cant to make that work :( – Matteo Jan 23 '20 at 03:03
0

Why don't you try this way to generate token just put below code in your MainActivity.

FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(this, 
instanceIdResult -> {
String newToken = instanceIdResult.getToken();
SharedPreference.getInstance(getApplicationContext()).saveDeviceToken(token)
haresh
  • 1,424
  • 2
  • 12
  • 18
0

Firebase Id create once when app newly install or create id when app cache memory cleared. You need to uninstall and reinstall it before debug .

ashok
  • 431
  • 5
  • 8