Skip to content

Commit

Permalink
implementation for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrebnov committed Mar 12, 2014
1 parent 9839dd4 commit 9dd0a64
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 13 deletions.
32 changes: 32 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,36 @@ xmlns:android="http://schemas.android.com/apk/res/android"
<clobbers target="" />
</js-module>
</platform>

<!-- android -->
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="NotificationHub">
<param name="android-package" value="msopentech.azure.NotificationHub" />
</feature>
</config-file>

<config-file target="AndroidManifest.xml" parent="/*">
<permission android:name="$PACKAGE_NAME.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="$PACKAGE_NAME.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
</config-file>

<config-file target="AndroidManifest.xml" parent="/manifest/application">
<receiver android:name="msopentech.azure.NotificationHub$PushNotificationReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="$PACKAGE_NAME" />
</intent-filter>
</receiver>
</config-file>

<source-file src="src/android/NotificationHub.java" target-dir="src/msopentech/azure" />

<lib-file src="src/android/google-play-services.jar" />
<lib-file src="src/android/notification-hubs-0.1.jar" />
</platform>
</plugin>
145 changes: 145 additions & 0 deletions src/android/NotificationHub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package msopentech.azure;

import java.util.Set;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;

import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.microsoft.windowsazure.messaging.NativeRegistration;

/**
* Apache Cordova plugin for Windows Azure Notification Hub
*/
public class NotificationHub extends CordovaPlugin {

/**
* The callback context from which we were invoked.
*/
protected static CallbackContext _callbackContext = null;

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
_callbackContext = callbackContext;
try {

if (action.equals("registerApplication")) {
String hubName = args.getString(0);
String connectionString = args.getString(1);
String senderId = args.getString(4);
registerApplication(hubName, connectionString, senderId);
return true;
}

if (action.equals("unregisterApplication")) {
String hubName = args.getString(0);
String connectionString = args.getString(1);
unregisterApplication(hubName, connectionString);
return true;
}

return false; // invalid action
} catch (Exception e) {
_callbackContext.error(e.getMessage());
}
return true;
}

/**
* Asynchronously registers the device for native notifications.
*/
@SuppressWarnings("unchecked")
private void registerApplication(final String hubName, final String connectionString, final String senderId) {

try {
final GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(cordova.getActivity());
final com.microsoft.windowsazure.messaging.NotificationHub hub =
new com.microsoft.windowsazure.messaging.NotificationHub(hubName, connectionString, cordova.getActivity());

new AsyncTask() {
@Override
protected Object doInBackground(Object... params) {
try {
String gcmId = gcm.register(senderId);
NativeRegistration registrationInfo = hub.register(gcmId);

JSONObject registrationResult = new JSONObject();
registrationResult.put("registrationId", registrationInfo.getRegistrationId());
registrationResult.put("channelUri", registrationInfo.getGCMRegistrationId());
registrationResult.put("notificationHubPath", registrationInfo.getNotificationHubPath());
registrationResult.put("event", "registerApplication");

PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, registrationResult);
// keepKallback is used to continue using the same callback to notify about push notifications received
pluginResult.setKeepCallback(true);

NotificationHub.getCallbackContext().sendPluginResult(pluginResult);

} catch (Exception e) {
NotificationHub.getCallbackContext().error(e.getMessage());
}
return null;
}
}.execute(null, null, null);
} catch (Exception e) {
NotificationHub.getCallbackContext().error(e.getMessage());
}
}

/**
* Unregisters the device for native notifications.
*/
private void unregisterApplication(final String hubName, final String connectionString) {
try {
final com.microsoft.windowsazure.messaging.NotificationHub hub =
new com.microsoft.windowsazure.messaging.NotificationHub(hubName, connectionString, cordova.getActivity());
hub.unregister();
NotificationHub.getCallbackContext().success();
} catch (Exception e) {
NotificationHub.getCallbackContext().error(e.getMessage());
}
}

/**
* Handles push notifications received.
*/
public static class PushNotificationReceiver extends android.content.BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

if (NotificationHub.getCallbackContext() == null){
return;
}
JSONObject json = new JSONObject();
try {

Set<String> keys = intent.getExtras().keySet();
for (String key : keys) {
json.put(key, intent.getExtras().get(key));
}
PluginResult result = new PluginResult(PluginResult.Status.OK, json);
result.setKeepCallback(true);
NotificationHub.getCallbackContext().sendPluginResult(result);
} catch (JSONException e) {
e.printStackTrace();
}
}

}

/**
* Returns plugin callback.
*/
protected static CallbackContext getCallbackContext() {
return _callbackContext;
}
}
Binary file added src/android/google-play-services.jar
Binary file not shown.
Binary file added src/android/notification-hubs-0.1.jar
Binary file not shown.
34 changes: 21 additions & 13 deletions www/NotificationHub.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ var Promise = require('./Promise');
*
* @param {string} notificationHubPath The notification hub path (name).
* @param {string} connectionString The connection string.
* @param {string} options Platform specific additional parameters (optional).
*/
var NotificationHub = function (notificationHubPath, connectionString) {
var NotificationHub = function (notificationHubPath, connectionString, options) {
if (typeof notificationHubPath == 'undefined') {
throw new Error('Please specify notificationHubPath');
}
Expand All @@ -39,6 +40,7 @@ var NotificationHub = function (notificationHubPath, connectionString) {
}
this.notificationHubPath = notificationHubPath;
this.connectionString = connectionString;
this.options = options;

this.onPushNotificationReceived = null;
};
Expand All @@ -50,16 +52,6 @@ var NotificationHub = function (notificationHubPath, connectionString) {
* @param {Array} tags The tags (not supported currently).
*/
NotificationHub.prototype.registerApplicationAsync = function (tags) {
var deferral = new Promise.Deferral(),

successCallback = function (result) {
deferral.resolve(result);
},

errorCallback = function (err) {
deferral.reject(err);
};

var me = this,
globalNotificationHandlerName = 'NotificationHub_onNotificationReceivedGlobal';
// global handler that will be called every time new notification is received
Expand All @@ -68,9 +60,25 @@ NotificationHub.prototype.registerApplicationAsync = function (tags) {
if (me.onPushNotificationReceived != null) {
me.onPushNotificationReceived(msg)
}
}
};

var deferral = new Promise.Deferral(),

successCallback = function (result) {
// registration completeness callback
if (result && result.event == 'registerApplication') {
delete result.event; // not required
deferral.resolve(result);
} else { //push notification
window[globalNotificationHandlerName](result);
}
},

errorCallback = function (err) {
deferral.reject(err);
};

exec(successCallback, errorCallback, 'NotificationHub', 'registerApplication', [this.notificationHubPath, this.connectionString, globalNotificationHandlerName, tags]);
exec(successCallback, errorCallback, 'NotificationHub', 'registerApplication', [this.notificationHubPath, this.connectionString, globalNotificationHandlerName, tags, this.options]);

return deferral.promise;
}
Expand Down

0 comments on commit 9dd0a64

Please sign in to comment.