Ibccx
Ibccx

Reputation: 145

SetAlarmClock not showing alarm in notification

According to the docs:

The expectation is that when this alarm triggers, the application will further wake up the device to tell the user about the alarm -- turning on the screen, playing a sound, vibrating, etc. As such, the system will typically also use the information supplied here to tell the user about this upcoming alarm if appropriate.

SetAlarmClock docs

Is this done automatically? or I have to add a service with a notification.

I have read about setAlarmClock including a visible notification, or has the default behaviour been changed?

val activityIntent = Intent(requireContext(), AlarmClickedActivity::class.java)
pendingIntent =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PendingIntent.getActivity(
requireContext(),
0,
activityIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
} else {
PendingIntent.getActivity(
requireContext(),
0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT
)
 }

val alarmClockInfo = AlarmManager.AlarmClockInfo(alarm.timeInMilliSecond, null)
alarmManager.setAlarmClock(alarmClockInfo, pendingIntent)

This only opens a new activity (AlarmClickedActivity) when the alarm goes off.

I don't get the notification of 'Current Alarm' or 'Upcoming Alarm' as mentioned in the docs

Upvotes: 0

Views: 899

Answers (1)

Niaj Mahmud
Niaj Mahmud

Reputation: 469

Firstly go to your app setting and give notification permission then run again and set a alarm then check.

If still having problem then use my code

             class AlarmReceiver : BroadcastReceiver() {
        
            override fun onReceive(context: Context, intent: Intent?) {
                Log.d("this", "notify")
        
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        
                    val intent = Intent(context, AlarmActivity2::class.java)
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
                    val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
        
                    val builder = NotificationCompat.Builder(context, "111")
                        .setSmallIcon(R.drawable.blue_stringart)
                        .setContentTitle("Alarm is running")
                        .setAutoCancel(true)
                        .setDefaults(NotificationCompat.DEFAULT_ALL)
                        .setDefaults(NotificationCompat.DEFAULT_SOUND)
                        .setDefaults(NotificationCompat.DEFAULT_VIBRATE)
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .addAction(R.drawable.ic_baseline_stop_24,"Stop",pendingIntent)
                        .setContentIntent(pendingIntent)
        
                    val notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
                    val r = RingtoneManager.getRingtone(context, notification)
                    r.play()
        
                    val notificationManagerCompat = NotificationManagerCompat.from(context)
                    notificationManagerCompat.notify(123, builder.build())
        
                }
        
            }
        
        } 
    
    

Then go to activity class and set your alarm in a method.

  private fun setAlarm1() {
    var calender: Calendar
  calender = Calendar.getInstance()
                calender.set(Calendar.HOUR_OF_DAY, PUT_YOUR_ALARM HOUR)
                calender.set(Calendar.MINUTE, PUT_YOUR_ALARM MINUTE)
                calender.set(Calendar.SECOND, 0)
            alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
            val thuReq: Long = Calendar.getInstance().timeInMillis + 1
            var reqReqCode = thuReq.toInt()
            if (calender.timeInMillis < System.currentTimeMillis()) {
                calender.add(Calendar.DAY_OF_YEAR, 1)
            }
            val alarmTimeMilsec = calender.timeInMillis
            val intent = Intent(this, AlarmReceiver::class.java)
            intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
            val pendingIntent = PendingIntent.getBroadcast(this, reqReqCode, intent, 0)
    
            alarmManager.setRepeating(
                AlarmManager.RTC_WAKEUP,
                calender.timeInMillis,
                24 * 60 * 60 * 1000,
                pendingIntent
            )
    
    

Note - 1.alarmManager.setRepeating here you can use your alarm type as your wish. 2.requestcode must be unique for each alarm. 3. you must take a alarm time and keep in calender.timeInMillis which is you expecting alarm time.

still problem comments below

Upvotes: 2

Related Questions