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
来设置,并在设备关闭时唤醒设备。
- 如何写while循环与在kotlin中的responseInputStream.read – (while((i = responseInputStream.read(byteContainer))
- java.lang.ClassNotFoundException:com.intellij.junit5.JUnit5IdeaTestRunner在kotlin中使用spek
- Kotlin SingleTon应用程序类
- Android:如何从Kotlin调用ndk函数?
- 如何将多个历史数据添加到GoogleFit
- Android工作室不解决在IDE中使用Kotlin(.kt)编写的类,但编译正常
- 使用编译“org.jetbrains.kotlin:kotlin-stdlib-jre7:$ kotlin_version”给错误
- 即时应用基本功能的Application.onCreate()永远不会被调用
- Kotlin中活动与片段之间的交流