Android Kotlin – 无法安排与警报管理器通知未来的日期

我有一些kotlin代码来安排Android notifcation 。 这是我的代码看起来像

 @Throws(ParseException::class) fun scheduleNotification(context: Context, info: Info) { val notificationIntent = Intent(context, NotificationPublisher::class.java) notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, System.currentTimeMillis()) notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, getNotification(context, info)) val pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT) val date = Utils.sdfWithTime.parse(String.format("%s %s", info.date, // yyyy-MM-dd format info.status)) // HH:mm format val delay = SharedPreferenceUtils.getPreferenceValueInteger(context, PreferenceKeys.alarmDelayPref) // date is in UTC val futureInMillis = date.time - delay * 60 * 60 * 1000 Log.d(TAG, "Schedule a notification for $info at $futureInMillis") val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager alarmManager.set(AlarmManager.RTC_WAKEUP, futureInMillis, pendingIntent) } fun getNotification(context: Context, info: Info): Notification { val builder = Notification.Builder(context) builder.setContentTitle("Time time - ${info.title}") val content = String.format("${info.content}") builder.setContentText(content) builder.setSmallIcon(R.drawable.logo) return builder.build() } 

这就是我的AndroidManifest.xml包含的内容

  <receiver android:name="com.agents.of.shield.coulson.NotificationPublisher" android:enabled="true" /> 

和我的通知广播接收器

 class NotificationPublisher : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val notification = intent.getParcelableExtra<Notification>(NOTIFICATION) Log.i(NotificationPublisher.javaClass.name, "Notification" + notification) val id = intent.getLongExtra(NOTIFICATION_ID, 0L) notificationManager.notify(id.toInt(), notification) } companion object { var NOTIFICATION_ID = "notification-id" var NOTIFICATION = "notification" } } 

当我使用alarmManager服务时, notification没有被调度。

你觉得我在这里做错了什么?

AlarmManager.RTC_WAKEUP标志需要时间以UTC来设置,并在设备关闭时唤醒设备。