Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add send method Android #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 40 additions & 39 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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/
32 changes: 32 additions & 0 deletions src/android/FirebaseMessagingPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String, String> 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<String, String> toMap(JSONObject jsonobj) throws JSONException {
Map<String, String> map = new HashMap<String, String>();
Iterator<String> 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);
Expand Down
5 changes: 5 additions & 0 deletions www/FirebaseMessaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down