我已经编写了用于通过通知渠道进行通知的代码。它在我的两台vivo手机中发出默认声音,但在装有android os 7的小米手机和装有android 8的三星手机中根本没有声音。
这是我的通知代码:
fun showNotification(context: Context) {
Log.e("xoxo", "showNotification called")
val homeIntent = Intent(context, HomeActivity::class.java)
homeIntent.putExtra(Constants.INTENT, 1)
val pendingIntent = PendingIntent.getActivity(
context, 0, homeIntent, PendingIntent.FLAG_UPDATE_CURRENT
)
var builder = NotificationCompat.Builder(context,CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Hey")
.setContentText("Lorem Ipsum is simply...")
.setStyle(
NotificationCompat.BigTextStyle().bigText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s ")
)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
with(notificationManager) {
createChannel(
this,
Constants.CHANNEL_ID,
Constants.CHANNEL_NAME
)
// notificationId is a unique int for each notification that you must define
var notification = builder.build()
notify(Constants.NOTIF_ID, notification)
}
}
这是createChannel方法代码:
fun createChannel( notificationManager: NotificationManager, channelId: String, channelName:String) {
if (Build.VERSION.SDK_INT < 26) {
return
}
val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH)
var att: AudioAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build()
channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),att)
channel.vibrationPattern = longArrayOf(500,500,500)
channel.enableLights(true)
channel.enableVibration(true)
notificationManager.createNotificationChannel(channel)
}
我想显示带有设备默认通知声音和振动的通知。我也曾在通知生成器中尝试过setSound和setVibration,但它根本无法正常工作。 请告诉我我的代码丢失还是错误。