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