【发布时间】:2021-05-07 01:14:58
【问题描述】:
我正在制作一个带有通知的简单提醒应用程序,正如标题所说,我的 android 通知永远不会出现。
我已经制作了一个这样的广播类:
class ReminderBroadcast : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
var builder = NotificationCompat.Builder(context, "notifyUser")
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Memo reminder")
.setContentText("You have a memo set to the current time!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
var notificationManager = NotificationManagerCompat.from(context)
notificationManager.notify( 200, builder.build())
}
}
在我的 MainActivity 中,我正在使用以下方法创建一个频道:
private fun createNotificationChannel(){
val name: CharSequence = "UserNotificationChannel"
val description: String = "Channel for notifying users"
val importance: Int = NotificationManager.IMPORTANCE_DEFAULT
val channel: NotificationChannel = NotificationChannel("notifyUser", name, importance)
channel.description = description
val notificationManager = getSystemService(NotificationManager::class.java)
notificationManager.createNotificationChannel(channel)
// }
}
最后我在一个包含新提醒表单的片段中创建通知。在创建表单(提交按钮的 onClickListener)后,我创建了这样的通知:
val intent = Intent(context, ReminderBroadcast::class.java)
val pendingIntent: PendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0)
val alarmManager: AlarmManager = requireContext().getSystemService(ALARM_SERVICE) as AlarmManager
//set notification to trigger 5 seconds from now
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5000, pendingIntent)
//set direction
findNavController().popBackStack()
通知从未显示,而且我几天来一直对我错过了什么一无所知。在此先感谢您的帮助:)
【问题讨论】:
-
只添加你的通知通道就可以了,记得在启动应用程序之前创建
标签: java android android-studio kotlin android-notifications