如何修复状态栏中的通知?

时间:2019-10-23 00:56:26

标签: java android

我正在开发一种可以显示三个通知的秒表。当我更新通知时(每秒),所有通知都在通知栏中显示,并在它们之间切换。

有什么方法可以防止它们在每次更新时都移到顶部吗?

使用以下代码,我从不同的服务创建通知,每个服务具有唯一的CHANNEL_ID和NOTIFY_ID。

   private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,getResources().getText(R.string.stopwatch_channel),
                    NotificationManager.IMPORTANCE_LOW
            );

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }

    private Notification sendNotification(){
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle(getResources().getText(R.string.stopwatch)+" · "+String.format("%02d:%02d:%02d",hour,min,secs))
                .setSmallIcon(R.drawable.ic_stopwatch_24dp)
                .setContentIntent(pendingIntent)
                .setShowWhen(false)
                .build();

        return notification;
    }

    private void updateNotification() {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(NOTIFY_ID, sendNotification());
    }

0 个答案:

没有答案