Commit 157283e4 authored by Simon MacDonald's avatar Simon MacDonald
Browse files

Send notification event

parent 1daa8fa0
Loading
Loading
Loading
Loading
+10 −10
Changes for src/android/com/plugin/gcm/GCMIntentService.java: 10 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ import com.google.android.gcm.GCMBaseIntentService;
@SuppressLint("NewApi")
public class GCMIntentService extends GCMBaseIntentService {

	private static final String TAG = "GCMIntentService";
	private static final String LOG_TAG = "PushPlugin_GCMIntentService";
	
	public GCMIntentService() {
		super("GCMIntentService");
@@ -27,7 +27,7 @@ public class GCMIntentService extends GCMBaseIntentService {
	@Override
	public void onRegistered(Context context, String regId) {

		Log.v(TAG, "onRegistered: "+ regId);
		Log.v(LOG_TAG, "onRegistered: "+ regId);

		JSONObject json;

@@ -35,25 +35,25 @@ public class GCMIntentService extends GCMBaseIntentService {
			json = new JSONObject();
			json.put("registrationId", regId);

			Log.v(TAG, "onRegistered: " + json.toString());
			Log.v(LOG_TAG, "onRegistered: " + json.toString());

			PushPlugin.sendRegistrationEvent( json );
			PushPlugin.sendEvent( json );
		}
		catch( JSONException e)
		{
			// No message to the user is sent, JSON failed
			Log.e(TAG, "onRegistered: JSON exception");
			Log.e(LOG_TAG, "onRegistered: JSON exception");
		}
	}

	@Override
	public void onUnregistered(Context context, String regId) {
		Log.d(TAG, "onUnregistered - regId: " + regId);
		Log.d(LOG_TAG, "onUnregistered - regId: " + regId);
	}

	@Override
	protected void onMessage(Context context, Intent intent) {
		Log.d(TAG, "onMessage - context: " + context);
		Log.d(LOG_TAG, "onMessage - context: " + context);

		// Extract the payload from the message
		Bundle extras = intent.getExtras();
@@ -122,10 +122,10 @@ public class GCMIntentService extends GCMBaseIntentService {
			notId = Integer.parseInt(extras.getString("notId"));
		}
		catch(NumberFormatException e) {
			Log.e(TAG, "Number format exception - Error parsing Notification ID: " + e.getMessage());
			Log.e(LOG_TAG, "Number format exception - Error parsing Notification ID: " + e.getMessage());
		}
		catch(Exception e) {
			Log.e(TAG, "Number format exception - Error parsing Notification ID" + e.getMessage());
			Log.e(LOG_TAG, "Number format exception - Error parsing Notification ID" + e.getMessage());
		}
		
		mNotificationManager.notify((String) appName, notId, mBuilder.build());
@@ -143,7 +143,7 @@ public class GCMIntentService extends GCMBaseIntentService {
	
	@Override
	public void onError(Context context, String errorId) {
		Log.e(TAG, "onError - errorId: " + errorId);
		Log.e(LOG_TAG, "onError - errorId: " + errorId);
	}

}
+2 −2
Changes for src/android/com/plugin/gcm/PushHandlerActivity.java: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ import android.util.Log;

public class PushHandlerActivity extends Activity
{
	private static String TAG = "PushHandlerActivity"; 
	private static String LOG_TAG = "PushPlugin_PushHandlerActivity"; 

	/*
	 * this activity will be started if the user touches a notification that we own. 
@@ -22,7 +22,7 @@ public class PushHandlerActivity extends Activity
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Log.v(TAG, "onCreate");
		Log.v(LOG_TAG, "onCreate");

		boolean isPushPluginActive = PushPlugin.isActive();
		processPushBundle(isPushPluginActive);
+30 −49
Changes for src/android/com/plugin/gcm/PushPlugin.java: 30 added lines, 49 removed lines.
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ public class PushPlugin extends CordovaPlugin {

	private static CallbackContext pushContext;
	private static CordovaWebView gWebView;
	private static String gECB;
	//private static String gECB;
	private static String gSenderID;
	private static Bundle gCachedExtras = null;
    private static boolean gForeground = false;
@@ -62,10 +62,9 @@ public class PushPlugin extends CordovaPlugin {
				gWebView = this.webView;
				Log.v(LOG_TAG, "execute: jo=" + jo.toString());

				//gECB = (String) jo.get("ecb");
				gSenderID = (String) jo.get("senderID");
				gSenderID = jo.getString("senderID");

				Log.v(LOG_TAG, "execute: ECB=" + gECB + " senderID=" + gSenderID);
				Log.v(LOG_TAG, "execute: senderID=" + gSenderID);

				GCMRegistrar.register(getApplicationContext(), gSenderID);
				result = true;
@@ -97,33 +96,20 @@ public class PushPlugin extends CordovaPlugin {
		return result;
	}

	public static void sendRegistrationEvent(JSONObject _json) {
	public static void sendEvent(JSONObject _json) {
        PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, _json);
        pluginResult.setKeepCallback(true);
        pushContext.sendPluginResult(pluginResult);
	}

	/*
	 * Sends a json object to the client as parameter to a method which is defined in gECB.
	 */
	public static void sendJavascript(JSONObject _json) {
		String _d = "javascript:" + gECB + "(" + _json.toString() + ")";
		Log.v(LOG_TAG, "sendJavascript: " + _d);

		if (gECB != null && gWebView != null) {
			gWebView.sendJavascript(_d);
		}
	}

	/*
	 * Sends the pushbundle extras to the client application.
	 * If the client application isn't currently active, it is cached for later processing.
	 */
	public static void sendExtras(Bundle extras)
	{
	public static void sendExtras(Bundle extras) {
		if (extras != null) {
			if (gECB != null && gWebView != null) {
				sendJavascript(convertBundleToJson(extras));
			if (gWebView != null) {
				sendEvent(convertBundleToJson(extras));
			} else {
				Log.v(LOG_TAG, "sendExtras: caching extras to send at a later time.");
				gCachedExtras = extras;
@@ -155,7 +141,6 @@ public class PushPlugin extends CordovaPlugin {
    public void onDestroy() {
        super.onDestroy();
        gForeground = false;
		gECB = null;
		gWebView = null;
    }

@@ -164,39 +149,35 @@ public class PushPlugin extends CordovaPlugin {
     */
    private static JSONObject convertBundleToJson(Bundle extras)
    {
		try
		{
			JSONObject json;
			json = new JSONObject().put("event", "message");

			JSONObject jsondata = new JSONObject();
		try {
			JSONObject json = new JSONObject();
			JSONObject additionalData = new JSONObject();
			Iterator<String> it = extras.keySet().iterator();
			while (it.hasNext())
			{
				String key = it.next();
				Object value = extras.get(key);
				 
				Log.d(LOG_TAG, "key = " + key);

				// System data from Android
				if (key.equals("from") || key.equals("collapse_key"))
				{
					json.put(key, value);
				if (key.equals("from") || key.equals("collapse_key")) {
					additionalData.put(key, value);
				}
				else if (key.equals("foreground"))
				{
					json.put(key, extras.getBoolean("foreground"));
				else if (key.equals("foreground")) {
					additionalData.put(key, extras.getBoolean("foreground"));
				}
				else if (key.equals("coldstart"))
				{
					json.put(key, extras.getBoolean("coldstart"));
				else if (key.equals("coldstart")){
					additionalData.put(key, extras.getBoolean("coldstart"));
				} else if (key.equals("message") || key.equals("title")) {
					json.put(key, value);
				} else if (key.equals("msgcnt")) {
					json.put("count", value);
				} else if (key.equals("soundname")) {
					json.put("sound", value);
				}
				else 
				{
					// Maintain backwards compatibility
					if (key.equals("message") || key.equals("msgcnt") || key.equals("soundname"))
					{
						json.put(key, value);
					}

					if ( value instanceof String ) {
					// Try to figure out if the value is another JSON object

@@ -204,10 +185,10 @@ public class PushPlugin extends CordovaPlugin {
						if (strValue.startsWith("{")) {
							try {
								JSONObject json2 = new JSONObject(strValue);
								jsondata.put(key, json2);
								additionalData.put(key, json2);
							}
							catch (Exception e) {
								jsondata.put(key, value);
								additionalData.put(key, value);
							}
							// Try to figure out if the value is another JSON array
						}
@@ -216,21 +197,21 @@ public class PushPlugin extends CordovaPlugin {
							try
							{
								JSONArray json2 = new JSONArray(strValue);
								jsondata.put(key, json2);
								additionalData.put(key, json2);
							}
							catch (Exception e)
							{
								jsondata.put(key, value);
								additionalData.put(key, value);
							}
						}
						else
						{
							jsondata.put(key, value);
							additionalData.put(key, value);
						}
					}
				}
			} // while
			json.put("payload", jsondata);
			json.put("additionalData", additionalData);

			Log.v(LOG_TAG, "extrasToJSON: " + json.toString());

+1 −1
Changes for www/push.js: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ var PushNotification = function(options) {
    var success = function(result) {
        if (result && typeof result.registrationId !== 'undefined') {
            that.emit('registration', result);
        } else if (result && typeof result.notification !== 'undefined') {
        } else if (result && typeof result.message !== 'undefined') {
            that.emit('notification', result);
        }
    };