Java 运行API 27的仿真器上未显示Android通知
有人能帮我理解我做错了什么吗?我有一个Alarm Receiver对象,当我的三星Galaxy Tab 4上运行几年前获得的API 19触发通知时,它似乎可以工作。但是代码显然有一些bug,因为模拟器上从来没有触发过什么。我一直在读到,对于较新的API,创建NotificationChannel对象是必要的,但即使在我的代码中有了这个对象,我似乎也无法得到想要的结果Java 运行API 27的仿真器上未显示Android通知,java,android,notifications,kotlin,Java,Android,Notifications,Kotlin,有人能帮我理解我做错了什么吗?我有一个Alarm Receiver对象,当我的三星Galaxy Tab 4上运行几年前获得的API 19触发通知时,它似乎可以工作。但是代码显然有一些bug,因为模拟器上从来没有触发过什么。我一直在读到,对于较新的API,创建NotificationChannel对象是必要的,但即使在我的代码中有了这个对象,我似乎也无法得到想要的结果 class AlarmReceiver : BroadcastReceiver() { override fu
class AlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val notificationIntent = Intent(context, NotificationActivity::class.java)
val stackBuilder = TaskStackBuilder.create(context)
stackBuilder.addParentStack(NotificationActivity::class.java)
stackBuilder.addNextIntent(notificationIntent)
val pendingIntent = stackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT)
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
var builder = NotificationCompat.Builder(context, "DailyJoke")
if (android.os.Build.VERSION_CODES.O <= android.os.Build.VERSION.SDK_INT) {
val importance = NotificationManager.IMPORTANCE_HIGH
//Here's the notificationChannel object. It doesn't seem to work, idk why
val notificationChannel = NotificationChannel("DailyJoke", "Name", importance)
notificationManager.createNotificationChannel(notificationChannel)
Builder(context, notificationChannel.id)
} else {
Builder(context)
}
val sound = Uri.parse("android.resource://" + context.packageName + "/" + "raw/drumroll")
builder = builder
.setSmallIcon(R.mipmap.ic_launcher_round)
.setColor(Color.BLUE)
.setContentTitle("Content Title")
.setTicker("TICKER Text")
.setContentText("KDW setContentText")
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setChannelId("DailyJoke")
.setSound(sound)
notificationManager.notify(1, builder!!.build())
}
}
class AlarmReceiver:BroadcastReceiver(){
覆盖接收(上下文:上下文,意图:意图){
val notificationIntent=Intent(上下文,NotificationActivity::class.java)
val stackBuilder=TaskStackBuilder.create(上下文)
addParentStack(NotificationActivity::class.java)
stackBuilder.addNextIntent(notificationIntent)
val pendingent=stackBuilder.getpendingent(100,pendingent.FLAG_UPDATE_CURRENT)
val notificationManager=context.getSystemService(context.NOTIFICATION\u服务)作为notificationManager
var builder=NotificationCompat.builder(上下文,“DailyJoke”)
if(android.os.Build.VERSION_code.O