Reputation: 3271
I am trying to generate firebase registration token with new method but unable to generate this is my code below:
MyFirebaseInstanceIdService.java
public class MyFirebaseInstanceIdService extends FirebaseMessagingService {
@Override
public void onNewToken(String s) {
super.onNewToken(s);
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String token = instanceIdResult.getToken();
Log.d("Token",token);
}
});
}
}
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
package="com.app.retrofitapp">
<dist:module dist:instant="true" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Users"></activity>
<activity android:name=".MainActivity">
<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.MESSAGING_EVENT"></action>
</intent-filter>
</service>
</application>
There is no token is showing in log cat with above method.Someone please let me know what I am doing wrong.Any help would be appreciated.
THANKS
Upvotes: 2
Views: 3322
Reputation: 770
If you want just fcm token(not bother about notification) you only need below service in manifest with action INSTANCE_ID_EVENT...
<service android:name=".fcm.FireBaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
And after this add the below code in your activity (onCreate will be good)
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "Failed", task.getException());
return;
}
String token = task.getResult().getToken();
Log.i("FCM", "Current token=" + token);
}
});
You will get your current token there. Hope it will help.
Upvotes: 1
Reputation: 770
You can directly Log the token into your onNewToken method of FireBaseMessagingService class...
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.i("FCM", "FCMMessagingService token=" + s);
//store it in prefrence
ApplicationPreferences.setFcmToken(getApplicationContext(), s);
}
No need to call...(this method is useful when you want token in other class/activity)
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String token = instanceIdResult.getToken();
Log.d("Token",token);
}
});
Confirm two services in manifest file
<service android:name=".fcm.FireBaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".fcm.FireBaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
Upvotes: 1
Reputation: 1
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
Note - here you get the firebase token everytime. and you can save firebase token into your app or with your server.
}
Upvotes: 0
Reputation: 2321
Add below line in your Application class or in launch activity.
Without initialising your firebase instance token will never get generated.
FirebaseApp.initializeApp(getApplicationContext());
Upvotes: 0
Reputation: 546
In your case this is giving the token, try debugging on the real device.
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
}
Upvotes: 0