Commit eddf785c authored by Simon MacDonald's avatar Simon MacDonald
Browse files

Issue #74: Implement Inbox style for Android

parent fb5998d9
Loading
Loading
Loading
Loading
+34 −0
Changes for README.md: 34 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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:
+1 −1
Changes for example/server/pushGCM.rb: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -4,6 +4,6 @@ GCM.host = 'https://android.googleapis.com/gcm/send'
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)
+31 −31
Changes for src/android/com/adobe/phonegap/push/GCMIntentService.java: 31 added lines, 31 removed lines.
Original line number Diff line number Diff line
@@ -37,7 +37,9 @@ import java.util.ArrayList;
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){

@@ -196,17 +198,7 @@ public class GCMIntentService extends GCMBaseIntentService {
         */
        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,42 +241,34 @@ public class GCMIntentService extends GCMBaseIntentService {
        NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
        String message = getMessageText(extras);

        int group = 0;
        try {
            group = Integer.parseInt(extras.getString("group"));
            if(group == 1){
        String style = extras.getString("style", STYLE_TEXT);
        if(STYLE_INBOX.equals(style)){
            setNotification(message);

            mBuilder.setContentText(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");
                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(Object noticationMensage : messageList){
                       notificationInbox.addLine(Html.fromHtml(noticationMensage.toString()));
                for (int i=messageList.size()-1; i >= 0; i--) {
                    notificationInbox.addLine(Html.fromHtml(messageList.get(i)));
                }

                mBuilder.setStyle(notificationInbox);
                    mBuilder.setLargeIcon(null);
                }
            }
            }
        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(message);
                mBuilder.setContentText(Html.fromHtml(message));

                bigText.bigText(message);
                bigText.setBigContentTitle(extras.getString("title"));
@@ -299,6 +283,7 @@ public class GCMIntentService extends GCMBaseIntentService {
                mBuilder.setContentText("<missing message content>");
            }
        }
    }

    private String getMessageText(Bundle extras) {
        String message = extras.getString("message");
@@ -420,4 +405,19 @@ public class GCMIntentService extends GCMBaseIntentService {
        }
    }

    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