diff --git a/.gitignore b/.gitignore index da7f48c..de75462 100644 --- a/.gitignore +++ b/.gitignore @@ -1,39 +1,40 @@ -# Logs -logs -*.log -npm-debug.log* - -# Runtime data -pids -*.pid -*.seed - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage - -# nyc test coverage -.nyc_output - -# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# node-waf configuration -.lock-wscript - -# Compiled binary addons (http://nodejs.org/api/addons.html) -build/Release - -# Dependency directories -node_modules -jspm_packages - -# Optional npm cache directory -.npm - -# Optional REPL history -.node_repl_history - -.DS_Store +# Logs +logs +*.log +npm-debug.log* + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules +jspm_packages + +# Optional npm cache directory +.npm + +# Optional REPL history +.node_repl_history + +.DS_Store +/nbproject/ diff --git a/src/android/FirebaseMessagingPlugin.java b/src/android/FirebaseMessagingPlugin.java index c443435..6719dae 100644 --- a/src/android/FirebaseMessagingPlugin.java +++ b/src/android/FirebaseMessagingPlugin.java @@ -24,8 +24,12 @@ import org.json.JSONObject; import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; import java.util.Set; import me.leolin.shortcutbadger.ShortcutBadger; +import org.json.JSONArray; public class FirebaseMessagingPlugin extends ReflectiveCordovaPlugin { @@ -150,6 +154,34 @@ private void requestPermission(CallbackContext callbackContext) { } } + @CordovaMethod + private void send(String senderId, String id, JSONObject data, CallbackContext callbackContext) { + FirebaseMessaging fm = FirebaseMessaging.getInstance(); + RemoteMessage.Builder messageBuilder = new RemoteMessage.Builder(senderId + "@gcm.googleapis.com") + .setMessageId(id); + try { + for (Map.Entry entry : toMap(data).entrySet()) { + messageBuilder.addData(entry.getKey(), entry.getValue()); + } + fm.send(messageBuilder.build()); + } catch (JSONException e) { + Log.e(TAG, "sendNotification", e); + callbackContext.error("Message is not sent"); + } + + } + + public static Map toMap(JSONObject jsonobj) throws JSONException { + Map map = new HashMap(); + Iterator keys = jsonobj.keys(); + while(keys.hasNext()) { + String key = keys.next(); + String value = jsonobj.get(key).toString(); + map.put(key, value); + } return map; + } + + @Override public void onNewIntent(Intent intent) { JSONObject notificationData = getNotificationData(intent); diff --git a/www/FirebaseMessaging.js b/www/FirebaseMessaging.js index 2e7cd4b..fe92f53 100644 --- a/www/FirebaseMessaging.js +++ b/www/FirebaseMessaging.js @@ -32,6 +32,11 @@ module.exports = { exec(resolve, reject, PLUGIN_NAME, "getToken", []); }); }, + send: function(senderId, id, data) { + return new Promise(function(resolve, reject) { + exec(resolve, reject, PLUGIN_NAME, "send", [senderId, id, data]); + }); + }, setBadge: function(value) { return new Promise(function(resolve, reject) { exec(resolve, reject, PLUGIN_NAME, "setBadge", [value]);