I am testing sorting android notifications by using grouping and sortKey, the order android shown in the tray seems incorrect to me according to lexicographical order.
Referring to android official doc about sortKey
Notifications will be sorted lexicographically using this value
In my test, I posted 6 notifications w/ same channel = "channelId", groupId = "group_id_2", and various sortKeys = ["1", "10", "11", "111", "2", "3"], posting at the same time.
The result I am expecting is [1, 10, 11, 111, 2, 3], according to lexicographical oder. But the tray shows [10, 111, 11, 1, 2, 3], screenshot is here. Is it correct based on lexicographical order?
The code is as below, tray screenshot is here
package com.example.tray;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.core.app.NotificationCompat;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
public class MainActivity extends AppCompatActivity {
private final static String NOTIFICATION_CHANNEL_ID = "channel_id_";
private final static String NOTIFICATION_CHANNEL_ID_2 = NOTIFICATION_CHANNEL_ID + "2";
private final static String NOTIFICATION_CHANNEL_NAME = "channel_name_";
private final static String NOTIFICATION_CHANNEL_NAME_2 = NOTIFICATION_CHANNEL_NAME + "2";
private final static String NOTIFICATION_CHANNEL_DESCRIPTION =
"channel description";
private final static String NOTIFICATION_GROUP_ID_2 = "group_id_" + "2";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupNotificationChannelAndSentPush();
}
private void setupNotificationChannelAndSentPush() {
//Create channel 2
NotificationChannel notificationChannel2 = createPushChannel(NOTIFICATION_CHANNEL_ID_2,
NOTIFICATION_CHANNEL_NAME_2);
NotificationManager nm = getSystemService(NotificationManager.class);
nm.createNotificationChannel(notificationChannel2);
//Post 6 push to Channel_2, Group_2
for (int i = 0; i < 6; i++) {
int notificationId = i;
@Nullable String sortKey = null;
switch (i) {
case 0:
sortKey = "1";
break;
case 1:
sortKey = "10";
break;
case 2:
sortKey = "11";
break;
case 3:
sortKey = "111";
break;
case 4:
sortKey = "2";
break;
case 5:
sortKey = "3";
break;
}
Notification notification =
new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID_2)
.setGroup(NOTIFICATION_GROUP_ID_2)
.setSmallIcon(R.drawable.ic_launcher_background)
.setSortKey(sortKey)
.setContentTitle("Notification Title " + i)
.setContentText(getContent(notificationId,
NOTIFICATION_CHANNEL_ID_2, NOTIFICATION_GROUP_ID_2, sortKey))
.build();
// if notificationId is the same, then that means replace existing one
nm.notify(notificationId, notification);
sleep();
}
}
// Create push channel, priority = default
private NotificationChannel createPushChannel(String notificationChannelId,
String notificationChannelName) {
NotificationChannel notificationChannel =
new NotificationChannel(notificationChannelId, notificationChannelName,
NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription(NOTIFICATION_CHANNEL_DESCRIPTION);
notificationChannel.enableLights(true);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
return notificationChannel;
}
@SuppressLint("DefaultLocale")
private String getContent(int notificationId, String channelId, String groupId, String sortKey) {
return String.format("notifId:%d, channelId:%s, groupId:%s, sortKey:%s", notificationId,
channelId, groupId, sortKey);
}
private void sleep() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Thanks for helping beforehand!