[英]android notification channel sound not working
我知道有很多關於這個問題的帖子。 我都試過了。 這是我做的步驟。
首先,我發現頻道一旦創建就無法更改。 唯一的方法是重新安裝應用程序。 所以這就是我所做的,但沒有奏效。
其次,有人說我可以刪除頻道,所以我也使用此代碼這樣做了
val channelList = mNotificationManager.notificationChannels
var i = 0
while (channelList != null && i < channelList.size) {
Log.d("channelList","channel ID is ${channelList[i].id}")
//mNotificationManager.deleteNotificationChannel(channelList[i].id)
i++
}
然后在刪除后重新創建頻道。
第三,我嘗試使用新的通知渠道,但每次使用新渠道時都會出現錯誤。
這是我在嘗試過的所有解決方案中使用的代碼
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channelList = mNotificationManager.notificationChannels
var i = 0
while (channelList != null && i < channelList.size) {
Log.d("channelList","channel ID is ${channelList[i].id}")
mNotificationManager.deleteNotificationChannel(channelList[i].id)
i++
}
Log.d("isnotification"," is it needed $isNotificationSoundNeeded importance is $importance")
val mChannel = NotificationChannel(CHANNEL_ID, appName, NotificationManager.IMPORTANCE_HIGH)
mChannel.setShowBadge(false)
mChannel.setSound(notifSound, audioAttributes)
val mChannelnew = NotificationChannel(CHANNEL_ID2, appName, NotificationManager.IMPORTANCE_DEFAULT)
mChannelnew.setShowBadge(false)
mChannelnew.setSound(notifSound, audioAttributes)
mNotificationManager.createNotificationChannel(mChannel)
我錯過了什么? 有任何想法嗎? 謝謝
更新:這是notifsound的代碼
val notifSound = Uri.parse("android.resource://" + packageName + "/" + R.raw.unconvinced)
首先,我不知道您的通知在Oreo 、 Pie或低於N 的設備上不起作用。
For your question StackOver Flow have lots of answer.
現在根據您的問題您只缺少一行代碼但是由於您尚未粘貼,因此無法檢查您的整個通知代碼。
在這里,我粘貼了一個通知代碼,它只是滿足您的所有通知要求。 (完全自定義通知)
帶有圖像的通知
public void createNotificationWithImage(String title,String message ,Bitmap image) {
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
0 /* Request code */, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Custom Sound Uri
Uri soundUri = Uri.parse("android.resource://" + mContext.getApplicationContext()
.getPackageName() + "/" + R.raw.sniper_gun);
mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.notification_icon);
// Pay attention on below line here (NOTE)
mBuilder.setSound(soundUri);
if (image!=null) {
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setLargeIcon(image)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image).setSummaryText(message).bigLargeIcon(null))
.setColor(Color.GREEN)
.setContentIntent(resultPendingIntent);
}
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
現在我正在粘貼可以在上面或在OREO設備上工作的通知代碼。
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
if(soundUri != null){
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
notificationChannel.setSound(soundUri,audioAttributes);
}
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
below middle braces use for close your method.
}
無圖像通知
public void createNotification(String title,String message){
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
0 /* Request code */, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri soundUri = Uri.parse("android.resource://" + mContext.getApplicationContext()
.getPackageName() + "/" + R.raw.sniper_gun);
mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.notification_icon);
mBuilder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),
R.mipmap.icon));
mBuilder.setSound(soundUri);
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setColor(Color.GREEN)
.setStyle(new NotificationCompat.BigTextStyle())
.setContentIntent(resultPendingIntent);
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
// notificationChannel.s
if(soundUri != null){
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
notificationChannel.setSound(soundUri,audioAttributes);
}
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
}
注意:在我的代碼中,我提到要注意我描述要使用通知設置聲音 Uri 的特定行。 你可以這樣描述。
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setSound(soundUri)
.setColor(Color.GREEN)
.setStyle(new NotificationCompat.BigTextStyle())
.setContentIntent(resultPendingIntent);
但它不會為您播放聲音,因為在 Oreo 設備未將聲音設置為優先級之后。
所以總是像我描述的那樣使用聲音代碼。
我猜您使用了錯誤的使用類型,請將您的audioAttributes
使用編輯為USAGE_NOTIFICATION
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build()
來自官方文檔:
USAGE_NOTIFICATION :當使用是通知時使用的使用值。
您需要使用音頻屬性,還需要在許可的情況下定義鈴聲 URI。
所以首先我們定義鈴聲 URI:
Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
boolean vibrate = true;
long[] vibratePattern = new long[]{0L, 1000L};
public constructor(){
notificationBuilder = new NotificationCompat.Builder(mContext, app.getAppContext().getString(R.string.default_notification_channel_id));
mNotifyManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
mContext.grantUriPermission("com.android.systemui", ringtoneUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
public void showNotificationNormal(String notificationTitle, String notificationBody, Intent intent) {
String id = mContext.getString(R.string.default_notification_channel_id);
PendingIntent lowIntent = PendingIntent.getActivity(mContext, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext, id);
NotificationManager mNotifyManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = mContext.getString(R.string.default_notification_channel_name);
String description = mContext.getString(R.string.default_notification_channel_description); //user visible
int importance = NotificationManager.IMPORTANCE_HIGH;
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.enableVibration(vibrate);
mChannel.setVibrationPattern(vibratePattern);
mChannel.setLightColor(Color.RED);
mChannel.setSound(ringtoneUri, att);
mChannel.setBypassDnd(true);
mChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
mChannel.setShowBadge(true);
if (mNotifyManager != null) {
mNotifyManager.createNotificationChannel(mChannel);
}
notificationBuilder
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setVibrate(vibratePattern)
.setSound(ringtoneUri)
.setColor(ContextCompat.getColor(mContext, R.color.colorPrimary))
.setContentTitle(notificationTitle)
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
.setAutoCancel(true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(lowIntent);
} else {
notificationBuilder.setContentTitle(notificationTitle)
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setVibrate(vibratePattern)
.setSound(ringtoneUri)
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
.setColor(ContextCompat.getColor(mContext, R.color.colorPrimary))
.setAutoCancel(true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(lowIntent);
}
notificationBuilder.setContentText(notificationBody);
if (mNotifyManager != null) {
mNotifyManager.notify(AppConstants.NOTIFY_ID, notificationBuilder.build());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.