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

Commit da71c32

Browse files
committed
✨ Allow sorting inbox notifications in ascending order
1 parent e5370c4 commit da71c32

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

docs/PAYLOAD.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,51 @@ You will get an inbox view so you can display multiple notifications in a single
966966

967967
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.
968968

969+
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:
970+
971+
```json
972+
{
973+
"registration_ids": ["my device id"],
974+
"data": {
975+
"title": "My Title",
976+
"message": "My first message",
977+
"style": "inbox",
978+
"inbox-order": "asc",
979+
"summaryText": "There are %n% notifications"
980+
}
981+
}
982+
```
983+
984+
Here is an example using fcm-node that sends the above JSON:
985+
986+
```javascript
987+
const FCM = require('fcm-node');
988+
// Replace these with your own values.
989+
const apiKey = 'replace with API key';
990+
const deviceID = 'my device id';
991+
const fcm = new FCM(apiKey);
992+
993+
const message = {
994+
to: deviceID,
995+
data: {
996+
title: 'My Title',
997+
message: 'My second message',
998+
style: 'inbox',
999+
'inbox-order': 'asc',
1000+
summaryText: 'There are %n% notifications'
1001+
}
1002+
};
1003+
1004+
fcm.send(message, (err, response) => {
1005+
if (err) {
1006+
console.log(err);
1007+
console.log('Something has gone wrong!');
1008+
} else {
1009+
console.log('Successfully sent with response: ', response);
1010+
}
1011+
});
1012+
```
1013+
9691014
## Action Buttons
9701015

9711016
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
21282173
"custom_var_2:" "custom value here" /* Retrieved on app as data.additionalData.custom_var_2 */
21292174
},
21302175
/* Forces FCM silent push notifications to be triggered in the foreground of your iOS device. */
2131-
"content_available": true
2176+
"content_available": true
21322177
}
21332178
```
21342179
*Doc modification came in response to @andreszs - Issue [#2449](https://github.com/phonegap/phonegap-plugin-push/issues/2449).
21352180
2136-
** 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.
2181+
** 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.
21372182
21382183
More information on how to send push notifications using the FCM HTTP protocol and payload details can be found here:
21392184

src/android/com/adobe/phonegap/push/FCMService.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,8 +677,17 @@ private void setNotificationMessage(int notId, Bundle extras, NotificationCompat
677677
NotificationCompat.InboxStyle notificationInbox = new NotificationCompat.InboxStyle()
678678
.setBigContentTitle(fromHtml(extras.getString(TITLE))).setSummaryText(fromHtml(stacking));
679679

680-
for (int i = messageList.size() - 1; i >= 0; i--) {
681-
notificationInbox.addLine(fromHtml(messageList.get(i)));
680+
String order = extras.getString(INBOX_ORDER, ORDER_DESC);
681+
682+
if (ORDER_ASC.equals(order)) {
683+
// Display last 4 messages.
684+
for (int i = Math.max(0, messageList.size() - 4); i < messageList.size(); i++) {
685+
notificationInbox.addLine(fromHtml(messageList.get(i)));
686+
}
687+
} else {
688+
for (int i = messageList.size() - 1; i >= 0; i--) {
689+
notificationInbox.addLine(fromHtml(messageList.get(i)));
690+
}
682691
}
683692

684693
mBuilder.setStyle(notificationInbox);

src/android/com/adobe/phonegap/push/PushConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public interface PushConstants {
3939
public static final String STYLE_INBOX = "inbox";
4040
public static final String STYLE_PICTURE = "picture";
4141
public static final String STYLE_TEXT = "text";
42+
public static final String INBOX_ORDER = "inbox-order";
43+
public static final String ORDER_ASC = "asc";
44+
public static final String ORDER_DESC = "desc";
4245
public static final String BADGE = "badge";
4346
public static final String INITIALIZE = "init";
4447
public static final String SUBSCRIBE = "subscribe";

0 commit comments

Comments
 (0)