在Android上使用NotificationCompat时,通知仅在API级别26或更高级别上可以正常工作。
代码:
val chan2 = NotificationChannel(SECONDARY_CHANNEL,
getString(R.string.noti_channel_second), NotificationManager.IMPORTANCE_HIGH)
manager.createNotificationChannel(chan2)
fun getNotification2(title: String, body: String): NotificationCompat.Builder {
return NotificationCompat.Builder(applicationContext, SECONDARY_CHANNEL)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(smallIcon)
.setAutoCancel(true)
}
fun notifySecondaryChannel(id: Int, notification: NotificationCompat.Builder) {
notification.priority = NotificationCompat.PRIORITY_MAX
manager.notify(id, notification.build())
}
依赖项:
完整代码on github(派生和更新的Google示例)。
答案 0 :(得分:1)
可以使用以下声音播放声音:
builder.setDefaults(Notification.DEFAULT_SOUND) or
builder.setDefaults(Notification.DEFAULT_ALL)
或setSound的替代之一。例如:
public Notification.Builder setSound (Uri sound,
int streamType)