Android: FCM java.io.IOException: SERVICE_NOT_AVAILABLE error on some devices
Asked Answered
C

8

27

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

Cab answered 7/5, 2018 at 6:42 Comment(3)
You may refer with this thread. This error 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.Gatias
@Gatias 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?Concert
before accessing the result check for if(task!=null && task.isSuccessful())){ task.getResult()} this check will avoid app from getting crashedSloat
Z
23

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);
    
Zoril answered 25/12, 2018 at 3:14 Comment(3)
kindly provide a sample try-catch here... thank youForgive
Also, in my case, the problem might be, when phone connected to Charles proxy.Silda
I turned on the internet and it works. thanks a lot!Saveall
N
10

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.

Neilneila answered 22/4, 2021 at 5:34 Comment(0)
L
8

Check following:

1- Internet connection

2- Phone date/time to be correct

Lashelllasher answered 10/11, 2019 at 9:23 Comment(2)
2- Phone date/time to be correct Why, it can cause this problem?Ddt
Date and time was the issue, cheers.Nanji
G
3

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.

Gennie answered 20/7, 2021 at 15:24 Comment(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.Fredrickafredrickson
G
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])
Grigri answered 14/6, 2023 at 4:46 Comment(0)
P
0

Try removing this code from void main() and putting it somewhere else; e.g., after the user is successfully logged in, or inside the init function on the first page:

await Firebase.initializeApp();
await _firebaseMessaging.requestPermission();
final fCMToken = await _firebaseMessaging.getToken();
Pansy answered 23/2, 2024 at 19:11 Comment(0)
D
0

If you are using a proxy to monitor network, Firebase will not share its token. It's a security feature by Google.

Demented answered 15/7, 2024 at 14:37 Comment(0)
C
0

Checking... your mobile Internet connection *

  • Often this error can occur when the net is off
Cryogen answered 3/8, 2024 at 4:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.