dev90
dev90

Reputation: 7519

How to get Firebase Token on a certain Event

I have added Firebase Notification in my project, now when i launch the application it Hit the Firebase server and gets the Token. I want it to hold the request, and hit the firebase server on an event.

This is what i have done, I have not created any instance of Firebase, It gets called on its own.

<service android:name=".FirebaseCloudMessaging.FirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>


    <service android:name=".FirebaseCloudMessaging.FirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

FirebaseInstanceIDService Class

public class FirebaseInstanceIDService extends FirebaseInstanceIdService {

    private final String SHARD_PREF = "com.tixsee.mavs.gcmcloudmessaging";
    private final String GCM_TOKEN = "gcmtoken";
    private final String TAG = FirebaseInstanceIDService.class.getSimpleName();
    String token;
    private Context mContext;
    public FirebaseInstanceIDService() {

    }

 @Override
    public void onTokenRefresh() {

        SharedPreferences appPrefs = getSharedPreferences(SHARD_PREF, Context.MODE_PRIVATE);
        token = appPrefs.getString(GCM_TOKEN, "");
        if (token.isEmpty()) {

            token = FirebaseInstanceId.getInstance().getToken();
            sendTokenToService(token);
            Log.d(TAG, "New Token Created" + token);

            SharedPreferences notificationPreference = getSharedPreferences(SHARD_PREF, Context.MODE_PRIVATE);
            SharedPreferences.Editor notificationPrefsEditor = notificationPreference.edit();
            notificationPrefsEditor.putString("NOTIFICATION_ENABLED", "true");
            notificationPrefsEditor.apply();
}

Kindly guide me how to hold Firebase request, and trigger it on any certain event.

Upvotes: 0

Views: 644

Answers (1)

Sangeet Suresh
Sangeet Suresh

Reputation: 2545

Just store a flag for user accepts notification in Shared Preference

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("notification", true);
editor.commit();

Then check the flag in onTokenRefresh()

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
boolean value = sharedPref.getBoolean("notification", false);

if(value)
   {
     \\ Send to token(FirebaseInstanceId.getInstance().getToken()) to server 
  } 

If the flag is set then send token to server.

Also put if onTokenRefresh is called automatically before user accepts screen. Then check FirebaseInstanceId.getInstance().getToken(); is null or not.

if(FirebaseInstanceId.getInstance().getToken()!=null)
{
 \\ Send to token(FirebaseInstanceId.getInstance().getToken()) to server 
}

If its not null then send that token to server.

Hope this will solve your question.

Upvotes: 1

Related Questions