尝试将google-services.json从firebase插件复制到Phonegap构建项目的根目录 .
https://github.com/jvwelzen/cordova-plugin-firebase
对于将GoogleService-Info.plist复制到root的iO工作正常但是对于android似乎不起作用,任何想法都是欢迎的 .
plugin.xml中
<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-plugin-firebase" version="0.1.18"
xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<name>Google Firebase Plugin</name>
<license>MIT</license>
<engines>
<engine name="cordova" version=">=3.2.0" />
</engines>
<platform name="android">
<js-module name="FirebasePlugin" src="www/firebase.js">
<clobbers target="FirebasePlugin" />
</js-module>
<config-file parent="/*" target="res/xml/config.xml">
<feature name="FirebasePlugin">
<param name="android-package" value="org.apache.cordova.firebase.FirebasePlugin" />
<param name="onload" value="true" />
</feature>
</config-file>
<config-file parent="/resources" target="res/values/strings.xml">
<string name="google_app_id">@string/google_app_id</string>
</config-file>
<config-file parent="/resources" target="res/values/strings.xml">
<string name="google_api_key">@string/google_api_key</string>
</config-file>
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<service android:name="org.apache.cordova.firebase.FirebasePluginMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name="org.apache.cordova.firebase.FirebasePluginInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<receiver android:name="org.apache.cordova.firebase.OnNotificationOpenReceiver"></receiver>
<source-file src="src/android/google-services.json" target-dir="." />
<source-file src="src/android/FirebasePlugin.java" target-dir="src/org/apache/cordova/firebase" />
<source-file src="src/android/OnNotificationOpenReceiver.java" target-dir="src/org/apache/cordova/firebase" />
<source-file src="src/android/FirebasePluginInstanceIDService.java" target-dir="src/org/apache/cordova/firebase" />
<source-file src="src/android/FirebasePluginMessagingService.java" target-dir="src/org/apache/cordova/firebase" />
<framework src="src/android/build.gradle" custom="true" type="gradleReference" />
<framework src="com.google.firebase:firebase-core:+" />
<framework src="com.google.firebase:firebase-messaging:+" />
<framework src="com.google.firebase:firebase-crash:+" />
<framework src="com.google.firebase:firebase-config:+" />
</platform>
<platform name="ios">
<js-module name="FirebasePlugin" src="www/firebase.js">
<clobbers target="FirebasePlugin" />
</js-module>
<config-file parent="/*" target="config.xml">
<feature name="FirebasePlugin">
<param name="ios-package" value="FirebasePlugin" />
<param name="onload" value="true" />
</feature>
</config-file>
<config-file parent="aps-environment" target="*/Entitlements-Debug.plist">
<string>development</string>
</config-file>
<config-file parent="aps-environment" target="*/Entitlements-Release.plist">
<string>production</string>
</config-file>
<resource-file src="src/ios/GoogleService-Info.plist" target="Resources/GoogleService-Info.plist" />
<header-file src="src/ios/AppDelegate+FirebasePlugin.h" />
<source-file src="src/ios/AppDelegate+FirebasePlugin.m" />
<header-file src="src/ios/FirebasePlugin.h" />
<source-file src="src/ios/FirebasePlugin.m" />
<header-file src="src/ios/Firebase/Firebase.h" />
<framework custom="true" src="src/ios/Firebase/Analytics/FirebaseAnalytics.framework" />
<framework custom="true" src="src/ios/Firebase/Analytics/FirebaseCore.framework" />
<framework custom="true" src="src/ios/Firebase/Analytics/FirebaseInstanceID.framework" />
<framework custom="true" src="src/ios/Firebase/Analytics/GoogleInterchangeUtilities.framework" />
<framework custom="true" src="src/ios/Firebase/Analytics/GoogleSymbolUtilities.framework" />
<framework custom="true" src="src/ios/Firebase/Analytics/GoogleUtilities.framework" />
<framework custom="true" src="src/ios/Firebase/Messaging/FirebaseMessaging.framework" />
<framework custom="true" src="src/ios/Firebase/Messaging/GoogleIPhoneUtilities.framework" />
<framework custom="true" src="src/ios/Firebase/Crash/FirebaseCrash.framework" />
<framework custom="true" src="src/ios/Firebase/RemoteConfig/FirebaseRemoteConfig.framework" />
</platform>
<platform name="browser">
<js-module name="FirebasePlugin" src="www/firebase-browser.js">
<clobbers target="FirebasePlugin" />
</js-module>
</platform>
<hook src="scripts/after_prepare.js" type="after_prepare" />
</plugin>
after_prepare.js
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var getValue = function(config, name) {
var value = config.match(new RegExp('<' + name + '>(.*?)</' + name + '>', "i"))
if(value && value[1]) {
return value[1]
} else {
return null
}
}
function fileExists(path) {
try {
return fs.statSync(path).isFile();
}
catch (e) {
return false;
}
}
function directoryExists(path) {
try {
return fs.statSync(path).isDirectory();
}
catch (e) {
return false;
}
}
var config = fs.readFileSync("config.xml").toString()
var name = getValue(config, "name")
if (directoryExists("platforms/ios")) {
var paths = ["GoogleService-Info.plist", "platforms/ios/www/GoogleService-Info.plist"];
for (var i = 0; i < paths.length; i++) {
if (fileExists(paths[i])) {
try {
var contents = fs.readFileSync(paths[i]).toString();
fs.writeFileSync("platforms/ios/" + name + "/Resources/GoogleService-Info.plist", contents)
} catch(err) {
process.stdout.write(err);
}
break;
}
}
}
if (directoryExists("platforms/android")) {
var paths = ["google-services.json", "platforms/android/assets/www/google-services.json"];
for (var i = 0; i < paths.length; i++) {
if (fileExists(paths[i])) {
try {
var contents = fs.readFileSync(paths[i]).toString();
fs.writeFileSync("platforms/android/google-services.json", contents);
var json = JSON.parse(contents);
var strings = fs.readFileSync("platforms/android/res/values/strings.xml").toString();
// strip non-default value
strings = strings.replace(new RegExp('<string name="google_app_id">([^\@<]+?)</string>', "i"), '')
// strip non-default value
strings = strings.replace(new RegExp('<string name="google_api_key">([^\@<]+?)</string>', "i"), '')
// strip empty lines
strings = strings.replace(new RegExp('(\r\n|\n|\r)[ \t]*(\r\n|\n|\r)', "gm"), '$1')
// replace the default value
strings = strings.replace(new RegExp('<string name="google_app_id">([^<]+?)</string>', "i"), '<string name="google_app_id">' + json.client[0].client_info.mobilesdk_app_id + '</string>')
// replace the default value
strings = strings.replace(new RegExp('<string name="google_api_key">([^<]+?)</string>', "i"), '<string name="google_api_key">' + json.client[0].api_key[0].current_key + '</string>')
fs.writeFileSync("platforms/android/res/values/strings.xml", strings);
} catch(err) {
process.stdout.write(err);
}
break;
}
}
}
1 回答
你不必从cordova-plugin-firebase复制google-services.json . 而是在firebase console创建一个新的!创建一个新项目并添加ios / android . 只需按照步骤操作即可获得所需文件 .