diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index caee145e..2235bcbe 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -19,7 +19,7 @@ public class GCMIntentService extends GCMBaseIntentService { private static final String TAG = "GCMIntentService"; - + public GCMIntentService() { super("GCMIntentService"); } @@ -64,19 +64,30 @@ protected void onMessage(Context context, Intent intent) { if (extras != null) { // if we are in the foreground, just surface the payload, else post it to the statusbar - if (PushPlugin.isInForeground()) { + if (PushPlugin.isInForeground()) { extras.putBoolean("foreground", true); - PushPlugin.sendExtras(extras); + PushPlugin.sendExtras(extras); } else { extras.putBoolean("foreground", false); - - // Send a notification if there is a message - if (extras.getString("message") != null && extras.getString("message").length() != 0) { - createNotification(context, extras); - } - } - } + String message = extras.getString("message"); + + if (message == null) { + // Providers like Parse always send a 'data' as root object, so "Parse" that + try { + JSONObject object_example = new JSONObject(extras.getString("data")); + message = object_example.getString("alert"); + extras.putString("message", message); + } + catch (JSONException e) {} + } + + // Send a notification if there is a message + if (message != null && message.length() != 0) { + createNotification(context, extras); + } + } + } } public void createNotification(Context context, Bundle extras) @@ -89,7 +100,7 @@ public void createNotification(Context context, Bundle extras) notificationIntent.putExtra("pushBundle", extras); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); - + int defaults = Notification.DEFAULT_ALL; if (extras.getString("defaults") != null) { @@ -97,7 +108,7 @@ public void createNotification(Context context, Bundle extras) defaults = Integer.parseInt(extras.getString("defaults")); } catch (NumberFormatException e) {} } - + NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setDefaults(defaults) @@ -119,9 +130,9 @@ public void createNotification(Context context, Bundle extras) if (msgcnt != null) { mBuilder.setNumber(Integer.parseInt(msgcnt)); } - + int notId = 0; - + try { notId = Integer.parseInt(extras.getString("notId")); } @@ -131,20 +142,20 @@ public void createNotification(Context context, Bundle extras) catch(Exception e) { Log.e(TAG, "Number format exception - Error parsing Notification ID" + e.getMessage()); } - + mNotificationManager.notify((String) appName, notId, mBuilder.build()); } - + private static String getAppName(Context context) { - CharSequence appName = + CharSequence appName = context .getPackageManager() .getApplicationLabel(context.getApplicationInfo()); - + return (String)appName; } - + @Override public void onError(Context context, String errorId) { Log.e(TAG, "onError - errorId: " + errorId);