自定义通知声音在5.0及更高版本中不起作用

时间:2018-07-09 10:25:42

标签: android android-notifications android-5.0-lollipop android-notification-bar

我正在尝试向通知中添加自定义声音。以下是我的代码:

notificationSoundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.error);

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    NotificationChannel channel = new NotificationChannel("default",
                            "YOUR_CHANNEL_NAME",
                            NotificationManager.IMPORTANCE_DEFAULT);
                    channel.setDescription("YOUR_NOTIFICATION_CHANNEL_DISCRIPTION");
                    mNotificationManager.createNotificationChannel(channel);
                }

                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "default")
                        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                        .setSmallIcon(R.mipmap.ic_launcher) // notification icon
                        .setContentTitle("BATTERY FULL") // title for notification
                        .setContentText("Battery is full. Please plug out the charger. Overcharging may decrease battery life span.")// message for notification
                        .setSound(notificationSoundUri) // set alarm sound for notification
                        .setAutoCancel(true); // clear notification after click

                mNotificationManager.notify(1, mBuilder.build());

此代码在棒棒糖之前的设备中运行,而不在棉花糖上运行。在棉花糖中,它使用电话的默认通知声音。我假设这个问题是针对棒棒糖和更高设备的。我在这里想念什么?

3 个答案:

答案 0 :(得分:1)

对此问题进行了几次尝试,看来NotificationCompat错过了在Android 21和25之间打开声音的方法,要使通知正常工作,您需要将声音设置为此

val builder: Notification.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      Notification.Builder(this, channelId1)
} else {
      Notification.Builder(this)        
}
 ...
val audioAttr = AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_ALARM).build()
            builder.setSound(
                notification1Sound(), // custom uri
                audioAttr)

我不支持以前的棒棒糖版本,所以这对我有用,对于较旧的版本,您可能需要添加第二个版本检查

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   builder.setSound(
       notification1Sound(),
       audioAttr
    )
} else {
   builder.setSound(notification1Sound(), AudioManager.STREAM_ALARM)
}

答案 1 :(得分:0)

您必须使用通知频道和Android O(https://developer.android.com/training/notify-user/channels)。 该功能仅在API 26+上有效,因为NotificationChannel类是新的,而不在支持库中。

答案 2 :(得分:0)

 builder.setStyle(new NotificationCompat.InboxStyle());

完整代码:

   NotificationCompat.Builder builder =  
        new NotificationCompat.Builder(this)  
        .setSmallIcon(R.drawable.ic_launcher)  
        .setContentTitle("Notifications Example")  
        .setContentText("This is a test notification");  


Intent notificationIntent = new Intent(this, MenuScreen.class);  

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,   
        PendingIntent.FLAG_UPDATE_CURRENT);  

builder.setContentIntent(contentIntent);  
builder.setAutoCancel(true);
builder.setLights(Color.BLUE, 500, 500);
long[] pattern = {500,500,500,500,500,500,500,500,500};
builder.setVibrate(pattern);
builder.setStyle(new NotificationCompat.InboxStyle());

将这些代码行用于自定义声音

URI uri=Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.FILE_NAME);//Here is FILE_NAME is the name of file that you want to play

builder.setSound(uri);

别忘了添加通知频道

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            @SuppressLint("WrongConstant")
            NotificationChannel channel = new NotificationChannel("XYZ", "ABC",NotificationManager.IMPORTANCE_MAX);
            mNotificationManager.createNotificationChannel(channel);
        }

        mNotificationManager.notify(0, mBuilder.build());