我正在开发一款闹钟应用程序,我面临的问题是,当一个闹钟响起并且新闹钟启动时,新闹钟的通知会取代现有的通知。我希望两个通知同时显示,而新通知不会取消前一个通知。
服务代码
package com.example.alarmclock.service
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.media.AudioManager
import android.os.IBinder
import androidx.core.app.NotificationCompat
import com.example.alarmclock.R
import com.example.alarmclock.common.AlarmSound
import com.example.alarmclock.common.Constant.EXTRA_ID
import com.example.alarmclock.common.Constant.EXTRA_MESSAGE
import com.example.alarmclock.common.Constant.EXTRA_SOUND
import com.example.alarmclock.common.Constant.EXTRA_TIME
import com.example.alarmclock.common.Constant.NOTIFICATION_CHANNEL_ID
import com.example.alarmclock.common.formatCurrentTime
import com.example.alarmclock.presentation.alarm_challenge_screen.ui.AlarmChallengeScreen
import java.util.Calendar
class AlarmsService : Service() {
private var alarmSound: AlarmSound? = null
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val id = intent?.getIntExtra(EXTRA_ID, 0) ?: return START_NOT_STICKY
val message = intent.getStringExtra(EXTRA_MESSAGE) ?: ""
val soundResId = intent.getIntExtra(EXTRA_SOUND, R.raw.default_sound)
val time = Calendar.getInstance().formatCurrentTime()
displayNotification(
id,
this,
"Alarm Triggered : $time",
"Message: $message",
createAlarmChallengeScreenIntent(this, time, id)
)
alarmSound?.release()
alarmSound = AlarmSound(this, soundResId).apply { startSound() }
return START_STICKY
}
override fun onBind(intent: Intent): IBinder? {
return null
}
override fun onDestroy() {
alarmSound?.release()
super.onDestroy()
}
private fun displayNotification(
id: Int,
context: Context,
title: String,
description: String,
openAlarmChallengeScreenIntent: PendingIntent
) {
val notification = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID).apply {
setSmallIcon(R.drawable.baseline_alarm_add_24)
setContentTitle(title)
setContentText(description)
setPriority(NotificationCompat.PRIORITY_HIGH)
setCategory(NotificationCompat.CATEGORY_ALARM)
setOngoing(true)
setFullScreenIntent(openAlarmChallengeScreenIntent, true)
}.build()
startForeground(id, notification)
}
private fun createAlarmChallengeScreenIntent(
context: Context,
time: String,
id: Int,
): PendingIntent {
val intent = Intent(context, AlarmChallengeScreen::class.java).apply {
putExtra(EXTRA_TIME, time)
putExtra(EXTRA_ID, id)
}
val requestCode = "openAlarmChallengeScreen$time${id}".hashCode()
return PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_MUTABLE)
}
}
问题可能出在 startForeground(id, notification) 上。即使我使用不同的通知 ID,问题仍然存在,当前通知被取消并显示新通知。
要显示多个通知而不替换旧通知,您需要提供唯一的 ID。
val uniqueId = System.currentTimeMillis().toInt()
val notification = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID).apply {
setSmallIcon(R.drawable.baseline_alarm_add_24)
setContentTitle(title)
setContentText(description)
setPriority(NotificationCompat.PRIORITY_HIGH)
setCategory(NotificationCompat.CATEGORY_ALARM)
setOngoing(true)
setFullScreenIntent(openAlarmChallengeScreenIntent, true)
}.build()
startForeground(uniqueId, notification)