From eddf785cbac194481427123d4f6fdbd5a4567ff2 Mon Sep 17 00:00:00 2001 From: Simon MacDonald Date: Mon, 24 Aug 2015 21:15:41 -0400 Subject: [PATCH] Issue #74: Implement Inbox style for Android --- README.md | 34 ++++++ example/server/pushGCM.rb | 2 +- .../adobe/phonegap/push/GCMIntentService.java | 108 +++++++++--------- 3 files changed, 89 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 9ff54aeb3..970bfb673 100644 --- a/README.md +++ b/README.md @@ -314,6 +314,40 @@ and: You will only see both "Push number 1" and "Push number 2" in the shade. +### Inbox Stacking ### + +A better alternative to stacking your notifications is to use the inbox style to have up to 8 lines of notification text in a single notification. If you send the following JSON from GCM you will see: + +```javascript +{ + title:"My Title", + message: "My first message", + style: "inbox", + summaryText: "There are %n% notifications" +} +``` + +It will produce a normal looking notification: + +![2015-08-25 14 11 27](https://cloud.githubusercontent.com/assets/353180/9468840/c9c5d43a-4b11-11e5-814f-8dc995f47830.png) + +But, if you follow it up with subsequent notifications like: + +```javascript +{ + title:"My Title", + message: "My second message", + style: "inbox", + summaryText: "There are %n% notifications" +} +``` + +You will get an inbox view so you can display multiple notifications in a single panel. + +![2015-08-25 14 01 35](https://cloud.githubusercontent.com/assets/353180/9468727/2d658bee-4b11-11e5-90fa-248d54c8f3f6.png) + +If you use `%n%` in the `summaryText` of the JSON coming down from GCM it will be replaced by the number of messages that are currently in the queue. + ### Action Buttons Your notification can include action buttons. If you wish to include an icon along with the button name they must be placed in the `res/drawable` directory of your Android project. Then you can send the following JSON from GCM: diff --git a/example/server/pushGCM.rb b/example/server/pushGCM.rb index d4a4fb7bc..e1efce5ee 100644 --- a/example/server/pushGCM.rb +++ b/example/server/pushGCM.rb @@ -4,6 +4,6 @@ GCM.format = :json GCM.key = "API_KEY_GOES_HERE" destination = ["REGISTRATION_ID_GOES_HERE"] -data = {:message => "PhoneGap Build rocks!", :msgcnt => "1", :soundname => "beep.wav", :group => 1, :stacking => "There %n% notifications",} +data = {:message => "PhoneGap Build rocks!", :msgcnt => "1", :soundname => "beep.wav"} GCM.send_notification( destination, data) diff --git a/src/android/com/adobe/phonegap/push/GCMIntentService.java b/src/android/com/adobe/phonegap/push/GCMIntentService.java index eff8152c0..e73500a3a 100644 --- a/src/android/com/adobe/phonegap/push/GCMIntentService.java +++ b/src/android/com/adobe/phonegap/push/GCMIntentService.java @@ -37,7 +37,9 @@ public class GCMIntentService extends GCMBaseIntentService { private static final String LOG_TAG = "PushPlugin_GCMIntentService"; - private static ArrayList messageList = new ArrayList(); + private static final String STYLE_INBOX = "inbox"; + private static final String STYLE_TEXT = "text"; + private static ArrayList messageList = new ArrayList(); public void setNotification(String message){ @@ -55,7 +57,7 @@ public GCMIntentService() { @Override public void onRegistered(Context context, String regId) { - Log.v(LOG_TAG, "onRegistered: "+ regId); + Log.v(LOG_TAG, "onRegistered: " + regId); try { JSONObject json = new JSONObject().put("registrationId", regId); @@ -196,18 +198,8 @@ public void createNotification(Context context, Bundle extras) { */ createActions(extras, mBuilder, resources, packageName); - 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()); - } - + int notId = parseInt("notId", extras); + mNotificationManager.notify((String) appName, notId, mBuilder.build()); } @@ -249,54 +241,47 @@ private void setNotificationMessage(Bundle extras, NotificationCompat.Builder mB NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle(); String message = getMessageText(extras); - int group = 0; - try { - group = Integer.parseInt(extras.getString("group")); - if(group == 1){ - setNotification(message); - - Integer sizeList = messageList.size(); - if(sizeList > 1){ - String sizeListMessage = sizeList.toString(); - String stacking = sizeList+" more"; - if(extras.getString("stacking") != null){ - stacking = extras.getString("stacking"); - stacking = stacking.replace("%n%", sizeListMessage); - } - NotificationCompat.InboxStyle notificationInbox = new NotificationCompat.InboxStyle() - .setBigContentTitle(extras.getString("title")) - .setSummaryText(stacking); + String style = extras.getString("style", STYLE_TEXT); + if(STYLE_INBOX.equals(style)){ + setNotification(message); - for(Object noticationMensage : messageList){ - notificationInbox.addLine(Html.fromHtml(noticationMensage.toString())); - } + mBuilder.setContentText(message); - mBuilder.setStyle(notificationInbox); - mBuilder.setLargeIcon(null); + Integer sizeList = messageList.size(); + if(sizeList > 1){ + String sizeListMessage = sizeList.toString(); + String stacking = sizeList+" more"; + if(extras.getString("summaryText") != null){ + stacking = extras.getString("summaryText"); + stacking = stacking.replace("%n%", sizeListMessage); } + NotificationCompat.InboxStyle notificationInbox = new NotificationCompat.InboxStyle() + .setBigContentTitle(extras.getString("title")) + .setSummaryText(stacking); + + for (int i=messageList.size()-1; i >= 0; i--) { + notificationInbox.addLine(Html.fromHtml(messageList.get(i))); + } + + mBuilder.setStyle(notificationInbox); } - } - catch(NumberFormatException e) { - Log.e(LOG_TAG, "Number format exception - Error parsing Group: " + e.getMessage()); - } - catch(Exception e) { - Log.e(LOG_TAG, "Number format exception - Error parsing Group" + e.getMessage()); - } + } else { + setNotification(""); + if (message != null) { + mBuilder.setContentText(Html.fromHtml(message)); - if (message != null) { - mBuilder.setContentText(message); + bigText.bigText(message); + bigText.setBigContentTitle(extras.getString("title")); - bigText.bigText(message); - bigText.setBigContentTitle(extras.getString("title")); + String summaryText = extras.getString("summaryText"); + if (summaryText != null) { + bigText.setSummaryText(summaryText); + } - String summaryText = extras.getString("summaryText"); - if (summaryText != null) { - bigText.setSummaryText(summaryText); + mBuilder.setStyle(bigText); + } else { + mBuilder.setContentText(""); } - - mBuilder.setStyle(bigText); - } else { - mBuilder.setContentText(""); } } @@ -420,4 +405,19 @@ public void onError(Context context, String errorId) { } } + private int parseInt(String value, Bundle extras) { + int retval = 0; + + try { + retval = Integer.parseInt(extras.getString(value)); + } + catch(NumberFormatException e) { + Log.e(LOG_TAG, "Number format exception - Error parsing " + value + ": " + e.getMessage()); + } + catch(Exception e) { + Log.e(LOG_TAG, "Number format exception - Error parsing " + value + ": " + e.getMessage()); + } + + return retval; + } } \ No newline at end of file