【发布时间】:2020-05-14 08:56:40
【问题描述】:
我正在尝试在屏幕关闭时调用通知。
这适用于华为 p10 Lite(序列号:HUAWEIWAS-LX1AAndroid 8.0.0),但不适用于 Android 10 三星 Galaxy Tab S6(QP1A.190711.020)。不过,通知可以通过锁屏。
这是我的代码:
fun Notifications () {
mNotification = NotificationCompat.Builder(this, CHANNEL_ID)
.setDefaults(NotificationCompat.DEFAULT_VIBRATE)
.setVibrate(longArrayOf(1000))
.setSmallIcon(R.drawable.icon)
.setSound(soundUri)
.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.icon))
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentTitle("Title")
.setContentText("Text")
.setChannelId(CHANNEL_ID)
.setAutoCancel(true)
.setOngoing(true)
.setColor(NotificationCompat.COLOR_DEFAULT)
.setColorized(true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(CHANNEL_ID, "my-app", NotificationManager.IMPORTANCE_HIGH).apply {
description = "Test"
}
channel.importance = NotificationManager.IMPORTANCE_HIGH
channel.shouldVibrate()
channel.enableVibration(true)
channel.vibrationPattern=longArrayOf(1000, 1000, 1000, 1000)
channel.canBypassDnd()
channel.lightColor = NotificationCompat.COLOR_DEFAULT
channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mNotificationManager.createNotificationChannel(channel)
}
}
MainActivity 类的成员:
val soundUri: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
lateinit var mNotification : NotificationCompat.Builder
lateinit var mNotificationManager : NotificationManager
val mNotificationID: Int = 1
val CHANNEL_ID : String = "com.example.myapp"
这是我的使用方法:
with(NotificationManagerCompat.from(this)) {
notify(mNotificationID, mNotification.build())
}
【问题讨论】:
标签: android android-notifications