Уведомления Android не могут полностью сортироваться по лексикографическому порядку при группировке с помощью sortkeyAndroid

Форум для тех, кто программирует под Android
Ответить Пред. темаСлед. тема
Anonymous
 Уведомления Android не могут полностью сортироваться по лексикографическому порядку при группировке с помощью sortkey

Сообщение Anonymous »

Я тестирую сортировку уведомлений Android с помощью группировки и sortKey, порядок Android, отображаемый в области уведомлений, кажется мне неверным в соответствии с лексикографическим порядком.Ссылка на официальный документ Android о sortKey.

Уведомления будут сортироваться лексикографически с использованием этого значения.

В моем тесте я разместил 6 уведомлений с одним и тем же каналом = "channelId", groupId = "group_id_2" и различными сортировочными ключами = ["1", "10", "11", "111". ", "2", "3"], публикуя одновременно.
Результат, который я ожидаю, — [1, 10, 11, 111, 2, 3], согласно лексикографический порядок. Но в трее отображается [10, 111, 11, 1, 2, 3], скриншот здесь. Правильно ли это с учетом лексикографического порядка?
Код такой, как показано ниже, скриншот в трее здесь

Код: Выделить всё

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);
}
}
}

Заранее спасибо за помощь!

Подробнее здесь: https://stackoverflow.com/questions/781 ... -when-grou
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Android»