Reputation: 990
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>
Upvotes: 7
Views: 551
Reputation: 3527
NOTE: Solution is based on FirebaseInstanceIdService
and FirebaseMessagingService classes. But it will work with the newer update to Firebase Android SDK
Here is the scenario to solve your problem:
In your callback of token refresh first check if the FirebaseUser
authentication is present. If present simply write the token to your
Firebase database. Also store a boolean to Shared Preferences to
true if token saved in db else set it to false. This solves the
issue that you don't have database access but you are getting token.
Next create a Async task in separate class which simply call
FirebaseInstanceId.getInstance().getToken();
to trigger the
onTokenRefresh
in service.
Next in your login process after firebase auth success call the
above async task which will force the onTokenRefresh
callback. Now
since user auth is present your write to db will be successful (also
setting the shared pref token_sent_to_db = true).
Now in you launching activity where you are checking user auth for navigation to appropriate activity add a check if the shared pref token_sent_to_db = false; call the Async Task again. This will ensure that if some how above steps miss the token save in db we can send the token again.
Lastly the token for FCM will not change until you force it to change or if the apps gets un-installed. So the token will only get written to db once only.
P.S: Also make sure that after user logout you expire the FCM token otherwise your server will keep sending the notification to the logged out user. FirebaseInstanceId.getInstance().deleteInstanceId();
Upvotes: 0
Reputation: 2817
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.
Upvotes: 5
Reputation: 54
put this code in your launcher/main activity:
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d("", "Refreshed token: " + refreshedToken);
this will get you the token, than go to this below web -
https://console.firebase.google.com/project/_/notification
and create new notification->choose single device->paste
the token that was logged in your android studio project->check if you got the notification
Upvotes: 3