Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification Services

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>
like image 328
tarun14110 Avatar asked Jul 01 '18 13:07

tarun14110


1 Answers

Based on how I understood your question, I see more than 1 solution which are as follows:

  1. Initially you can save Token to shared preference, as mentioned by Nainal And later when the user successfully logs in and database created you can store to table.

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 
                }
});
  1. It'd be quite long but You can send own custom broadcast from onTokenRefresh() or onNewToken(), Set callback interface in to Broadcast receiver from activity and get that call in Activity by overriding callback method.

Note: the FirebaseInstanceIdService is deprecated instead we can override onNewToken() to get new/updated token in FirebaseMessagingService only.
you can check this link.

like image 114
Man Avatar answered Oct 13 '22 01:10

Man