forked from sgrebnov/cordova-plugin-azure-notificationhub
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
198 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters