简体   繁体   English

如何在 Capacitor 应用程序中设置 Cordova 插件变量?

[英]How to set Cordova plugin variables in a Capacitor app?

I am attempting to set up the Wonderpush integration for Ionic using the docs at https://docs.wonderpush.com/docs/mobile-push-notifications-ionic我正在尝试使用https://docs.wonderpush.com/docs/mobile-push-notifications-ionic上的文档为 Ionic 设置 Wonderpush 集成

The instructions say to install the plugin with:说明说要安装插件:

ionic cordova plugin add wonderpush-cordova-sdk --variable CLIENT_ID=YOUR_CLIENT_ID --variable CLIENT_SECRET=YOUR_CLIENT_SECRET --variable SENDER_ID=YOUR_SENDER

...but since my app is a Capacitor app, I must install the plugin with: ...但由于我的应用程序是 Capacitor 应用程序,因此我必须安装插件:

npm install wonderpush-cordova-sdk

I can't figure out how to set the CLIENT_ID, CLIENT_SECRET, and SENDER_ID when installing the plugin this way (there is no “ --variable ” flag for this command), and I'm getting a runtime error when running the app on my device saying those variables are missing.以这种方式安装插件时,我不知道如何设置 CLIENT_ID、CLIENT_SECRET 和 SENDER_ID(此命令没有“ --variable ”标志),并且在运行应用程序时出现运行时错误我的设备说这些变量丢失了。 Do you know how I can populate these values?你知道我如何填充这些值吗? Thanks in advance!提前致谢!

I was able to find where to put these variables.我能够找到放置这些变量的位置。 In capacitor.config.json , I made the following changes to the "cordova": {} element.capacitor.config.json ,我对"cordova": {}元素进行了以下更改。

"cordova": {
    "preferences": {
      "WONDERPUSH_CLIENT_ID": "<YOUR-CLIENT-ID>",
      "WONDERPUSH_CLIENT_SECRET": "<YOUR-CLIENT-SECRET>",
      "WONDERPUSH_LOGGING": true,
      "WONDERPUSH_AUTO_INIT": true,
      "WONDERPUSH_REQUIRES_USER_CONSENT": false
    }
  }

Then, I ran ionic cap sync , started the app from the IDE, and success!然后,我运行ionic cap sync ,从 IDE 启动应用程序,成功!

Capacitor apps do not support variables while installing a plugin.安装插件时,电容器应用程序不支持变量。 This is because Capacitor apps are more native oriented and do not provide configuration hooks via command line like Cordova does.这是因为 Capacitor 应用程序更面向本机,并且不像 Cordova 那样通过命令行提供配置挂钩。

To add variable with a plugin, you need to manually update AndroidManifest.xml for Android and Info.plist for iOS .要使用插件添加变量,您需要手动更新 AndroidManifest.xml for Android 和 Info.plist for iOS You can get more details about it here https://www.joshmorony.com/using-cordova-plugins-that-require-install-variables-with-capacitor/您可以在此处获得有关它的更多详细信息https://www.joshmorony.com/using-cordova-plugins-that-require-install-variables-with-capacitor/

Example: <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>示例: <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>

Although these configuration settings are missing in the current documentation of Ionic plugins but for popular plugins, you can check their native versions documents and update the platform files accordingly.虽然这些配置设置在 Ionic 插件的当前文档中缺失,但是对于流行的插件,您可以检查它们的原生版本文档并相应地更新平台文件。

Wondershare Android Configuration: Look for "Alternate ways of specifying configuration options", Section Wondershare Android 配置:查找“指定配置选项的替代方法”,部分

https://docs.wonderpush.com/docs/android-sdk#alternate-ways-of-specifying-configuration-options https://docs.wonderpush.com/docs/android-sdk#alternate-ways-of-specifying-configuration-options

You can automatically update the AndroidManifest.xml for Android and Info.plist for iOS using Ionic's Trapeze tool .您可以使用Ionic 的 Trapeze 工具自动更新适用于 Android 的 AndroidManifest.xml 和适用于 iOS 的 Info.plist。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM