使用cordova-plugin-fcm通过PhoneGap Build构建Firebase推送通知

Jor*_*aso 9 push-notification firebase phonegap-build cordova-plugins firebase-cloud-messaging

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

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

我找到了:https://www.npmjs.com/package/cordova-plugin-firebase

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

<plugin name="cordova-plugin-fcm" source="npm" />
Run Code Online (Sandbox Code Playgroud)

并将我从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
Run Code Online (Sandbox Code Playgroud)

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

目前我的app文件夹如下所示:

\css
\img
\js
\res (icons and splashscreens)
index.html
config.xml
google-services.json
Run Code Online (Sandbox Code Playgroud)

如果有人知道我要放置该文件的位置,或者使用其他API有任何其他方式来实现它,我会非常感激.我从未使用过Phonegap CLI,我一直在使用Phonegap Build.

jce*_*ile 9

Cordova resource-file不久前推出了这个标签,它也适用于Phonegap Build.

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

把它放在项目的根目录中,并使用resource-fileconfig.xml中的标记,如下所示:

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

<platform name="android">
  <resource-file src="google-services.json" target="app/google-services.json" />
</platform>
Run Code Online (Sandbox Code Playgroud)

旧版本

<platform name="android">
  <resource-file src="google-services.json" target="google-services.json" />
</platform>
Run Code Online (Sandbox Code Playgroud)

iOS版

<platform name="ios">
  <resource-file src="GoogleService-Info.plist" />
</platform>
Run Code Online (Sandbox Code Playgroud)

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

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


Jai*_*mez 8

文件需要与www文件夹并排,问题是Phonegap Build只允许您上传所述文件夹的内容,因此无法上传文件.

幸运的是,你可以做些什么; Build直接从npm安装插件,插件可以考虑整个项目,所以你需要的是一个为你复制文件[ google-services.json,GoogleService-Info.plist] 的插件.

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

此插件在项目根文件夹和Xcode项目中复制所需的FCM配置.它与伟大的cordova-plugin-fcm插件结合使用.

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

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

  1. 将插件克隆/分叉到您自己的存储库中.
  2. 替换配置文件.
  3. 将它上传到Build可以找到的地方.

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

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

  • 仅供参考,您还可以克隆项目并将其托管在bitbucket的个人存储库中.然后在配置文件中使用<pluginname ="cordova-plugin-fcm-config"spec ="https:// username:password@bitbucket.org/username/cordova-plugin-fcm-config.git"/> (4认同)