Commit 0293676a authored by Simon MacDonald's avatar Simon MacDonald
Browse files

Issue #54: GCM notifications do not work due to wrong notification payload keys

parent 56682f6a
Loading
Loading
Loading
Loading
+120 −66
Changes for src/android/com/adobe/phonegap/push/GCMIntentService.java: 120 added lines, 66 removed lines.
Original line number Diff line number Diff line
@@ -77,7 +77,8 @@ public class GCMIntentService extends GCMBaseIntentService {
                extras.putBoolean("foreground", false);

                // Send a notification if there is a message
                if (extras.getString("message") != null && extras.getString("message").length() != 0) {
                String message = this.getMessageText(extras);
                if (message != null && message.length() != 0) {
                    createNotification(context, extras);
                }
            }
@@ -129,17 +130,7 @@ public class GCMIntentService extends GCMBaseIntentService {
         * To use, add the `iconColor` key to plugin android options
         *
         */
        int iconColor = 0;
        if (localIconColor != null) {
            try {
                iconColor = Color.parseColor(localIconColor);
            } catch (IllegalArgumentException e) {
                Log.e(LOG_TAG, "couldnt parse color from android options");
            }
        }
        if (iconColor != 0) {
            mBuilder.setColor(iconColor);
        }
        setNotificationIconColor(extras.getString("color"), mBuilder, localIconColor);

        /*
         * Notification Icon
@@ -153,16 +144,7 @@ public class GCMIntentService extends GCMBaseIntentService {
         * If no resource is found, falls
         *
         */
        int iconId = 0;
        if (localIcon != null) {
            iconId = resources.getIdentifier(localIcon, "drawable", packageName);
            Log.d(LOG_TAG, "using icon from plugin options");
        }
        if (iconId == 0) {
            Log.d(LOG_TAG, "no icon resource found - using application icon");
            iconId = context.getApplicationInfo().icon;
        }
        mBuilder.setSmallIcon(iconId);
        setNotificationSmallIcon(context, extras, packageName, resources, mBuilder, localIcon);

        /*
         * Notification Large-Icon
@@ -176,6 +158,95 @@ public class GCMIntentService extends GCMBaseIntentService {
         * - if none, we don't set the large icon
         *
         */
        setNotificationLargeIcon(extras, packageName, resources, mBuilder);

        /*
         * Notification Sound
         */
        if (soundOption) {
            setNotificationSound(context, extras, mBuilder);
        }

        /*
         * Notification message
         */
        setNotificationMessage(extras, mBuilder);

        /*
         * Notification count
         */
        setNotificationCount(extras, mBuilder);
        
        int notId = 0;
        
        try {
            notId = Integer.parseInt(extras.getString("notId"));
        }
        catch(NumberFormatException e) {
            Log.e(LOG_TAG, "Number format exception - Error parsing Notification ID: " + e.getMessage());
        }
        catch(Exception e) {
            Log.e(LOG_TAG, "Number format exception - Error parsing Notification ID" + e.getMessage());
        }
        
        mNotificationManager.notify((String) appName, notId, mBuilder.build());
    }

    private void setNotificationCount(Bundle extras, NotificationCompat.Builder mBuilder) {
        String msgcnt = extras.getString("msgcnt");
        if (msgcnt == null) {
            msgcnt = extras.getString("badge");
        }
        if (msgcnt != null) {
            mBuilder.setNumber(Integer.parseInt(msgcnt));
        }
    }

    private void setNotificationMessage(Bundle extras, NotificationCompat.Builder mBuilder) {
        NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();

        String message = getMessageText(extras);
        if (message != null) {
            mBuilder.setContentText(message);

            bigText.bigText(message);
            bigText.setBigContentTitle(extras.getString("title"));

            String summaryText = extras.getString("summaryText");
            if (summaryText != null) {
                bigText.setSummaryText(summaryText);
            }

            mBuilder.setStyle(bigText);
        } else {
            mBuilder.setContentText("<missing message content>");
        }
    }

    private String getMessageText(Bundle extras) {
        String message = extras.getString("message");
        if (message == null) {
            message = extras.getString("body");
        }
        return message;
    }

    private void setNotificationSound(Context context, Bundle extras, NotificationCompat.Builder mBuilder) {
        String soundname = extras.getString("soundname");
        if (soundname == null) {
            soundname = extras.getString("sound");
        }
        if (soundname != null) {
            Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                        + "://" + context.getPackageName() + "/raw/" + soundname);
            Log.d(LOG_TAG, sound.toString());
            mBuilder.setSound(sound);
        } else {
            mBuilder.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI);
        }
    }

    private void setNotificationLargeIcon(Bundle extras, String packageName, Resources resources, NotificationCompat.Builder mBuilder) {
        String gcmLargeIcon = extras.getString("image"); // from gcm
        if (gcmLargeIcon != null) {
            if (gcmLargeIcon.startsWith("http://") || gcmLargeIcon.startsWith("https://")) {
@@ -202,62 +273,45 @@ public class GCMIntentService extends GCMBaseIntentService {
                }
            }
        }
    }

        /*
         * Notification Sound
         */
        if (soundOption) {
            String soundname = extras.getString("soundname");
            if (soundname == null) {
                soundname = extras.getString("sound");
    private void setNotificationSmallIcon(Context context, Bundle extras, String packageName, Resources resources, NotificationCompat.Builder mBuilder, String localIcon) {
        int iconId = 0;
        String icon = extras.getString("icon");
        if (icon != null) {
            iconId = resources.getIdentifier(icon, "drawable", packageName);
            Log.d(LOG_TAG, "using icon from plugin options");
        }
            if (soundname != null) {
                Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                            + "://" + context.getPackageName() + "/raw/" + soundname);
                Log.d(LOG_TAG, sound.toString());
                mBuilder.setSound(sound);
            } else {
                mBuilder.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI);
        else if (localIcon != null) {
            iconId = resources.getIdentifier(localIcon, "drawable", packageName);
            Log.d(LOG_TAG, "using icon from plugin options");
        }
        if (iconId == 0) {
            Log.d(LOG_TAG, "no icon resource found - using application icon");
            iconId = context.getApplicationInfo().icon;
        }
        
		NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();  

		String message = extras.getString("message");
		String summaryText = extras.getString("summaryText");
		if (message != null) {
			mBuilder.setContentText(message);

			bigText.bigText(message);
			bigText.setBigContentTitle(extras.getString("title"));

			if (summaryText != null) {
				bigText.setSummaryText(summaryText);
        mBuilder.setSmallIcon(iconId);
    }

			mBuilder.setStyle(bigText);
		} else {
			mBuilder.setContentText("<missing message content>");
    private void setNotificationIconColor(String color, NotificationCompat.Builder mBuilder, String localIconColor) {
        int iconColor = 0;
        if (color != null) {
            try {
                iconColor = Color.parseColor(color);
            } catch (IllegalArgumentException e) {
                Log.e(LOG_TAG, "couldnt parse color from android options");
            }

        String msgcnt = extras.getString("msgcnt");
        if (msgcnt != null) {
            mBuilder.setNumber(Integer.parseInt(msgcnt));
        }
        
        int notId = 0;
        
        else if (localIconColor != null) {
            try {
            notId = Integer.parseInt(extras.getString("notId"));
                iconColor = Color.parseColor(localIconColor);
            } catch (IllegalArgumentException e) {
                Log.e(LOG_TAG, "couldnt parse color from android options");
            }
        catch(NumberFormatException e) {
            Log.e(LOG_TAG, "Number format exception - Error parsing Notification ID: " + e.getMessage());
        }
        catch(Exception e) {
            Log.e(LOG_TAG, "Number format exception - Error parsing Notification ID" + e.getMessage());
        if (iconColor != 0) {
            mBuilder.setColor(iconColor);
        }
        
        mNotificationManager.notify((String) appName, notId, mBuilder.build());
    }

    public Bitmap getBitmapFromURL(String strURL) {
+5 −3
Changes for src/android/com/adobe/phonegap/push/PushPlugin.java: 5 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -184,9 +184,11 @@ public class PushPlugin extends CordovaPlugin {
                }
                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")) {
                } else if (key.equals("message") || key.equals("body")) {
                    json.put("message", value);
                } else if (key.equals("title")) {
                    json.put("title", value);
                } else if (key.equals("msgcnt") || key.equals("badge")) {
                    json.put("count", value);
                } else if (key.equals("soundname") || key.equals("sound")) {
                    json.put("sound", value);