From da71c3223d39dfb3d71a6c8e97b0587bb67cdd0d Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 4 Feb 2019 09:04:41 +0100 Subject: [PATCH] :sparkles: Allow sorting inbox notifications in ascending order --- docs/PAYLOAD.md | 49 ++++++++++++++++++- .../com/adobe/phonegap/push/FCMService.java | 13 ++++- .../adobe/phonegap/push/PushConstants.java | 3 ++ 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/docs/PAYLOAD.md b/docs/PAYLOAD.md index d06085340..995b7c0e1 100644 --- a/docs/PAYLOAD.md +++ b/docs/PAYLOAD.md @@ -966,6 +966,51 @@ You will get an inbox view so you can display multiple notifications in a single If you use `%n%` in the `summaryText` of the JSON coming down from FCM it will be replaced by the number of messages that are currently in the queue. +By default, inbox notifications are ordered in descending order (last notification is displayed first). You can use the `inbox-order` parameter to change the order to ascending: + +```json +{ + "registration_ids": ["my device id"], + "data": { + "title": "My Title", + "message": "My first message", + "style": "inbox", + "inbox-order": "asc", + "summaryText": "There are %n% notifications" + } +} +``` + +Here is an example using fcm-node that sends the above JSON: + +```javascript +const FCM = require('fcm-node'); +// Replace these with your own values. +const apiKey = 'replace with API key'; +const deviceID = 'my device id'; +const fcm = new FCM(apiKey); + +const message = { + to: deviceID, + data: { + title: 'My Title', + message: 'My second message', + style: 'inbox', + 'inbox-order': 'asc', + summaryText: 'There are %n% notifications' + } +}; + +fcm.send(message, (err, response) => { + if (err) { + console.log(err); + console.log('Something has gone wrong!'); + } else { + console.log('Successfully sent with response: ', response); + } +}); +``` + ## Action Buttons Your notification can include a maximum of three action buttons. You register the event callback name for each of your actions, then when a user clicks on one of notification's buttons, the event corresponding to that button is fired and the listener you have registered is invoked. For instance, here is a setup with two actions `emailGuests` and `snooze`. @@ -2128,12 +2173,12 @@ On iOS, using the FCM app server protocol, if you are trying to send a silent pu "custom_var_2:" "custom value here" /* Retrieved on app as data.additionalData.custom_var_2 */ }, /* Forces FCM silent push notifications to be triggered in the foreground of your iOS device. */ - "content_available": true + "content_available": true } ``` *Doc modification came in response to @andreszs - Issue [#2449](https://github.com/phonegap/phonegap-plugin-push/issues/2449). -** IMPORTANT: When using the content_available field, Android payload issues may occur. [Read here](../docs/PAYLOAD.md#user-content-use-of-content_available-true) Make sure you separate your Android/iOS server payloads to mitigate any problems that may arise. +** IMPORTANT: When using the content_available field, Android payload issues may occur. [Read here](../docs/PAYLOAD.md#user-content-use-of-content_available-true) Make sure you separate your Android/iOS server payloads to mitigate any problems that may arise. More information on how to send push notifications using the FCM HTTP protocol and payload details can be found here: diff --git a/src/android/com/adobe/phonegap/push/FCMService.java b/src/android/com/adobe/phonegap/push/FCMService.java index db1738a72..29b259f7c 100644 --- a/src/android/com/adobe/phonegap/push/FCMService.java +++ b/src/android/com/adobe/phonegap/push/FCMService.java @@ -677,8 +677,17 @@ private void setNotificationMessage(int notId, Bundle extras, NotificationCompat NotificationCompat.InboxStyle notificationInbox = new NotificationCompat.InboxStyle() .setBigContentTitle(fromHtml(extras.getString(TITLE))).setSummaryText(fromHtml(stacking)); - for (int i = messageList.size() - 1; i >= 0; i--) { - notificationInbox.addLine(fromHtml(messageList.get(i))); + String order = extras.getString(INBOX_ORDER, ORDER_DESC); + + if (ORDER_ASC.equals(order)) { + // Display last 4 messages. + for (int i = Math.max(0, messageList.size() - 4); i < messageList.size(); i++) { + notificationInbox.addLine(fromHtml(messageList.get(i))); + } + } else { + for (int i = messageList.size() - 1; i >= 0; i--) { + notificationInbox.addLine(fromHtml(messageList.get(i))); + } } mBuilder.setStyle(notificationInbox); diff --git a/src/android/com/adobe/phonegap/push/PushConstants.java b/src/android/com/adobe/phonegap/push/PushConstants.java index 9b4656a00..f7d76a793 100644 --- a/src/android/com/adobe/phonegap/push/PushConstants.java +++ b/src/android/com/adobe/phonegap/push/PushConstants.java @@ -39,6 +39,9 @@ public interface PushConstants { public static final String STYLE_INBOX = "inbox"; public static final String STYLE_PICTURE = "picture"; public static final String STYLE_TEXT = "text"; + public static final String INBOX_ORDER = "inbox-order"; + public static final String ORDER_ASC = "asc"; + public static final String ORDER_DESC = "desc"; public static final String BADGE = "badge"; public static final String INITIALIZE = "init"; public static final String SUBSCRIBE = "subscribe";