NativeScript Firebase plugin

Firebase
Google's realtime app platform (Database, Authentication, Configuration, Notifications) firebase.google.com

Requires NativeScript 2.3.0 or up. So npm install -g nativescript like a boss if you need to!

Features

For readability the supported features have been moved to their own README's:

Prerequisites

Head on over to https://console.firebase.google.com/ and sign up for a free account. Your first 'Firebase' will be automatically created and made available via an URL like https://n-plugin-test.firebaseio.com.

Open your Firebase project at the Google console and click 'Add app' to add an iOS and / or Android app. Follow the steps (make sure the bundle id is the same as your nativescript.id in package.json and you'll be able to download:

  • iOS: GoogleService-Info.plist which you'll add to your NativeScript project at app/App_Resources/iOS/GoogleService-Info.plist

  • Android: google-services.json which you'll add to your NativeScript project at platforms/android/google-services.json

Installation

If you rather watch a video explaining the steps then check out this step-by-step guide - you'll also learn how to add iOS and Android support to the Firebase console and how to integrate anonymous authentication: YouTube demo

From the command prompt go to your app's root folder and execute:

tns plugin add nativescript-plugin-firebase

This will guide you through installing additional components. Check the doc links above to see what's what. You can always change your choices later.

iOS

Looking to automate this step, but for now, to build with Xcode 8 (iOS 10 SDK), you need to open the Xcode project via the platforms/ios/appname.xcworkspace/ file - then select your app's target, capabilities, enable 'Keychain sharing'.

Android

Install packages 'Google Play Services' and 'Google Repository' in your Android SDK Manager

Open platforms/android/build.gradle

We're trying to automate these steps, but for now:

  • Near the top there's a dependencies section, add classpath "com.google.gms:google-services:3.0.0" so it becomes something like:

    dependencies {
    classpath "com.android.tools.build:gradle:X.X.X"
    classpath "com.google.gms:google-services:3.0.0"
    }
  • Add the very bottom of the same file add

    apply plugin: "com.google.gms.google-services"

Usage

If you want a quickstart, clone our demo app.

Start-up wiring

We need to do some wiring when your app starts, so open app.js and add this before application.start();:

JavaScript
var firebase = require("nativescript-plugin-firebase");

firebase.init({
  // Optionally pass in properties for database, authentication and cloud messaging,
  // see their respective docs.
}).then(
    function (instance) {
      console.log("firebase.init done");
    },
    function (error) {
      console.log("firebase.init error: " + error);
    }
);

TypeScript

import firebase = require("nativescript-plugin-firebase");

firebase.init({
  // Optionally pass in properties for database, authentication and cloud messaging,
  // see their respective docs.
}).then(
  (instance) => {
    console.log("firebase.init done");
  },
  (error) => {
    console.log("firebase.init error: " + error);
  }
);

Known issues on iOS

Authentication failed: invalid_token

On the simulator you may see this message if you have more than one app with the Firebase SDK ever installed:

[FirebaseDatabase] Authentication failed: invalid_token (Invalid claim 'aud' in auth token.)
or
[FirebaseDatabase] Authentication failed: invalid_token (audience was project 'firegroceries-904d0' but should have been project 'your-firebase-project')

This is a known issue in the Firebase SDK. I always use a real device to avoid this problem, but you can pass an 'iOSEmulatorFlush' option to init.

firebase.init({
  // Optionally pass in properties for database, authentication and cloud messaging,
  // see their respective docs and 'iOSEmulatorFlush' to flush token before init.
  iOSEmulatorFlush: true
}).then()

Pod dependency error

If you see an error like Unable to satisfy the following requirements: Firebase (~> 3.3.0) required by Podfile, then check issue 98 which perfectly explains how to update your local Pod spec repo, or first try to tns platform remove ios && tns platform add ios.

Known issues on Android

Genymotion

You can use the awesome Genymotion emulator but you'll need to install Google Play Services on it or you'll run into errors during authentication.

DexIndexOverflowException

com.android.dex.DexIndexOverflowException: method ID not in..

Congrats, you ran into this issue which can be solved by adding multiDexEnabled true to your app/App_Resources/Android/app.gradle so it becomes something like this:

android {  
  defaultConfig {  
    applicationId = "__PACKAGE__"  
    multiDexEnabled true
    generatedDensities = []
  }  
  aaptOptions {  
    additionalParameters "--no-version-vectors"  
  }  
}

java.lang.OutOfMemoryError: GC overhead limit exceeded

Increase the Java Max Heap Size like this (the bit at the end):

android {  
  defaultConfig {  
    applicationId = "__PACKAGE__"  
    multiDexEnabled true
    generatedDensities = []
  }
  aaptOptions {  
    additionalParameters "--no-version-vectors"  
  }
  dexOptions {
    javaMaxHeapSize "4g"
  }
}

FirebaseApp with name [DEFAULT] doesn't exist

Another possible error is "FirebaseApp with name [DEFAULT] doesn't exist." which will be solved by placing google-services.json to platforms/android/google-services.json (see above), and making the changes to build.gradle which are mentioned above as well.

Could not find com.google...

And there's this one: "Could not find com.google.firebase:firebase-auth:9.4.0". That means making sure you have the latest Google Repository bits installed. Just run android from a command prompt and install any pending updates.

Also, an error like "Could not find com.google.firebase:firebase-core:9.0.0" can be caused by having more than one version of the Android SDK installed. Make sure ANDROID_HOME is set to the Android SDK directory that is being updated otherwise it will seem as though your updates have no effect.

Found play-services:9.0.0, but version 9.0.2 is needed..

Update your Android bits like the issue above and reinstall the android platform in your project.

Credits

The starting point for this plugin was this great Gist by John Bristowe.

Related Repositories

awesome-angular

awesome-angular

:page_facing_up: A curated list of awesome Angular 2 and Angular 4 resources by ...

awesome-angular

awesome-angular

:page_facing_up: A curated list of awesome Angular 2 and Angular 4 resources by ...

awesome-angularjs

awesome-angularjs

:cyclone: A list of awesome AngularJs services, directives, utilities and resour ...

awesome-nativescript

awesome-nativescript

A curated list of awesome articles, plugins, and resources for NativeScript. ...

ocaml-cordova-plugin-list

ocaml-cordova-plugin-list

List of bindings in OCaml to cordova plugins. ...


Top Contributors

EddyVerbruggen t-moe devonite sean-perkins guillaume-roy Lampei marcbuils willhlaw colinskow bpmerkel devtrench mikeres0 NathanWalker stevetayloruk wdulin blagoev jamestamplin samuelagm

Dependencies

package version
fs-extra ~2.1.0
fs-promise ~2.0.0
nativescript-hook ~0.2.0
prompt-lite ~0.1.0
xcode ~0.9.0
dev prompt-lite ~0.1.0
xcode ~0.9.0
webpack ~2.2.0

Releases

-   3.7.1 zip tar
-   3.7.0 zip tar
-   3.6.4 zip tar
-   3.6.3 zip tar
-   3.6.2 zip tar
-   3.6.1 zip tar
-   3.5.4 zip tar
-   3.5.3 zip tar
-   3.5.1 zip tar
-   3.5.0 zip tar
-   3.4.4 zip tar
-   3.4.3 zip tar
-   3.4.2 zip tar
-   3.4.0 zip tar
-   3.3.0 zip tar
-   3.2.0 zip tar
-   3.1.0 zip tar
-   3.0.1 zip tar
-   3.0.0 zip tar
-   2.1.8 zip tar
-   2.1.7 zip tar
-   2.1.6 zip tar
-   2.1.5 zip tar
-   2.1.4 zip tar
-   2.1.2 zip tar
-   2.1.1 zip tar
-   2.0.1 zip tar
-   2.0.0 zip tar
-   1.2.2 zip tar
-   1.2.0 zip tar