我正在制作一个带有通知的简单提醒应用程序,正如标题所示,我的 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 中,我使用以下方法创建一个 channel :
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)
// }
}
最后,我在包含新提醒表单的 fragment 中创建通知。创建表单(用于提交按钮的 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()
通知从未显示,我已经好几天不知道自己错过了什么。预先感谢您的帮助:)
最佳答案
您没有收到通知的原因是通知 channel :-
从 Android 8.0(API 级别 26)开始,所有通知都必须分配给一个 channel 。对于每个 channel ,您可以设置应用于该 channel 中所有通知的视觉和听觉行为。然后,用户可以更改这些设置并决定应用程序中的哪些通知 channel 应该是侵入性的或完全可见的。
解决方案
第 1 步:创建通知 channel -
object NotificationHelper {
fun createNotificationChannel(context: Context, importance: Int, showBadge: Boolean, name: String, description: String) {
// 1
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// 2
val channelId = "${context.packageName}-$name"
val channel = NotificationChannel(channelId, name, importance)
channel.description = description
channel.setShowBadge(showBadge)
// 3
val notificationManager = context.getSystemService(NotificationManager::class.java)
notificationManager.createNotificationChannel(channel)
}
}
}
第 2 步:在您的应用程序类中注册 channel
class AppClass : Application() {
override fun onCreate() {
super.onCreate()
NotificationHelper.createNotificationChannel(this,
NotificationManagerCompat.IMPORTANCE_DEFAULT, false,
getString(R.string.app_name), "App notification channel.")
}
}
步骤 3. 在 Manifest 中注册此应用程序类
<application
android:name=".AppClass"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
注意:通过此链接了解有关通知 channel 的更多信息:https://developer.android.com/training/notify-user/channels
关于java - Android 通知永远不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67427788/