Commit 60cb9c77 authored by Bob Easterday's avatar Bob Easterday
Browse files

send entire payload to JS app

parent b7a25691
Loading
Loading
Loading
Loading
+20 −4
Changes for Example/www/index.html: 20 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -16,6 +16,22 @@
            function onDeviceReady() {
                $("#app-status-ul").append('<li>deviceready event received</li>');
                
				document.addEventListener("backbutton", function(e)
				{
                	$("#app-status-ul").append('<li>backbutton event received</li>');
  					
      				if( $("#home").length > 0)
					{
						e.preventDefault();
						pushNotification.unregister(successHandler, errorHandler);
						navigator.app.exitApp();
					}
					else
					{
						navigator.app.backHistory();
					}
				}, false);
			
				try 
				{ 
                	pushNotification = window.plugins.pushNotification;
@@ -37,7 +53,6 @@
            
            // handle APNS notifications for iOS
            function onNotificationAPN(e) {
                
               if (e.foreground) {
                    $("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');
                    
@@ -94,8 +109,8 @@
						else	// otherwise we were launched because the user touched a notification in the notification tray.
							$("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
							
						$("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.message + '</li>');
						$("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.msgcnt + '</li>');
						$("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
						$("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
                    break;
                    
                    case 'error':
@@ -126,11 +141,12 @@
			document.addEventListener('deviceready', onDeviceReady, true);

         </script>
         
		<div id="home">
			<div id="app-status-div">
				<ul id="app-status-ul">
					<li>Cordova PushNotification Plugin Demo</li>
				</ul>
			</div>
		</div>
    </body>
</html>
 No newline at end of file
+4 −26
Changes for src/android/com/google/android/gcm/GCMIntentService.java: 4 added lines, 26 removed lines.
Original line number Diff line number Diff line
@@ -5,7 +5,9 @@ import java.util.List;

import com.plugin.GCM.PushHandlerActivity;
import com.google.android.gcm.*;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

@@ -80,39 +82,16 @@ public class GCMIntentService extends GCMBaseIntentService {
    if (extras != null)
    {
    	boolean	foreground = this.isInForeground();
    	
    	extras.putBoolean("foreground", foreground);
    	
    	// we can't call into the JS app if we are in the background
    	if (foreground)
    	{
    		try
    		{
    			JSONObject json;
    			json = new JSONObject().put("event", "message");
            	
    			// My application on my host server sends back to "EXTRAS" variables message and msgcnt
    			// Depending on how you build your server app you can specify what variables you want to send
    			json.put("message", extras.getString("message"));
    			json.put("msgcnt", extras.getString("msgcnt"));
    			json.put("soundname", extras.getString("soundname"));
    			json.put("foreground", foreground);

    			Log.v(ME + ":onMessage ", json.toString());

    			PushPlugin.sendJavascript( json );
    		}
    		catch( JSONException e)
    		{
    			Log.e(ME + ":onMessage", "JSON exception");
    		}
    	}
    		PushHandlerActivity.sendToApp(extras);
    	else
    		this.onReceive(context, extras);
    }
  }
  
	// This is called for all notifications, whether the app is in the foreground or the background.
	// This is so we can update any existing notification in the tray, for our app.
	public void onReceive(Context context, Bundle extras)
	{
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
@@ -162,7 +141,6 @@ public class GCMIntentService extends GCMBaseIntentService {
		return false;
	}	


  @Override
  public void onError(Context context, String errorId) {
    Log.e(TAG, "onError - errorId: " + errorId);
+69 −8
Changes for src/android/com/plugin/GCM/PushHandlerActivity.java: 69 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -8,6 +8,9 @@

package com.plugin.GCM;

import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

@@ -35,27 +38,85 @@ public class PushHandlerActivity extends Activity
        {
    		Bundle	originalExtras = extras.getBundle("pushBundle");
    		if (originalExtras != null)
    			PushHandlerActivity.sendToApp(originalExtras);
        }
        finish();
	}
    
    public static void sendToApp(Bundle extras)
    {
		try
		{
			JSONObject json;
			json = new JSONObject().put("event", "message");
        
    				json.put("message", originalExtras.getString("message"));
    				json.put("msgcnt", originalExtras.getString("msgcnt"));
    				json.put("foreground", originalExtras.getBoolean("foreground"));
			JSONObject jsondata = new JSONObject();
			Iterator<String> it = extras.keySet().iterator();
			while (it.hasNext())
			{
				String key = it.next();
				String value = extras.getString(key);	
        	
    				Log.v("PushHandlerActivity:handlePush", json.toString());
				// System data from Android
				if (key.equals("from") || key.equals("collapse_key"))
				{
					json.put(key, value);
				}
				else if (key.equals("foreground"))
				{
					json.put(key, extras.getBoolean("foreground"));
				}
				else
				{
					// Maintain backwards compatibility
					if (key.equals("message") || key.equals("msgcnt") || key.equals("soundname"))
					{
						json.put(key, value);
					}
        		
    				PushPlugin.sendJavascript( json );
					// Try to figure out if the value is another JSON object
					if (value.startsWith("{"))
					{
						try
						{
							JSONObject json2 = new JSONObject(value);
							jsondata.put(key, json2);
						}
    			catch( JSONException e)
						catch (Exception e)
						{
    				Log.e("PushHandlerActivity:handlePush", "JSON exception");
							jsondata.put(key, value);
						}
						// Try to figure out if the value is another JSON array
					}
					else if (value.startsWith("["))
					{
						try
						{
							JSONArray json2 = new JSONArray(value);
							jsondata.put(key, json2);
						}
						catch (Exception e)
						{
							jsondata.put(key, value);
						}
					}
					else
					{
						jsondata.put(key, value);
					}
				}
			} // while
			json.put("payload", jsondata);
        
			Log.v("sendToApp:", json.toString());

			PushPlugin.sendJavascript( json );
			// Send the MESSAGE to the Javascript application
		}
		catch( JSONException e)
		{
			Log.e("sendToApp:", "JSON exception");
		}        	
        finish();
    }

    @Override
+1 −1
Changes for src/android/com/plugin/GCM/PushPlugin.java: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -76,7 +76,7 @@ public class PushPlugin extends Plugin {

      GCMRegistrar.unregister(this.ctx.getContext());
      Log.v(ME + ":" + UNREGISTER, "GCMRegistrar.unregister called ");

      result = new PluginResult(Status.OK);
    }
    else
    {