@@ -13,36 +13,71 @@ import com.example.flutter_alarm_manager_poc.activity.AlarmActivity
1313import com.example.flutter_alarm_manager_poc.model.AlarmItem
1414
1515class AlarmNotificationServiceImpl (private val context : Context ) : AlarmNotificationService {
16- private val CHANNEL_ID = " alarm_channel"
16+ // --- Define unique Channel IDs for each behavior ---
17+ private val CHANNEL_ID_VIBRATE_AND_SOUND = " alarm_channel_vibrate_sound"
18+ private val CHANNEL_ID_VIBRATE_ONLY = " alarm_channel_vibrate_only"
19+ private val CHANNEL_ID_SILENT = " alarm_channel_silent"
20+
1721 private val notificationManager: NotificationManager =
1822 context.getSystemService(Context .NOTIFICATION_SERVICE ) as NotificationManager
1923
2024 init {
21- createNotificationChannel ()
25+ createNotificationChannels ()
2226 }
2327
24- override fun createNotificationChannel () {
28+ override fun createNotificationChannels () {
2529 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
26- val name = " Alarm Channel"
27- val descriptionText = " Channel for Alarm Notifications"
28- val importance = NotificationManager .IMPORTANCE_HIGH
29- val channel = NotificationChannel (CHANNEL_ID , name, importance).apply {
30- description = descriptionText
30+ // 1. Channel for Vibrate and Sound
31+ val vibrateAndSoundChannel = NotificationChannel (
32+ CHANNEL_ID_VIBRATE_AND_SOUND ,
33+ " Alarms (Vibrate and Sound)" ,
34+ NotificationManager .IMPORTANCE_HIGH
35+ ).apply {
36+ description = " Channel for alarms with sound and vibration."
37+ setBypassDnd(true )
38+ lockscreenVisibility = Notification .VISIBILITY_PUBLIC
39+ enableVibration(true )
40+ // Default sound is enabled with IMPORTANCE_HIGH
41+ }
42+
43+ // 2. Channel for Vibrate Only
44+ val vibrateOnlyChannel = NotificationChannel (
45+ CHANNEL_ID_VIBRATE_ONLY ,
46+ " Alarms (Vibrate Only)" ,
47+ NotificationManager .IMPORTANCE_HIGH
48+ ).apply {
49+ description = " Channel for alarms with vibration only."
50+ setBypassDnd(true )
51+ lockscreenVisibility = Notification .VISIBILITY_PUBLIC
52+ enableVibration(true )
53+ setSound(null , null ) // Explicitly disable sound for this channel
54+ }
55+
56+ // 3. Channel for Silent
57+ val silentChannel = NotificationChannel (
58+ CHANNEL_ID_SILENT ,
59+ " Alarms (Silent)" ,
60+ NotificationManager .IMPORTANCE_HIGH // Still high to show as a heads-up notification
61+ ).apply {
62+ description = " Channel for silent alarms."
3163 setBypassDnd(true )
32- setShowBadge(true )
3364 lockscreenVisibility = Notification .VISIBILITY_PUBLIC
34- enableLights( true )
35- enableVibration( true ) // Vibrate is enabled by default on the channel
65+ enableVibration( false )
66+ setSound( null , null )
3667 }
37- notificationManager.createNotificationChannel(channel)
68+
69+ // --- Register all channels with the system ---
70+ notificationManager.createNotificationChannel(vibrateAndSoundChannel)
71+ notificationManager.createNotificationChannel(vibrateOnlyChannel)
72+ notificationManager.createNotificationChannel(silentChannel)
3873 }
3974 }
4075
4176 override fun showNotification (alarmItem : AlarmItem , behavior : String ) {
4277 val fullScreenIntent = Intent (context, AlarmActivity ::class .java).apply {
4378 flags = Intent .FLAG_ACTIVITY_NEW_TASK or
4479 Intent .FLAG_ACTIVITY_NO_USER_ACTION or
45- Intent .FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
80+ Intent .FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
4681
4782 putExtra(" ALARM_ID" , alarmItem.id)
4883 putExtra(" ALARM_MESSAGE" , alarmItem.message)
@@ -55,7 +90,17 @@ class AlarmNotificationServiceImpl(private val context: Context) : AlarmNotifica
5590 PendingIntent .FLAG_UPDATE_CURRENT or PendingIntent .FLAG_IMMUTABLE
5691 )
5792
58- val notificationBuilder = NotificationCompat .Builder (context, CHANNEL_ID )
93+ // --- Determine which channel to use based on the behavior string ---
94+ val channelId = when (behavior.lowercase()) {
95+ " vibrate" -> CHANNEL_ID_VIBRATE_ONLY
96+ " silent" -> CHANNEL_ID_SILENT
97+ " vibrateandsound" -> CHANNEL_ID_VIBRATE_AND_SOUND
98+ else -> CHANNEL_ID_VIBRATE_AND_SOUND // Default case
99+ }
100+
101+
102+ // --- Build the notification using the selected channel ID ---
103+ val notificationBuilder = NotificationCompat .Builder (context, channelId)
59104 .setSmallIcon(R .drawable.notification_bell)
60105 .setContentTitle(" Alarm" )
61106 .setContentText(alarmItem.message)
@@ -65,25 +110,9 @@ class AlarmNotificationServiceImpl(private val context: Context) : AlarmNotifica
65110 .setOngoing(true )
66111 .setAutoCancel(false )
67112
68- // --- Configure notification based on the behavior string ---
69- when (behavior) {
70- " Vibrate" -> {
71- // The channel has vibration enabled, so we only need to remove the sound.
72- notificationBuilder.setSound(null )
73- }
74- " Silent" -> {
75- // Remove both sound and vibration.
76- notificationBuilder.setSound(null )
77- notificationBuilder.setVibrate(null )
78- }
79- " VibrateAndSound" -> {
80- // Do nothing, let the channel's default behavior (high importance) take over.
81- }
82- }
83-
84113 val notification = notificationBuilder.build()
85114
86- notificationManager.cancel(alarmItem.id)
115+ // The system now handles sound/vibration based on the channel
87116 notificationManager.notify(alarmItem.id, notification)
88117 }
89118
0 commit comments