26

I use FCM in my project

It work correct on Sony xperia, Galaxy S6, Motorola and more. But on Galaxy S3 i get java.io.IOException: SERVICE_NOT_AVAILABLE error

Time of Galaxy S3 is auto and google play is updated

Internet connection is strong and i connected to open internet without proxy

AL.
  • 36,815
  • 10
  • 142
  • 281
Te Me
  • 295
  • 1
  • 4
  • 9
  • 3
    You may refer with this [thread](https://stackoverflow.com/questions/18325099/service-not-available-some-devices-on-android-gcm). This [error](https://developers.google.com/android/reference/com/google/android/gms/gcm/GoogleCloudMessaging#ERROR_SERVICE_NOT_AVAILABLE) means that your device can't read the response or there was a 500/503 from the server. You should use exponential back off and retry. – abielita May 07 '18 at 14:11
  • @abielita My app is crashing with `Fatal Exception: com.google.android.gms.tasks.RuntimeExecutionException java.io.IOException: SERVICE_NOT_AVAILABLE` while try to retrieve the token from result. so my concern is how to prevent the app from crashing with this error? – Stella Aug 08 '18 at 05:48
  • 1
    before accessing the result check for if(task!=null && task.isSuccessful())){ task.getResult()} this check will avoid app from getting crashed – Vijayalaxmi Dec 11 '20 at 18:38

5 Answers5

21

This error is caused when the device is unable to register to Firebase. Make sure the internet is working when this code is called. And put the code within try-catch to stop app from crashing.

Edit: Either add an internet connection check before registering the device or fetching token. Or wrap the Fetch token code in try-catch block to prevent app from crashing.

Example:

  1. Adding the internet connection check code:

     private boolean isNetworkConnected() {
       ConnectivityManager cm = (ConnectivityManager) 
       getSystemService(Context.CONNECTIVITY_SERVICE);
    
       return cm.getActiveNetworkInfo() != null && 
       cm.getActiveNetworkInfo().isConnected();
    }
    
  2. Internet check and move ahead.

    if (isNetworkConnected())
       refreshToken(this);
    
Prajwal Waingankar
  • 2,534
  • 2
  • 13
  • 20
Insane Developer
  • 1,014
  • 10
  • 19
8

Make sure that you have included

task.isSuccessful()

check in your code inside overriden onComplete method like this -

FirebaseMessaging.getInstance().getToken() .addOnCompleteListener(new OnCompleteListener() {

        @Override
        public void onComplete(@NonNull Task<String> task) {
            if(task.isSuccessful()) {
                fcmToken = task.getResult();
            }
        }
    });

If not, whenever a network error or some other issue occurs at the time of registration of fcm token, then you may get FIS_Authentication_Failed or SERVICE_NOT_AVAILABLE type of errors.

Damanpreet Singh
  • 678
  • 10
  • 15
7

Check following:

1- Internet connection

2- Phone date/time to be correct

Hamed Jaliliani
  • 2,789
  • 24
  • 31
2

In my case problem was in Google Mobile Services app. After clearing its data problem disappeared.

I found a key for problem after launching on another device, where issue doesn't reproduces.

mohax
  • 4,435
  • 2
  • 38
  • 85
  • 1
    This resolved it for me after taking the following steps: - Ensure the SHA1 of my android app on play store has been added to the list of unrestricted apps. - Ensured the google_oauth_client for the play store credentials is both used in the code and on the google-services.json file. - Ensured the SHA1 of my android app on play store has been added to firebase. - Then I cleared the cache and storage of google play services app on my pixel 6. - Uninstalled and installed the app from playstore and VOILA, the app was able to retrieve token. – Jamie Jul 02 '23 at 13:27
1

If someone is still facing an error, please try adding the getToken and other firebase messaging calls in useEffect and add messaging as its dependency.

Sometimes, the app is trying to invoke these methods before the messaging library is getting initialized, so try wrapping your code in useEffect with messaging as dependency.

import messaging from '@react-native-firebase/messaging';

React.useEffect(() => {
  // Get the token
  const token = await messaging().getToken();
  // other messaging operations
}, [messaging])
user1875926
  • 121
  • 11