android уведомление не отображается снова, если я отменил его - PullRequest
0 голосов
/ 30 апреля 2020

Вот весь мой код.

    private List<Pair<String, Integer>> indices = new ArrayList<>();
    private NotificationChannel notificationChannel = null;
    private String channelID = "1";
    private String channelName = "Messages from user";
    private int indexStart = 1000;
    private List<Pair<String, Integer>> indices = new ArrayList<>();
    private String currChatUserId = null;

    public void setCurrChatTargetUserId(String currChatTargetUserId) {
        this.currChatUserId = currChatTargetUserId;
    }

    private int buildNotifyIndex(String userId) {
        try {
            return Integer.parseInt(userId);
        } catch (NumberFormatException e) {
            Log.w(this.getClass().getSimpleName(), "userId is not a integer.");
        }
        Pair<String, Integer> userIdIndex = findNotifyIndexByUserId(userId);
        if (userIdIndex != null) {
            return userIdIndex.second;
        }
        indexStart++;
        indices.add(Pair.create(userId, indexStart));
        return indexStart;
    }

    private Pair<String, Integer> findNotifyIndexByUserId(String userId) {
        for (Pair<String, Integer> index : indices) {
            if (!TextUtils.isEmpty(index.first) && index.first.equals(userId)) {
                return index;
            }
        }
        return null;
    }

    private NotificationChannel getRongCloudNotificationChannel() {
        if (notificationChannel == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
            channel.setShowBadge(true);
            channel.enableLights(true);
            channel.enableVibration(true);
            channel.setDescription("Rong cloud message from users.");
//            channel.setVibrationPattern(new long[]{200});
            channel.setImportance(NotificationManager.IMPORTANCE_HIGH);
            return channel;
        }
        return notificationChannel;
    }
    private void showRongCloudNotification(Context context, String targetUserId, String name, String content) {
        if (!maybeClearNotificationByUserId(context, targetUserId)) {
            return;
        }
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelID);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle(name);
        builder.setContentText(content);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(getRongCloudNotificationChannel());
            }
        }
        builder.setChannelId(channelID);
        Notification notification = builder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        builder.setPublicVersion(notification);
        if (notificationManager != null) {
            notificationManager.notify(buildNotifyIndex(targetUserId), notification);
        }
    }

    private boolean maybeClearNotificationByUserId(Context context, String userId) {
        if (userId.equals(currChatUserId)) {
            Pair<String, Integer> notifyId = findNotifyIndexByUserId(userId);
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (notifyId != null) {
                notificationManager.cancel(notifyId.second);
            }
            indices.remove(notifyId);
            return false;
        }
        return true;
    }

Если я отменил свое уведомление и повторно вызвал функцию showRongCloudNotification(...),

Это странное поведение:

На samsung s7 edge API-28: Это может отобразиться снова;

На эмуляторе API-29: он не может отобразиться снова, но принудительно остановит приложение и перезапустит его.

На Xiaomi mix2s API-29: он не мог отобразиться снова, хотя перезапустите прогресс приложения.

show again означает уведомление в списке уведомлений, но без всплывающего окна.

Я хочу снова показать, как работает Samsung, как я могу это сделать?

...