Android 10 NotificationManager不显示

1:问题:
使用三方推送,在Android 10显示通知栏,有时显示有时不显示,略显尴尬

2:解决:

private fun processCustomMessage(context: Context, message: String) {
     
        val channelID = "1001"
        val channelName = "bs_notify"

        //点击通知跳转界面
        val intent = Intent(context, BNoTitleWebActivity::class.java)
        val bundle = Bundle()
        bundle.putString(AppConstant.NEW_URL, "TZGG_URL")
        intent.putExtras(bundle)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        val pendingIntent = PendingIntent.getActivity(context, 1000, intent, PendingIntent.FLAG_UPDATE_CURRENT)

        //通知栏显示
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val builder: Notification.Builder
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
     
            builder = Notification.Builder(context, channelID)
            val channel = NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH)
            notificationManager.createNotificationChannel(channel)
        } else {
     
            builder = Notification.Builder(context)
        }
        builder.setAutoCancel(true)
                .setContentText(gson.fromJson(message, OnNoticePushBean::class.java).PushContent)
                .setContentTitle(gson.fromJson(message, OnNoticePushBean::class.java).PushTitle)
                .setSmallIcon(R.mipmap.icon_app)
                .setDefaults(Notification.DEFAULT_ALL)
                .setContentIntent(pendingIntent)
        notificationManager.notify((System.currentTimeMillis() / 1000).toInt(), builder.build())
    }

亲测可用

你可能感兴趣的:(kotlin)