Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Commit

Permalink
🐛 Issue #1188: Strings.xml google_app_id conflict with google-service…
Browse files Browse the repository at this point in the history
…s.json
  • Loading branch information
macdonst committed Apr 29, 2017
1 parent 453561c commit f3b01f1
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 12 deletions.
15 changes: 5 additions & 10 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,16 @@
<engine name="cordova-ios" version=">=4.3.0"/>
</engines>

<preference name="SENDER_ID" default="85075801930"/>

<!-- android -->
<platform name="android">
<hook type="before_plugin_install" src="scripts/copyAndroidFile.js" />

<config-file target="res/xml/config.xml" parent="/*">
<feature name="PushNotification" >
<param name="android-package" value="com.adobe.phonegap.push.PushPlugin"/>
</feature>
</config-file>

<config-file target="res/values/strings.xml" parent="/resources">
<string name="google_app_id">$SENDER_ID</string>
</config-file>

<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Expand All @@ -65,10 +60,11 @@
</service>
</config-file>

<framework src="push.gradle" custom="true" type="gradleReference" />
<framework src="com.android.support:support-v13:23+" />
<framework src="me.leolin:ShortcutBadger:1.1.11@aar"/>
<!-- framework src="com.google.gms:google-services:3.0.0" /-->
<framework src="com.google.firebase:firebase-messaging:9+" />
<framework src="push.gradle" custom="true" type="gradleReference" />

<source-file src="src/android/com/adobe/phonegap/push/FCMService.java" target-dir="src/com/adobe/phonegap/push/" />
<source-file src="src/android/com/adobe/phonegap/push/PushConstants.java" target-dir="src/com/adobe/phonegap/push/" />
Expand Down Expand Up @@ -104,7 +100,8 @@
<!-- Start FCM Related Files -->
<framework src="FirebaseMessaging" type="podspec" spec="~> 1.2.1"/>

<resource-file src="src/ios/GoogleService-Info.plist" target="Resources/GoogleService-Info.plist" />
<!-- resource-file src="src/ios/GoogleService-Info.plist" target="Resources/GoogleService-Info.plist" / -->
<hook src="scripts/copy_file.js" type="after_prepare" />
<!-- End FCM Related Files -->
</platform>

Expand All @@ -117,6 +114,4 @@
<preference name="WindowsToastCapable" value="true" />
</config-file>
</platform>

<hook src="scripts/copy_file.js" type="after_prepare" />
</plugin>
4 changes: 4 additions & 0 deletions push.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def doExtractStringFromManifest(name) {
return matcher.group(1)
}

cdvPluginPostBuildExtras << {
apply plugin: 'com.google.gms.google-services'
}

android {
sourceSets {
main {
Expand Down
34 changes: 34 additions & 0 deletions scripts/copyAndroidFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = function(ctx) {
var fs = ctx.requireCordovaModule('fs'),
path = ctx.requireCordovaModule('path'),
os = require("os"),
readline = require("readline"),
deferral = ctx.requireCordovaModule('q').defer();

var settingsFile = path.join(ctx.opts.projectRoot, 'google-services.json');

fs.stat(settingsFile, function(err,stats) {
if (err) {
deferral.reject("To use this plugin on android you'll need to add a google-services.json file with the FCM project_info and place that into your www folder");
} else {

fs.createReadStream(settingsFile).pipe(fs.createWriteStream('platforms/android/google-services.json'));

var lineReader = readline.createInterface({
terminal: false,
input : fs.createReadStream('platforms/android/build.gradle')
});
lineReader.on("line", function(line) {
fs.appendFileSync('./build.gradle', line.toString() + os.EOL);
if (/.*\ dependencies \{.*/.test(line)) {
fs.appendFileSync('./build.gradle', '\t\tclasspath "com.google.gms:google-services:3.0.0"' + os.EOL);
}
}).on("close", function () {
fs.rename('./build.gradle', 'platforms/android/build.gradle', deferral.resolve);
});

}
});

return deferral.promise;
};
11 changes: 10 additions & 1 deletion scripts/copy_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,13 @@ if(fileExists("GoogleService-Info.plist") && directoryExists("platforms/ios/")){
} catch(err) {
process.stdout.write(err);
}
}
}

if(fileExists("google-services.json") && directoryExists("platforms/android/")){
try {
var contents = fs.readFileSync("google-services.json").toString();
fs.writeFileSync("platforms/android/google-services.json", contents);
} catch(err) {
process.stdout.write(err);
}
}
1 change: 1 addition & 0 deletions src/android/com/adobe/phonegap/push/PushConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@ public interface PushConstants {
public static final String IMAGE_TYPE_SQUARE = "square";
public static final String IMAGE_TYPE_CIRCLE = "circle";
public static final String GOOGLE_APP_ID = "google_app_id";
public static final String GCM_DEFAULT_SENDER_ID = "gcm_defaultSenderId";
}
2 changes: 1 addition & 1 deletion src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void run() {

Log.v(LOG_TAG, "execute: jo=" + jo.toString());

senderID = getStringResourceByName(GOOGLE_APP_ID);
senderID = getStringResourceByName(GCM_DEFAULT_SENDER_ID);

Log.v(LOG_TAG, "execute: senderID=" + senderID);

Expand Down

0 comments on commit f3b01f1

Please sign in to comment.