Android Notifications未在运行API 27的模拟器上显示

时间:2017-11-17 21:53:22

标签: java android notifications kotlin

任何人都可以帮助我理解我做错了什么吗?我有一个警报接收器对象似乎在我几年前运行API 19的三星Galaxy Tab 4上触发通知时起作用。但代码显然有一些错误,因为在仿真器上没有触发任何东西。我一直在阅读使用较新的API创建NotificationChannel对象是必要的,但即使在我的代码中使用此对象,我似乎也无法获得所需的结果。

 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())

    }
}

0 个答案:

没有答案