Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Commit

Permalink
Issue #74: Implement Inbox style for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
macdonst committed Aug 25, 2015
1 parent fb5998d commit eddf785
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 55 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion example/server/pushGCM.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
108 changes: 54 additions & 54 deletions src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> messageList = new ArrayList();

public void setNotification(String message){

Expand All @@ -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);
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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("<missing message content>");
}

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

Expand Down Expand Up @@ -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;
}
}

0 comments on commit eddf785

Please sign in to comment.