nativescript-plugin-firebase-mlkit-unofficial
by davidpanic | v5.3.1-beta2
火!基础!Firebase!
npm i --save nativescript-plugin-firebase-mlkit-unofficial

NativeScript Firebase 插件

注意:这只是一个分支的构建: issue699-mlkit-support

Build Status NPM version Downloads Twitter Follow

Firebase

功能

先决条件

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

在 Google 控制台中打开您的 Firebase 项目,点击 '添加应用' 以添加 iOS 和 / 或 Android 应用。遵循步骤(确保包标识符与 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 演示

从命令提示符转到您的应用根目录并执行

tns plugin add nativescript-plugin-firebase

这将启动一个安装脚本,该脚本将引导您安装其他组件。检查上面的文档链接以了解详情。您以后可以更改您的选择。

配置

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

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

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

使用 Vue 吗?

如果您正在使用此模板: vue-cli-template,请将 firebase.nativescript.json 复制到 template 文件夹。您也可以创建符号链接: firebase.nativescript.json -> template/firebase.nativescript.json

此外,在 Vue.start 运行之前(可能是在 main.js 中),请先 require 插件,然后运行 firebase.init()(尽管它可能在之前就起作用)。您可以在一个超时中包裹它以确保。

iOS (Cocoapods)

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

Google Play 服务版本

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

project.ext {
googlePlayServicesVersion = "+"
}

使用方法

示例应用

如果您想快速入门,请克隆存储库,cd src,然后运行npm run demo.iosnpm run demo.android

启动连接

当您的应用启动时,我们需要进行一些连接操作,因此打开app.js并在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}`);
}
);

Angular

由于Angular引导的具体情况,最好在Angular应用运行后初始化Firebase。例如,您的主组件的ngOnInit方法

import 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(
instance => {
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服务,否则您将在身份验证过程中遇到错误。

DexIndexOverflowException

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

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

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"
}
}

FirebaseApp with name [DEFAULT] doesn't exist

另一个可能的问题是“FirebaseApp with name [DEFAULT] doesn't exist。”,可以通过将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:10.A.B,但需要版本11.X.Y

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

include.gradle:无法应用插件 .. 对于输入字符串:“+”

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

android {  
// other stuff here

project.ext {
googlePlayServicesVersion = "11.8.0"
}
}

其中"11.8.0"最好设置为此文件firebaseVersion的值。