Nativescript-plugin-firebase: Push fails to write from Android device

Created on 16 Jun 2016  路  51Comments  路  Source: EddyVerbruggen/nativescript-plugin-firebase

This is my service that consumes the nativescript-plugin-firebase API (that's what's on the other side of the FIREBASE token), and the push inside addChild resolves, but no data is written in firebase.

import {Injectable, Inject} from '@angular/core';
import {FIREBASE} from '../../../app/frameworks/core.framework/index'

@Injectable()
export class NSDatabaseService {
  private database:any;
  private onSync:Function;
  private userID:string;
  constructor(@Inject(FIREBASE) firebase:any) {
    console.log('Constructing NSDatabaseService');
    this.database = firebase;
    this.database.init({
      persist: true // Allow disk persistence. Default false.
    }).then((instance:any) => {
      console.log('firebase.init successful');
    }, (error:any) => {
      console.log('firebase.init error: ' + error);
    });
  }

  sync(path: string, onValueReceived: Function):void {
    this.onSync = (result:any) => onValueReceived(result.value);
    this.database.addValueEventListener(this.onSync, path);
  }

  addChild(path:string, data:any, callback?:Function):void {
    this.database.push(path, data).then(function (result:any) {
      console.log('created key: ' + result.key);
      if (callback) {
        callback(result.key)
      }
    });
  }
}

Note that it works on the iOS emulator.

All 51 comments

I just deployed my proof of concept from a few weeks ago to my phone again, and that has no trouble writing to firebase.

I believe the only difference in my setup between then and now was that I had to add multiDex to the build.grade in the most recent project.

Can add a bit of loggin to the push function of the firebase plugin and see if it's invoking the correct methods at all? I have no idea what's up though, can't reproduce this issue on my sample app.

@EddyVerbruggen I added some basic logging to push in firebase.android.js, and the method is getting called with the correct parameters, and the promise is definitely resolving, but still no data in firebase. How else can I help debug this?

I'll give it a shot with the sample app linked from the readme and see if that gives me any trouble.

Reproduced trying the demo for this repo against the same firebase I've been using. Same behavior, the console says a key was created but no new data appears.

I'm refactoring things as we speak so I'll definitely check it out. Thanks for your efforts.

Awesome, thanks @EddyVerbruggen

Collected a little more data鈥揑 tried the demo against another Firebase instance as a sanity check, same behavior. The only way I'm deviating from the setup in the readme is setting the applicationId in platforms/android/build.gradle instead of the recommended app/App_Resources/Android/app.gradle. I'm doing this because I don't see app/App_Resources/Android/app.gradle in either the advanced seed that I'm using to prototype, or the demo from this repo.

I tried creating a new app/App_Resources/Android/app.gradle and setting just:

android {
    defaultConfig {
        applicationId "com.entercastle.ng2Test"
    }
}

but still no luck.

Just tried the demo app again - the 'store by push' button results in:
screen shot 2016-06-17 at 21 01 46

Are your rules the same as mine (evenybody can read/write), or do you authenticate the user before writing?
screen shot 2016-06-17 at 21 03 29

Let me see if moving the applicationId to that file makes a difference for me...

@EddyVerbruggen indeed, free-for-all on the permissions:
screen shot 2016-06-17 at 3 07 59 pm

Note: I updated those strings to booleans, still no luck

It's a bit weird that you don't have app/App_Resources/Android/app.gradle as that comes standard when starting a new NativeScript app with the CLI. I'm running 2.x, you too?

Are you 100% sure your Android device can reach your DB? WiFi connection OK?

Running 2.0.1:
screen shot 2016-06-17 at 3 12 56 pm

As for "Are you 100% sure your Android device can reach your DB? WiFi connection OK?", the init succeeds, and my device has no trouble accessing the internet through other channels.

OK, asked because if you disable WiFi you can still store data if persist: true is passed in init - and it will sync with the server once you have a conncetion.

Hmm, I'll flip that bit and see if that affects anything. I do have it set to persist currently

I guess if your Android browser can reach the FB URL you should be fine.

No luck with setting persist to false.

Puzzled.. the weird this is, the demo app you just tried is using an older version of the plugin (with the Firebase 2.x SDK). Can you just keep it connected to the demo Firebase db and see if that makes a difference? Just clone it without modifications..

More data, just tried my prototype of the advanced-seed in a genymotion emulator, same behavior.

I'm confused what you mean by just clone it without modifications? Don't I need to add the google-services.json and do the rest of the setup for it to work?

You said "Reproduced trying the demo for this repo against the same firebase I've been using. Same behavior, the console says a key was created but no new data appears." so I guess you cloned this repo and followed the instructions in the readme. Since that's using Firebase 2.x you don't need to add google-services.json etc because that's a Firebase 3.x requirement.

Can I clone you repo and run it on my Android device?

Well most of the config is in files that aren't tracked by version control (stuff in platforms/android), so I can't easily pass my project. However, I wrote up pretty detailed instructions on my setup here.

I cloned the demo repo and ran without any config/setup. The push appears to be working from the console, but I don't have access to the firebase admin, so I can't tell if it's actually writing.

Can you run 'store by push' in the demo again and report back the key that's echo'd in the console?

Just did a few:

JS: firebase.push done, created key: -KKUsE-bfHox3yL4Iyqe
JS: firebase.push done, created key: -KKUsFPUVzVGVlTfhVfr
JS: firebase.push done, created key: -KKUsFwVYrXgIjxnwFer
JS: firebase.push done, created key: -KKUsu5BumnQ6CfeqICS
JS: firebase.push done, created key: -KKUsuDm6ILN9NIxR95O

Crickets..
screen shot 2016-06-17 at 21 43 54

This is so weird. So the old and new version don't work for you.

Firewall issues? Does it matter if your try 3g/4g vs WiFi? 馃憥

Disconnected from wifi and sent a few more:

JS: firebase.push done, created key: -KKUto1cK-VZzejdn0eW
JS: firebase.push done, created key: -KKUtoRME_k8ZTEgOIZM
JS: firebase.push done, created key: -KKUtoZMDYCoouEjBEH-

I really don't think it's a generic network thing because it works fine from web & an iOS emulator.

Oh shoot, that's not the correct firebase instance sorry. You're connecting to https://resplendent-fire-4211.firebaseio.com but it should be https://n-plugin-test.firebaseio.com. Please change that here and run again.

That old instance turned up in some kind of limbo state after migrating my account to Firebase 3.

Updated the ref:

JS: firebase.push done, created key: -KKUvHpBbWGIlNBR35Ya
JS: firebase.push done, created key: -KKUvIDchsV3uY1W87eP
JS: firebase.push done, created key: -KKUvIJbq2UxCysFBNuq

Cool, so the Firebase 2.x plugin/demo push correctly:

screen shot 2016-06-17 at 21 54 26

Can you now connect your app to this Firebase instance? So https://n-plugin-test.firebaseio.com?

I'm happy to try, but the setup is different for 2.x and 3.x and my app is using the latest version of this plugin. Is there a specific version you want me to hop back to and try?

I'd say just use the latest with what you already have (your app, not the demo) and connect your app to my Firebase instance. The instances are backward compatible and I can clean it up afterwards with one click.

"connect your app to my Firebase instance" => Please excuse my ignorance here, but I only know how to do that with the google-services.json file

Is it as simple as adding the url to the init call?

That's hardly ignorant :)

Let me update the demo app first so you can simply clone that.

Thanks for all your help!

More data: I have one instance on my machine of the angular2-advanced-seed from a few weeks ago where this plugin was working fine, iOS & Android. I was looking at the difference in npm dependencies across the projects and noticed this plugin differed by a minor update (3.0.0 in my project that's working, 3.1.0 in my latest). Unfortunately, I got the same behavior when explicitly using 3.0.0 in my latest: the push promise still resolves, but no data is stored. :[

Can you make sure your Google Play services stuff is up to date by running android in a terminal and updating those bits?

I installed the versions in the SO post linked from the readme. Currently on Google Play Services (rev 30) and Google Repository (rev 26). Currently updating Google Repository to rev 28

In other news: I've just pushed 3.1.0 to npm and update the demo app so please clone it, follow the steps in the README of that repo (!) and don't change google-services.json so you connect to my Firebase instance.

(off to bed)

Installed and configured with your google-services.json, then tried a few pushes:

JS: firebase.push done, created key: -KKVL21TlTVNZvRI43zO
JS: firebase.push done, created key: -KKVL2Zp7Lt_ZEY61r5P
JS: firebase.push done, created key: -KKVL2llq6c3nrg49xHe

Yes! Those 3 at the bottom arrived at y Firebase instance:

screen shot 2016-06-18 at 10 08 02

So what happens if you now add your google-services.json to the demo app? Make sure the applicationId is the same as the one in this file, etc.

Amazing, that's working for me. I'll keep you posted on whether or not 3.1.0 resolves the issue in my prototype. Thanks again for all your help so far. 馃憤

Sorry just to chime in here. Could the issue not have something to do with the fact that new projects created in the new firebase console are not backward compatible with the javascript firebase 2.x.x SDK?

IE Old projects there were migrated would work with 2.x.x and 3.x.x firebase SDK but new projects are only able to use 3.x.x.

I ran into a similar experience with web @matdehaast. I couldn't authenticate from the 2.X SDK to a new firebase instance created from the google console, but their customer support verified the claim in the migration guide that the 2.X SDK should still work if you migrate from firebase.com. I'm yet to verify their claim.

@EddyVerbruggen so the demo is working when I swap your google-services.json for mine, but when I start a fresh angular2-advanced-seed and follow these instructions, I'm still getting the same behavior from before: the call to push resolves, but no data is stored in firebase:

// libs
import {Store} from '@ngrx/store';

// app
import {FormComponent} from '../../frameworks/core.framework/index';
import {NameListService} from '../../frameworks/app.framework/index';

var firebase = require('nativescript-plugin-firebase');

@FormComponent({
  moduleId: module.id,
  selector: 'sd-home',
  templateUrl: 'home.component.html',
  styleUrls: ['home.component.css']
})
export class HomeComponent {
  public newName: string = '';
  constructor(private store: Store<any>, public nameListService: NameListService) { 
    firebase.init({
      persist: true // Allow disk persistence. Default false.
    }).then((instance:any) => {
      console.log('firebase.init successful'); //succeeds
      firebase.push('/counters', 100).then((result:any) => {
        console.log('pushed', result.key); //logs a key
      }, (err:any) => console.log(err));
    }, (error:any) => {
      console.log('firebase.init error: ' + error);
    });
  }

  /*
   * @param newname  any text as input.
   * @returns return false to prevent default form submit behavior to refresh the page.
   */
  addName(): boolean {
    this.nameListService.add(this.newName);
    this.newName = '';
    return false;
  }
}

@lowe0292 Is it incorrect for me to say that if the plugin works in the demo app and not in some other app the problem is not with the plugin but with the other app?

That sounds like solid logic to me.

@lowe0292 did you ever find a solution here? Reading through the comments, very curious indeed.

@NathanWalker nope, never solved it with your seed. In fact, this was the Achilles heel that killed our NativeScript project. 馃槩

@lowe0292 sry to hear, completely understand though. Lot of interesting details above.
If you have a branch or the project laying around, I'd love to fire it up and try to debug it myself!
Lemme know 馃憤 And we could put this mystery to rest.

I also had the same problem. The Solution was Just to change the firebase realtime database settings ( read and write) to true. Being public, android could now read and write to the database.
image

Was this page helpful?
0 / 5 - 0 ratings