- 版本:11.1.5
- GitHub:https://github.com/eddyverbruggen/nativescript-plugin-firebase
- NPM:https://npmjs.net.cn/package/%40chasexc%2Fnativescript-firebase
- 下载
- 昨天:1
- 上周:9
- 上个月:24
NativeScript Firebase 插件
🚨🚨🚨 我无法再维护此项目 - 我现在正在存档该项目,但如果您想接管,请不要打开问题,而是通过 Twitter @eddyverbruggen 联系我。或者,请查看这个出色的插件:https://github.com/NativeScript/firebase
特性
- AdMob
- 分析
- 身份验证
- Cloud Firestore
- Cloud Functions
- Cloud Messaging / 非 Firebase 推送消息
- Crashlytics
- 应用内消息
- 动态链接
- ML Kit
- 性能监控
- 实时数据库
- 远程配置
- 存储
先决条件
前往 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 控制台以及如何集成匿名身份验证:
从命令提示符转到您的应用程序根目录并执行此操作以使用 NativeScript 7+
tns plugin add @nativescript/firebase
或对于 NativeScript 6
tns plugin add nativescript-plugin-firebase
这将启动一个安装脚本,该脚本将引导您安装附加组件。查看上面的文档链接以了解具体情况。您以后始终可以更改您的选择。
想使用此插件与 外部推送通知提供商 并 不 使用任何 Firebase 功能?只需回答第一个问题的 'y' 以跳过其中大部分,并转到推送通知。在这种情况下,请不要运行插件的
.init
函数!
使用 NativeScript SideKick?那么上述安装脚本将无法(运行)。在这种情况下,运行 Android 应用会导致 此问题。要修复此问题,请参阅 此评论。
配置
如果您选择在安装过程中保存您的配置,支持选项可能已保存在应用程序根目录的 firebase.nativescript.json
中。这是为了确保您的应用程序可以通过源控制来回旅行,并且 CI 安装期间不会提示用户输入。
您可以通过转到 node_modules/nativescript-plugin-firebase
并运行 npm run config
来重新配置插件。
您还可以通过删除firebase.nativescript.json
并重新安装插件来更改配置。
请注意。在
firebase.nativescript.json
中启用某些功能(例如Admob)可能需要额外的配置。如果您在安装此插件后遇到崩溃或错误,请查阅每个已启用功能的文档,以确保无需进行额外配置。
使用Vue吗?
请将您的NativeScript-Vue模板更新到2.0,因为它与该插件完美匹配(因为该模板现在与常规NativeScript项目非常相似)。
如果您想查看使用Vue和Firestore的演示,请查看这个项目footplr,如果您想查看使用实时数据库的,请查看这个项目mixology-mobile。
iOS (Cocoapods)
通过Cocoapods安装Firebase iOS SDK,因此请在命令提示符(任何文件夹)中运行pod repo update
以确保您有最新的规范。
Google Play服务版本
插件将默认使用此版本的Android play-services-base
SDK。如果您需要更改版本(例如,更改为最新版本),您可以将项目扩展属性googlePlayServicesVersion
添加到app/App_Resources/Android/app.gradle
project.ext {
googlePlayServicesVersion = "+"
}
用法
演示应用
如果您想快速入门,请克隆存储库,然后
cd src
.npm i
(对于任何提示,只需回答'n',因为它们将被忽略)。npm run demo.ios
或npm run demo.android
(如果提示,再次回答'n')。
启动配置
当您的应用启动时,我们需要进行一些配置,因此打开app.js
并在application.start();
之前添加以下内容
JavaScript
// NativeScript 7+
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
// NativeScript 7+
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
由于Angular引导的具体情况,最好在Angular应用启动后初始化Firebase。例如,您的组件的主方法的ngOnInit
// NativeScript 7+
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}`);
}
);
}
}
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
另一个可能的错误是 "不存在名为[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 Services(可能是Google Maps)的插件。我们需要将版本锁定到特定的play services版本,以便与其他插件友好协作,因此打开 app/App_Resources/Android/app.gradle
并添加
android {
// other stuff here
project.ext {
googlePlayServicesVersion = "15.0.0"
}
}
其中 "15.0.0"
最好设置为与 此文件 中的 googlePlayServicesVersion
值相同的值。
环境分离
可以通过使用多个 GoogleService-Info.plist
和 google-services.json
文件来使用不同的开发和生产环境。
设置
-
创建两个独立的Firebase项目(例如
myproject
和myproject-dev
),并使用相同的包名进行配置 -
下载两个项目的
plist
和json
文件,并将它们放置在相应的目录中,文件名后追加.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
- iOS
注意:如果您当前在 firebase.init()
中有 storageBucket
属性,则删除它(自本插件版本 6.5.0
起不再强制要求),它将自动从相关的google服务 plist
和 json
文件中获取。
构建
此插件的构建钩子现在将根据您的构建方式选择您的google服务的 plist
和 json
文件的 dev
或 prod
版本。
- 如果您使用
--env.dev
、--env.development
或--env.staging
标志运行,则将选择dev
。 - 如果您使用
--env.prod
或--env.production
运行,则将选择prod
。
注意:如果您不使用上述任何标志而使用 --release
标志,则默认环境将设置为生产。如果您需要创建具有开发环境的发布版本,则需要显式设置它。
注意:如果您没有放置 dev
和 prod
文件,则将使用常规的 GoogleService-Info.plist
和 google-services.json
文件。