使用 cordova-plugin-fcm 的 PhoneGap Build 的 Firebase 推送通知

Firebase Push notifications with PhoneGap Build using cordova-plugin-fcm

我需要在我正在开发的 PhoneGap Build 应用程序中实现推送通知。

我没有找到太多关于使用新的 Google 服务 Firebase 云消息传递 (FCM) 的文档,但由于它应该是我们现在必须使用的,所以我搜索了 API 这可以让我的生活更轻松。

我发现:https://www.npmjs.com/package/cordova-plugin-firebase

所以我添加到我的 config.xml:

<plugin name="cordova-plugin-fcm" source="npm" />

并将我从 https://console.firebase.google.com/ 创建的 google-services.json 放在我压缩上传到 Phonegap Build 的文件夹的根目录中。

但是我得到以下错误:

Execution failed for task ':processReleaseGoogleServices'.
> File google-services.json is missing. The Google Services Plugin cannot function without it. 
  Searched Location: 
  /project/src/release/google-services.json
  /project/google-services.json

我在根目录中创建了 "project" 文件夹并将文件放在那里,但这也不起作用。

目前我的应用程序文件夹如下所示:

\css
\img
\js
\res (icons and splashscreens)
index.html
config.xml
google-services.json

如果有人知道我要把那个文件放在哪里,或者有任何其他方法来实现这个甚至使用其他 API,我将非常感激。 我从未使用过 Phonegap CLI,我一直在使用 Phonegap Build。

文件需要放在 www 文件夹旁边,问题是 Phonegap Build 只允许你上传所述文件夹的内容,所以你不能上传文件。

幸运的是,您可以做些事情; Build 直接从 npm 安装插件,插件可以在整个项目中工作,所以你需要的是一个可以为你复制文件 [google-services.json, GoogleService-Info.plist] 的插件。

大部分工作已经完成:cordova-plugin-fcm-config

This plugin copies the required FCM configurations in the project root folders and Xcode project. It is used in combination with the great cordova-plugin-fcm plugin.

它不是为此目的而建造的,但效果非常好。

虽然增加了复杂性;由于您无法上传插件(您只能在 config.xml 中添加对它的引用),并且此插件需要您自己的应用配置文件,因此您必须:

  1. Clone/Fork 将插件放入您自己的存储库中。
  2. 替换配置文件。
  3. 上传到 Build 可以找到的地方。

你在哪里上传的?这可能很棘手。基础架构是围绕插件用于通用目的并且可以在每个项目中配置 xml 的想法构建的,因此 npm 是有意义的。但在这种情况下,您的插件将包含非常具体的项目数据,因此在我看来上传到 npm 会污染命名空间。

我不了解你,但我有一个付费帐户,所以我将插件发布在我自己的存储库中并作为私有插件提交。这是我推荐的。

Cordova 前段时间引入了 resource-file 标签,它也适用于 Phonegap Build。

您可以使用它而不是分叉插件来复制 google-services.jsonGoogleService-Info.plist 文件

将 放在项目的根目录中,并在 config.xml 中使用 resource-file 标签,如下所示:

如果使用 cordova-android 7 或更新版本:

<platform name="android">
  <resource-file src="google-services.json" target="app/google-services.json" />
</platform>

旧版本

<platform name="android">
  <resource-file src="google-services.json" target="google-services.json" />
</platform>

iOS

<platform name="ios">
  <resource-file src="GoogleService-Info.plist" />
</platform>

您也可以将其放在 www 文件夹中,在这种情况下,在我的示例中,在 src 字段中的文件名前添加 www/

https://cordova.apache.org/docs/en/7.x/config_ref/index.html#resource-file

对我来说,我必须进入 .xcodeproj 文件并将主界面从 MainViewController.xib 更改为 CDVLaunchScreen.storyboard

重建并成功。我的应用程序是用 Ionic 构建的,我在 Ionic Creator 中启动它。