自定义通知声音,安卓 Oreo?

23

我想在我的应用程序中设置自定义通知声音,使用一个原始的mp3或wav文件。以下是我的代码:

private void sendMyNotification(String message) {
    Intent intent;
    if (sharedPreferences.getBoolean(SPConstants.IS_LOGGED_IN, false)) {
        intent = new Intent(this, ActivityNotification.class);
    } else {
        intent = new Intent(this, ActivitySplash.class);
    }
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.panic);
    AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    manager.setStreamVolume(AudioManager.STREAM_MUSIC, 100, 0);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(mChannel);
    }
    notificationManager.notify(0, notificationBuilder.build());
}
紧急情况的音频文件位于res->raw中。 我已经尝试使用声音的mp3和wav格式,但似乎没有任何方法可以设置通知声音。 我目前正在Pixel 2 OS 8.1上进行测试。
有什么建议可能是问题吗?

2
嗯,我遇到了类似的问题。我找到的唯一解决方案是通过操作系统通知渠道设置来设置它的可能性。这是因为一旦您创建了一个通知渠道,您就无法以编程方式更改其设置(但也许我做错了)。如果您找到了任何其他解决方案,我很想知道如何解决这个问题。 - Patryk Jabłoński
@PatrykJabłoński在Khaled的回答下似乎只有在您没有设置setContentIntent时才有效。 - WISHY
我一定会检查它,但在我的情况下,我使用setContentIntent。谢谢你通知我 :) - Patryk Jabłoński
1
@PatrykJabłoński 是的,我也遇到了同样的问题。我也使用了setContentIntent,但在这种情况下不起作用。 - WISHY
请确保卸载该应用程序。 - Ehsan Ghasaei
显示剩余2条评论
3个回答

50
  • 我测试了以下代码,符合我的预期。

  • 添加内容意图后,它仍然能够正常工作,没有出现任何问题。

  • private void sendMyNotification(String message) {
    
    Intent intent = new Intent(this, SplashActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    
    Uri soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.correct_answer);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CH_ID")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent);
    
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    
        if(soundUri != null){
            // Changing Default mode of notification
            notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
            // Creating an Audio Attribute
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build();
    
            // Creating Channel
            NotificationChannel notificationChannel = new NotificationChannel("CH_ID","Testing_Audio",NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.setSound(soundUri,audioAttributes);
            mNotificationManager.createNotificationChannel(notificationChannel);
        }
    }
    mNotificationManager.notify(0, notificationBuilder.build());
    }
    

更新

  • 你可能需要卸载应用程序以更改声音设置,请查看这些链接以获取更多详细信息。

1
如果您将内容意图设置为通知构建器,则此代码将无法工作。 - WISHY
@KhaledLela 我遇到了这个问题:https://dev59.com/q7Dma4cB1Zd3GeqPALIU - Pratik Butani
4
卸载部分似乎非常关键 - 在没有进行卸载的情况下,我在几部手机上都无法运行。 - dorsz
4
卸载并重新安装应用程式对我很有帮助,谢谢。 - Unaisul Hadi
1
我按照你上面说的做了,但是直到重新安装应用程序才起作用,谢谢你的帮助。 - Usama Saeed US
显示剩余3条评论

4

简单回答:

Uri soundUri = Uri.parse(
                         "android.resource://" + 
                         getApplicationContext().getPackageName() +
                         "/" + 
                         R.raw.push_sound_file);

AudioAttributes audioAttributes = new AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_ALARM)
            .build();

// Creating Channel
NotificationChannel channel = new NotificationChannel("YOUR_CHANNEL_ID",
                                                      "YOUR_CHANNEL_NAME",
                                                      NotificationManager.IMPORTANCE_HIGH);
channel.setSound(soundUri, audioAttributes);

((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
                                           .createNotificationChannel(notificationChannel);

1
晚了一些但可能对某些人有帮助,只需在您的 NotificationCompat.Builder() 实例中添加以下行:
.setSound("your sound uri",AudioManager.STREAM_NOTIFICATION)

注意: 由于 NotificationCompat.Builder() 具有向后兼容性,因此通知渠道中的 AudioAttributes 等内容不是必需的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接