SetAlarmClock 未在通知中显示警报

SetAlarmClock not showing alarm in notification

根据文档:

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

这是自动完成的吗?或者我必须添加带有通知的服务。

我已阅读有关包含可见通知的 setAlarmClock 的信息,或者默认行为是否已更改?

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)

这只会在闹钟响起时打开一个新的 activity (AlarmClickedActivity)。

我没有收到文档中提到的 'Current Alarm' 或 'Upcoming Alarm' 的通知

首先转到您的应用设置并授予通知权限,然后再次 运行 并设置闹钟,然后检查。

如果仍有问题,请使用我的代码

             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())
        
                }
        
            }
        
        } 
    
    

然后转到 activity class 并在方法中设置闹钟。

  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
            )
    
    

注- 1.alarmManager.setRepeating 在这里您可以根据需要使用您的闹钟类型。 2.requestcode 每个警报必须是唯一的。 3.您必须设定一个闹钟时间并保持calender.timeInMillis这是您期望的闹钟时间。

下面还有问题评论