I am trying to receive Android notifications using firebase. I have defined the below services. But the service MyFirebaseInstanceIDService
is running on running the app for the first time. I don't even have a database for the user at that time. I want to store the firebase token for the user, but first I want to make an entry for the user on first activity and start the services on the second activity. SO that I can store the token of the user.
Android Manifest file
<service
android:name="in.anamika.anamika.notifications.MyFirebaseMessagingService"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service android:name="in.anamika.anamika.notifications.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
Based on how I understood your question, I see more than 1 solution which are as follows:
The updated API returns Task on which you can add onSuccess listener and that will be called when you can successfully get token.
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(yourActivity,
new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String deviceToken = instanceIdResult.getToken();
// Do whatever you want with your token now
// i.e. store it on SharedPreferences or DB
// or directly send it to server
}
});
Note: the FirebaseInstanceIdService is deprecated instead we can override onNewToken() to get new/updated token in FirebaseMessagingService only.
you can check this link.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With