NativeScript Firebase plugin
I'M NO LONGER ABLE TO MAINTAIN THIS PROJECT - I'm archiving the project for now, but if you want to take over, please don't open an issue, but hit me up on Twitter @eddyverbruggen. Alternatively, please check out this excellent plugin: https://github.com/NativeScript/firebase
Features
- AdMob
- Analytics
- Authentication
- Cloud Firestore
- Cloud Functions
- Cloud Messaging / Non-Firebase Push Messaging
- Crashlytics
- In-App Messaging
- Dynamic Links
- ML Kit
- Performance Monitoring
- Realtime Database
- Remote Config
- Storage
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.plistwhich you'll add to your NativeScript project atapp/App_Resources/iOS/GoogleService-Info.plist -
Android:
google-services.jsonwhich you'll add to your NativeScript project atapp/App_Resources/Android/google-services.json
Note: for using separate versions of these files for development and production environments see this section
Installation
If you rather watch a (slightly outdated) 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:
From the command prompt go to your app's root folder and execute this for NativeScript 7+:
or for NativeScript 6:
This will launch an install script which will guide you through installing additional components. Check the doc links above to see what's what. You can always change your choices later.
Want to use this plugin with an external push notification provider and not use any Firebase feature? Just answer 'y' to the first question to skip most of them, and hop on over to the Push Notification. Do not run the plugin's
.initfunction in this case!
Using NativeScript SideKick? Then the aforementioned install script will not (be able to) run. In that case, running the app for Android will result in this issue. To fix that, see this comment.
Config
If you choose to save your config during the installation, the supported options may be saved in the firebase.nativescript.json at the root of your app.
This is to ensure your app may roundtrip source control and installation on CI won't prompt for user input during installation.
You can reconfigure the plugin by going to the node_modules/nativescript-plugin-firebase and running npm run config.
You can also change the configuration by deleting the firebase.nativescript.json and reinstalling the plugin.
Be advised. Enabling some features (such as Admob) in the
firebase.nativescript.jsonmay require additional configuration. If you are experiencing crashes or bugs after installing this plugin please consult the documentation for each of the features you've enabled to ensure that no additioal configuration is required.
Using Vue?
Please update your NativeScript-Vue template to 2.0 because it aligns perfectly with this plugin (because that template is now much more similar to a regular NativeScript project).
If you want a demo using Vue and Firestore, then check out this project, if you want one with Realtime DB, check out this one.
iOS (Cocoapods)
The Firebase iOS SDK is installed via Cocoapods, so run pod repo update from the command prompt (in any folder) to ensure you have the latest spec.
Google Play Services Version
The plugin will default to this version of the Android play-services-base SDK.
If you need to change the version (to for instance the latest version), you can add a project ext property googlePlayServicesVersion to app/App_Resources/Android/app.gradle:
project.ext {
googlePlayServicesVersion = "+"
}
Usage
Demo app
If you want a quickstart, clone the repo, then:
cd src.npm i(just answer 'n' to any prompts as they are ignored anyway).npm run demo.iosornpm run demo.android(answer 'n' again if prompted).
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/firebase").firebase;
// NativeScript 6-
var firebase = require("nativescript-plugin-firebase");
firebase.init({
// Optionally pass in properties for database, authentication and cloud messaging,
// see their respective docs.
}).then(
function () {
console.log("firebase.init done");
},
function (error) {
console.log("firebase.init error: " + error);
}
);
TypeScript
import { firebase } from "@nativescript/firebase";
// NativeScript 6-
const firebase = require("nativescript-plugin-firebase");
firebase.init({
// Optionally pass in properties for database, authentication and cloud messaging,
// see their respective docs.
}).then(
() => {
console.log("firebase.init done");
},
error => {
console.log(`firebase.init error: ${error}`);
}
);
Angular
Because of the specifics of the angular bootstrap it is best to initalize firebase once the angular application is running. For example your main compoment's ngOnInit method:
import { firebase } from "@nativescript/firebase";
// NativeScript 6-
const firebase = require("nativescript-plugin-firebase");
@Component({
// ...
})
export class AppComponent implements OnInit {
ngOnInit() {
firebase.init({
// Optionally pass in properties for database, authentication and cloud messaging,
// see their respective docs.
}).then(
() => {
console.log("firebase.init done");
},
error => {
console.log(`firebase.init error: ${error}`);
}
);
}
}
Known issues on iOS
Trouble running on the simulator
Open or create App_Resources/iOS/ and add these two keys with the value true:
DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.keystore.access-keychain-keyskey>
<true/>
<key>com.apple.keystore.devicekey>
<true/>
dict>
plist>
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')