首页 > 解决方案 > 通知未显示 android API 29

问题描述

好的,伙计们,我知道为什么,但在使用 API 29 的手机上没有显示通知。例如,在 Api 21 上,一切正常。是的,我尝试阅读 SO 上的其他帖子,发现没有任何用处,也尝试了这些帖子中的所有建议。

最小 sdk 24,最大 sdk - 29 在 gradle

清单中的接收器:

<receiver android:name=".ui.more_content.receivers.RemindersReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED"/>
                <action android:name="android.intent.action.PACKAGE_REMOVED"/>
                <data android:scheme="package"/>
            </intent-filter>
        </receiver>

接收器类:

class RemindersReceiver: BroadcastReceiver() {

    override fun onReceive(p0: Context?, p1: Intent?) {
        // Идентификатор уведомления
        if(p1?.action.equals("android.intent.action.PACKAGE_ADDED")||p1?.action.equals("android.intent.action.PACKAGE_REMOVED")
            ||p1?.action.equals("android.intent.action.PACKAGE_UPDATED")) {
            showNotificationWith("qwqwdqwqs", p0)
        }
    }

    private fun showNotificationWith(message: String, context: Context?) {
        val channelId = "com.example.notif.channelId"
        val channelName = "App status"
        val contentTitle = "Title"
        val notificationManager =
            context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        val activityIntent = Intent()
        val contentIntent: PendingIntent = PendingIntent.getActivity(
            context,
            0, activityIntent, PendingIntent.FLAG_CANCEL_CURRENT
        )

        val largeIcon: Bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.info)
        val notificationBuilder = NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.drawable.info)
            .setContentTitle(contentTitle)
            .setContentText("asdasdasdasdas")
            .setStyle(
                NotificationCompat.BigTextStyle()
                    .bigText(context.getString(R.string.long_dummy_text))
                    .setBigContentTitle("Big content title")
                    .setSummaryText("Summary is this Text")
            )
            .setLargeIcon(largeIcon)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setColor(Color.RED)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)



        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            val importance = NotificationManager.IMPORTANCE_HIGH
            val notificationChannel = NotificationChannel(channelId, channelName, importance)
            notificationBuilder.setChannelId(channelName)

            notificationManager.createNotificationChannel(notificationChannel)
            notificationManager.notify(
                message.hashCode(), notificationBuilder
                    .setContentIntent(contentIntent).setAutoCancel(true).build()
            )

        } else {
            notificationManager
                .notify(
                message.hashCode(), notificationBuilder
                    .setContentIntent(contentIntent).setAutoCancel(true).build()
            )
        }
    }
}

感谢任何帮助,谢谢转发

标签: androidkotlin

解决方案


自 Android 8.0 以来,这些广播还没有传送到清单注册的接收器。


推荐阅读