testnsfire
火!基础!Firebase!
npm i --save testnsfire

NativeScript Firebase 插件

Firebase

为了与 NativeScript 7 兼容,运行 tns plugin add testnsfire

NPM version Downloads TotalDownloads

为了与 NativeScript 6.1+ 兼容,安装插件版本 10: tns plugin add nativescript-plugin-firebase@10

对于 NativeScript 6.0 及以下版本,请坚持使用插件版本 < 10。

NPM version Downloads TotalDownloads

功能

先决条件

前往 https://console.firebase.google.com/ 并注册一个免费账户。您的第一个 'Firebase' 将自动创建并通过类似 https://n-plugin-test.firebaseio.com 的 URL 提供。

在 Google 控制台中打开您的 Firebase 项目,然后点击 '添加应用' 以添加 iOS 和 / 或 Android 应用。按照步骤进行(确保 bundle id 与 package.json 中的 nativescript.id 相同),然后您可以下载

  • iOS: GoogleService-Info.plist,您需要将其添加到 NativeScript 项目的 app/App_Resources/iOS/GoogleService-Info.plist

  • Android: google-services.json,您需要将其添加到 NativeScript 项目的 app/App_Resources/Android/google-services.json

注意:对于开发和生产环境使用不同版本的这些文件,请参阅本节

安装

如果您想观看一个(略有过时)的视频来解释这些步骤,请查看此分步指南 - 您还将了解如何将 iOS 和 Android 支持添加到 Firebase 控制台以及如何集成匿名身份验证: YouTube demo

从命令提示符转到您的应用程序根目录并执行此操作(适用于 NativeScript 7+)

tns plugin add testnsfire

或 NativeScript 6

tns plugin add nativescript-plugin-firebase

这将启动一个安装脚本,该脚本将引导您安装其他组件。查看上面的文档链接以了解相关信息。您始终可以在以后更改您的选择。

想要使用此插件与 外部推送通知提供商 使用任何 Firebase 功能?只需回答第一个问题“是”即可跳过大多数问题,然后跳转到推送通知。在这种情况下不要运行插件的 .init 函数!

使用NativeScript SideKick?那么上述安装脚本将无法运行。在这种情况下,运行 Android 应用将导致此问题。要修复此问题,请参阅此评论

配置

如果您选择在安装过程中保存配置,支持的选项可能会保存在应用程序根目录下的 firebase.nativescript.json 文件中。这是为了确保您的应用程序能够在源控制和CI安装之间往返,且在CI安装时不会提示用户输入。

您可以通过访问 node_modules/nativescript-plugin-firebase 并运行 npm run config 来重新配置插件。

您也可以通过删除 firebase.nativescript.json 并重新安装插件来更改配置。

使用Vue吗?

请将您的 NativeScript-Vue 模板更新到2.0版本,因为它与该插件完美匹配(因为该模板现在与常规NativeScript项目更为相似)。

如果您想使用Vue和Firestore的演示,请查看这个项目:https://github.com/EddyVerbruggen/footplr,如果您想使用Realtime DB的演示,请查看这个项目:https://github.com/jlooper/mixology-mobile

iOS (Cocoapods)

Firebase iOS SDK 通过Cocoapods安装,因此从命令提示符(任何文件夹)中运行 pod repo update 以确保您有最新的规范。

Google Play Services 版本

插件将默认使用 Android play-services-base SDK 的此版本。如果您需要更改版本(例如,最新的版本),您可以在 app/App_Resources/Android/app.gradle 中添加一个项目扩展属性 googlePlayServicesVersion

project.ext {
googlePlayServicesVersion = "+"
}

使用方法

示例应用程序

如果您想快速开始,请克隆存储库,然后

  • cd src.
  • npm i (对于任何提示只需回答 'n',因为它们无论如何都会被忽略)。
  • npm run demo.iosnpm run demo.android (如果提示,再次回答 'n')。

启动连接

当您的应用程序启动时,我们需要做一些连接操作,因此打开 app.js 并在 application.start(); 之前添加以下内容。

JavaScript
// NativeScript 7+
var firebase = require("testnsfire");

// 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

// NativeScript 7+
import { firebase } from "testnsfire";

// 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

由于Angular引导的特定性,最好在Angular应用程序运行后初始化Firebase。例如,在主组件的 ngOnInit 方法中初始化。

// NativeScript 7+
import { firebase } from "testnsfire";

// 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}`);
}
);
}
}

在iOS上的已知问题

在模拟器上运行时出现问题

打开或创建 App_Resources/iOS/<appname>.entitlements 并添加这两个键,其值为 true

<?xml version="1.0" encoding="UTF-8"?>
<!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-keys</key>
<true/>
<key>com.apple.keystore.device</key>
<true/>
</dict>
</plist>

认证失败:无效令牌

在模拟器上,如果您安装了多个带有Firebase SDK的应用程序,可能会看到此消息。

[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')

这是 Firebase SDK中的已知问题。我总是使用真实设备来避免这个问题,但您可以在初始化时传递一个 'iOSEmulatorFlush' 选项。

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依赖错误

如果您看到类似于 Unable to satisfy the following requirements: Firebase (~> 3.17.0) required by Podfile 的错误,请在命令行上运行 pod repo update 以确保您有最新的Podspec。

这可能在更新插件到新版本时发生。您还需要运行 tns platform remove ios && tns platform add ios 以清除旧的pod版本。

在Android上的已知问题

Genymotion

您可以使用出色的 Genymotion模拟器,但您需要在它上安装 Google Play Services,否则在认证过程中会遇到错误。

DexIndexOverflowException

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

恭喜,您遇到了 这个问题,可以通过在 app/App_Resources/Android/app.gradle 中添加 multiDexEnabled true 来解决,使其类似于以下内容

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

java.lang.OutOfMemoryError: GC overhead limit exceeded

增加Java最大堆大小,如下所示(末尾的位)。

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

默认名称为[DEFAULT]的FirebaseApp不存在

另一个可能出现的错误是“默认名称为[DEFAULT]的FirebaseApp不存在。”,可以通过将google-services.json文件放置到platforms/android/google-services.json(参见上方),并对上述提到的build.gradle文件进行修改来解决这个问题。

关于API级别26.0.0的错误

更新您的本地Android SDK

只需从命令提示符运行$ANDROID_HOME/tools/bin/sdkmanager --update,或者从Android Studio启动SDK管理器,展开Extras并安装任何挂起的更新。

找到了play-services:A.C.D,但需要版本B.X.Y

更新您的Android位元,如上所述问题,并在您的项目中重新安装android平台。

include.gradle:应用插件失败。输入字符串:“+”

您可能还有另一个依赖于Google Play服务的插件(例如Google Maps)。我们需要将特定版本的Play服务锁定,以便与其他插件协同工作,因此请打开app/App_Resources/Android/app.gradle并添加

android {  
// other stuff here

project.ext {
googlePlayServicesVersion = "15.0.0"
}
}

其中"15.0.0"最好设置为与此文件中的googlePlayServicesVersion值相同的值。

环境分离

可以通过使用多个GoogleService-Info.plistgoogle-services.json文件来使用不同的开发和生产环境。

配置

  1. 创建两个单独的Firebase项目(例如myprojectmyproject-dev),并以相同的包名进行配置

  2. 下载两个项目的plistjson文件,并将它们放入相应的目录中,文件名后附加.dev.prod,以便您有以下文件

    • iOS
      • app/App_Resources/iOS/GoogleService-Info.plist.dev
      • app/App_Resources/iOS/GoogleService-Info.plist.prod
    • Android
      • app/App_Resources/Android/google-services.json.dev
      • app/App_Resources/Android/google-services.json.prod

注意:如果您目前在firebase.init()中具有storageBucket属性,则应将其删除(根据此插件的6.5.0版本,现在不再是强制性的),这样它将自动从相关的Google服务plistjson文件中获取。

构建

此插件的构建钩子现在将根据您如何运行构建选择您的Google服务plistjson文件的devprod版本

  • 如果使用--env.dev--env.development--env.staging标志运行,则选择dev
  • 如果使用--env.prod--env.production运行,则选择prod

注意:如果没有使用上述任何标志使用--release标志,则默认环境为生产。如果您需要创建具有开发环境的发布版本,则需要显式设置它。

注意:如果您没有放置devprod文件,则将使用常规的GoogleService-Info.plistgoogle-services.json文件。