Skip to content

Instantly share code, notes, and snippets.

@jbristowe
Last active June 29, 2017 21:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jbristowe/c89a7bcae7fc9a035ee7 to your computer and use it in GitHub Desktop.
Save jbristowe/c89a7bcae7fc9a035ee7 to your computer and use it in GitHub Desktop.

A Very Rough Guide to Firebase and NativeScript

Create Plugin

  • mkdir nativescript-firebase
  • cd nativescript-firebase
  • npm init
  • tns init
  • mkdir -p platforms/ios
  • touch platforms/ios/Podfile
  • In platforms/ios/Podfile: pod 'Firebase'
  • mkdir -p platforms/android/libs
  • Copy the Firebase SDK JAR to platforms/android/libs
  • (Optional) touch platforms/android/AndroidManifest.xml
  • (Optional) In platforms/android/AndroidManifest.xml:
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  <uses-permission android:name="android.permission.INTERNET" />
</manifest>

Import Plugin

  • In your NativeScript project: tns plugin add <location of nativescript-firebase>

Use Plugin on Android

var appModule = require("application");

// ...

var Firebase = com.firebase.client.Firebase;
var ChildEventListener = com.firebase.client.ChildEventListener;

Firebase.setAndroidContext(appModule.android.context);
ref = new Firebase(<Firebase URL>);

ref.addChildEventListener(new ChildEventListener({
	onChildRemoved: function(snapshot) {
		// do your thing
	},
	onChildAdded: function(snapshot, previousChildKey) {
		// do your thing
	},
	onChildChanged: function(snapshot, string) {
		// do your thing
	},
	onChildMoved: function(snapshot, string) {
		// do your thing
	}
}));

To add new items:

var data = new java.util.HashMap();
data.put(...);
ref.push().setValue(data);

To delete items:

ref.child(id).setValue(null);

And so on.

Use Plugin on iOS

var ref = new Firebase(<Firebase URL>);

ref.observeEventTypeWithBlock(FEventType.FEventTypeChildAdded, function (snapshot) {
	// do your thing
});

ref.observeEventTypeWithBlock(FEventType.FEventTypeChildRemoved, function (snapshot) {
	// do your thing
});

ref.observeEventTypeWithBlock(FEventType.FEventTypeChildChanged, function (snapshot) {
	// do your thing
});

To add new items:

ref.childByAutoId().setValue(...);

To delete items:

ref.childByAppendingPath(id).setValue(null);

And so on.

@lyonzy
Copy link

lyonzy commented Nov 27, 2015

Great minds think alike! I just created the nativescript-firebase plugin on npm based on the Firebase node.js SDK :)
Hey @EddyVerbruggen, just checking out your source - if you want a simpler way to turn Android objects back into JSON I used this slightly hacky but fairly concise trick back when I was using the Firebase Android SDK in {N}:

            var jobj = new org.json.JSONObject(javaObject);
            return JSON.parse(jobj.toString());

It works for objects and arrays but IIRC you'll need to check for strings/ints/etc and return them directly.
I never got around to doing it the other way around but presumably it's a similar concept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment