diff --git a/plugin.xml b/plugin.xml index 4f66d879f..2f511ef46 100755 --- a/plugin.xml +++ b/plugin.xml @@ -26,10 +26,9 @@ - - + @@ -37,10 +36,6 @@ - - $SENDER_ID - - @@ -65,10 +60,11 @@ - + + @@ -104,7 +100,8 @@ - + + @@ -117,6 +114,4 @@ - - diff --git a/push.gradle b/push.gradle index 11e735ae7..1a410b982 100644 --- a/push.gradle +++ b/push.gradle @@ -8,6 +8,10 @@ def doExtractStringFromManifest(name) { return matcher.group(1) } +cdvPluginPostBuildExtras << { + apply plugin: 'com.google.gms.google-services' +} + android { sourceSets { main { diff --git a/scripts/copyAndroidFile.js b/scripts/copyAndroidFile.js new file mode 100644 index 000000000..cbd9f3481 --- /dev/null +++ b/scripts/copyAndroidFile.js @@ -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; +}; diff --git a/scripts/copy_file.js b/scripts/copy_file.js index 63c4d14f7..7dd20dd00 100644 --- a/scripts/copy_file.js +++ b/scripts/copy_file.js @@ -40,4 +40,13 @@ if(fileExists("GoogleService-Info.plist") && directoryExists("platforms/ios/")){ } catch(err) { process.stdout.write(err); } -} \ No newline at end of file +} + +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); + } +} diff --git a/src/android/com/adobe/phonegap/push/PushConstants.java b/src/android/com/adobe/phonegap/push/PushConstants.java index ae4945fa6..5adf7e32b 100644 --- a/src/android/com/adobe/phonegap/push/PushConstants.java +++ b/src/android/com/adobe/phonegap/push/PushConstants.java @@ -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"; } diff --git a/src/android/com/adobe/phonegap/push/PushPlugin.java b/src/android/com/adobe/phonegap/push/PushPlugin.java index 8d4cdc44d..4819c13fc 100644 --- a/src/android/com/adobe/phonegap/push/PushPlugin.java +++ b/src/android/com/adobe/phonegap/push/PushPlugin.java @@ -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);