4

In my Android App, I use Firebase Notifications. As i know, If user doesn't have Google Play Services installed or properly configured, Can't receive notifications. I have two questions:

1- Is true what I say? If Google Play Services doesn't configured properly, user can't receive Firebase notifications?

2- I use this method to check if user have Google Play services or not, Then register user in Topics and so on... :

GoogleApiAvailability api = GoogleApiAvailability.getInstance();
int resultCode = api.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS) {
    //register in a topic
    FirebaseMessaging.getInstance().subscribeToTopic("all");
}else{
    alert("You have to install Google Play Services")
}

Is this method is right? My main goal is that if user can't register for Firebase messages, Get informed about the reason and without any error. In fact, Registering maximum users for receiving Firebase messages without any problem. I don't want my App crashes because of Google Play Services...

What is the best practice for these goals?

Fcoder
  • 9,066
  • 17
  • 63
  • 100

1 Answers1

4

This answer is based on my experience.

1- Is true what I say? If Google Play Services doesn't configured properly, user can't receive Firebase notifications?

Yes. User won't get any notification if user's phone doesn't have Google Play Services and also user won't get notification if installed Google Play Services is out of date.

2- I use this method to check if user have Google Play services or not, Then register user in Topics and so on... :

This approach seems good for me. You can show an alert for update google play service or install it. try below code

  GoogleApiAvailability api = GoogleApiAvailability.getInstance();
  int resultCode = api.isGooglePlayServicesAvailable(getApplicationContext());
  if (resultCode == ConnectionResult.SUCCESS) {
    //register in a topic
    FirebaseMessaging.getInstance().subscribeToTopic("all");
  }else{
       //Any random request code
      int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
      //Google Play Services app is not available or version is not up to date. Error the
      // error condition here
      if (api.isUserResolvableError(result)) {
        api.getErrorDialog(this, resultCode ,
            PLAY_SERVICES_RESOLUTION_REQUEST).show();
      }else{
         alert("You have to install Google Play Services")
      }
}

in your alert you can ask user to download Google Play Services from Playstore or browser. Most of the time the else part only trigger if device not going to support Google Play Services.So in this case better to disable FCM for this device.

                String appPackageName = "com.google.android.gms";
                try {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
                } catch (android.content.ActivityNotFoundException anfe) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
                }
Vinayak B
  • 4,430
  • 4
  • 28
  • 58
  • This is also answered in https://stackoverflow.com/questions/45536676/handling-cancellation-of-makegoogleplayservicesavailable/67823816#67823816 – arango_86 Jun 03 '21 at 15:09