2

I'm making an app which can write to firebase database on button click. The app is working on emulator but it's not working on my physical device. The app was working fine last week but today started giving error. Here's the main activity code:

public class MainActivity extends AppCompatActivity {
    private Firebase fled1;
    Button on;
    Button off;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        on=  findViewById(R.id.on);
        off=  findViewById(R.id.off);

        on.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            DatabaseReference fled1 = FirebaseDatabase.getInstance().getReferenceFromUrl("https://testproject-tb.firebaseio.com/");
            DatabaseReference fled1Child = fled1.child("Button1");     
            fled1Child.setValue("1");
            }
        });

        off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            DatabaseReference fled1 = FirebaseDatabase.getInstance().getReferenceFromUrl("https://testproject-tb.firebaseio.com/");
            DatabaseReference fled1Child = fled1.child("Button1"); 
            fled1Child.setValue("0");
            }
        });
    }

Here's my manifest file I have added all the permissions required:

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lalitahuja.iotbutton">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name="com.example.lalitahuja.iotbutton.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Here's the build Gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.lalitahuja.iotbutton"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.firebase:firebase-core:16.0.8'
    implementation 'com.google.firebase:firebase-database:16.1.0'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.7'
    implementation 'com.google.firebase:firebase-auth:16.2.0'
    implementation 'com.firebase:firebase-client-android:2.5.2'
}
apply plugin: 'com.google.gms.google-services'

The error I'm getting:

E/FirebaseInstanceId: Token retrieval failed: SERVICE_NOT_AVAILABLE

Edit: I tried the solutions existing on internet but none of them is working.

lalitahuja
  • 105
  • 1
  • 4
  • 13
  • what is your physical device model? is it logged into google play services? – Karan Harsh Wardhan Mar 29 '19 at 12:32
  • I have 3 different Xiomi devices Mi Redmi 3s, Mi A1, Mi Note 4 all of the devices are logged in google play service with different accounts. And the app was working fine on all the devices last week but today I'm getting the same error on all of the devices. – lalitahuja Mar 29 '19 at 12:34
  • uh oh i JUST saw another question by another user about this kinda issue. sounds like a firebase service outage – Karan Harsh Wardhan Mar 29 '19 at 12:39
  • But there is no mention of the outage of Realtime Database on the Firebase status page you can check on this link https://status.firebase.google.com/ – lalitahuja Mar 29 '19 at 12:46
  • well then what else could be the issue - did you delete your authentication keys by mistake? – Karan Harsh Wardhan Mar 29 '19 at 12:53
  • I don't think so, the code is the same as it was a week ago. Plus if I have deleted the authentication key the code should not work on the emulator but it's working. – lalitahuja Mar 29 '19 at 12:57
  • Does this answer your question? [FirebaseInstanceId Token retrieval failed SERVICE\_NOT\_AVAILABLE](https://stackoverflow.com/questions/49949818/firebaseinstanceid-token-retrieval-failed-service-not-available) – David Buck Oct 30 '19 at 20:14

3 Answers3

6

Since I recently faced this issue, let me list out why it came and how I could solve it.

I had already an app added (call it app1) to a project. now, while doing the clean up for the app, I did not want to delete this app, so I added another app with different package in the same project (lets call it newAPP.)

The problem happened since I copied the old google-services.json file (for app1) to the new android app. That was a mistake.

Once I added newAPP in firebase console and downloaded the google-services.json file in the newAPP codebase, still I kept getting this error.

I was getting this error, since the build file (which are automatically generated by studio)was still having the old app-id and client-id etc.

So to fix it , I had to do following:

  1. clean project (Under Build tab)
  2. rebuild project(Under Build tab)
  3. recreate emulator (using AVD)

Be sure to match the app-id in 'app\build\generated\res\google-services\debug\values\values.xml' (generated by studio build ) to the one corresponding to your app id on the firebase console. Goodluck!

urOutsourced
  • 1,029
  • 10
  • 14
3

I had this issue once. Few things you can try.

  1. Try restarting your device

  2. check if google play service and play store are enabled.

  3. some times google play service or play store are restricted over Cellular Data. To solve it, I did the following: App Data Usage -> Google Services -> Turn Off the "Restrict Cellular Data Access" option.

GodSpeed
  • 58
  • 3
1

Remove your existing application and then again run this application in your android device