我有一个不同的谷歌服务。每个发布通道的json文件(prod, stage, QA和dev的文件)
了解更多细节,我想实现推送通知,但在不同的环境。因为我不想在QA中发送测试通知并将通知发送给prod用户!
这是app.json
中的android配置
"android": {
"googleServicesFile": "./google-services.json",
"adaptiveIcon": {
"foregroundImage": "./src/assets/adaptive-icon.png"
},
"permissions": [
"android.permission.CAMERA",
"android.permission.ACCESS_FINE_LOCATION",
"android.permission.WRITE_EXTERNAL_STORAGE",
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.ACTION_BOOT_COMPLETED",
"android.permission.RECORD_AUDIO",
"com.google.android.gms.permission.AD_ID"
],
"package": "com.beyondbelievers.awal",
"versionCode": 1
},Ï
是否有任何方法可以在每个环境中加载不同的文件?
这个答案的灵感来自于一个老问题,你可以在这里查看在我搜索了整个互联网后,我找到了一种方法,但使用app.config.js
我将尝试在以下步骤中解释如何归档我的目标:
- 转换
app.json
到app.config.js
:这里的博览会文档解释了如何从app.json
迁移。 - 然后在我的
eas.json
中,我将以下行添加到所有配置文件"APP_VARIANT": "qa"
:
{
"qa": {
...
"releaseChannel": "qa",
"env": {
"APP_VARIANT": "qa"
}
...
},
"stage": {
...
"releaseChannel": "stage",
"env": {
"APP_VARIANT": "stage"
}
...
},
"prod": {
...
"releaseChannel": "prod",
"env": {
"APP_VARIANT": "prod"
}
...
}
}
}
"APP_VARIANT"
变量包含env的类型,根据它,我可以稍后检查哪个文件给我们。
- 现在回到
app.config.js
添加以下行:
// this will return the value of APP_VARIANT defined in eas.json
const APP_RELEASE_CHANNEL = process.env.APP_VARIANT;
// define the AndroidGoogleServicesFile variable to use instead of the normal string and assign a default value to it
let AndroidGoogleServicesFile = "./google-services-dev.json";
// now check the value of the APP_VARIANT and based on it assign the path of the google-services you wanna use
if (APP_RELEASE_CHANNEL === "qa") {
AndroidGoogleServicesFile = "./google-services-qa.json";
} else if (APP_RELEASE_CHANNEL === "stage") {
AndroidGoogleServicesFile = "./google-services-stage.json";
} else if (APP_RELEASE_CHANNEL === "prod") {
AndroidGoogleServicesFile = "./google-services-prod.json";
}
- 现在你知道你需要使用哪个文件了剩下的一步就是分配那个文件以便你的应用程序可以使用它
android: {
...
googleServicesFile: AndroidGoogleServicesFile,
...
},
这就是我如何解决我的问题,我希望这也能解决你的问题。
如果你有什么不明白的,我欢迎你更详细地解释