java - 通过 setSound() 设置自定义通知声音(和振动)不起作用

标签 java android audio notifications vibration

当尝试在我的应用程序中创建通知时,设置自定义通知声音不起作用,而是使用系统默认声音。另外,我尝试在通知上启用振动,但这也不起作用。

我尝试按如下方式设置声音(相关 fragment ,请参阅下面的完整代码):

Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bell);
NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel(channelId,
        "Order notifications", NotificationManager.IMPORTANCE_MAX);
    notificationManager.createNotificationChannel(channel);

    channel.setSound(sound, 
        new AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
            .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
            .build()
    );
    channel.setVibrationPattern(new long[]{0, 500});
} else {
    notificationBuilder
            .setVibrate(new long[]{0, 500})
            .setSound(sound);
}

notificationManager.notify(0, notificationBuilder.build());

我的自定义声音是一秒长的声音 fragment ,以 44.1 kHz 采样,存储为 res/raw/bell.wav。我还尝试将代码 fragment 转换为不同的其他格式,我尝试了 mp3ogg 但没有成功。 关于振动: android.permission.VIBRATE 包含在 list 中。

我不知道我做错了什么。以下是创建并发布通知的 sendNotification() 函数的完整代码。

private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bell);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.ic_launcher_web)
                    .setContentTitle("New order")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "Order notifications",
                NotificationManager.IMPORTANCE_MAX);
        notificationManager.createNotificationChannel(channel);

        AudioAttributes att = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                .build();

        channel.setSound(sound, att);
        channel.setVibrationPattern(new long[]{0, 500});
    } else {
        notificationBuilder
                .setVibrate(new long[]{0, 500})
                .setSound(sound);
    }

    notificationManager.notify(0, notificationBuilder.build());
}

最佳答案

创建NotificationChannel后,您无法更改灯光、声音和振动等内容。也许这就是问题所在。要查看更改,您需要先删除该应用并再次安装。

关于java - 通过 setSound() 设置自定义通知声音(和振动)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49677119/

相关文章:

java - jmdns 在 IPv4 接口(interface)上注册,但广播 IPv6

java - Java 世界中有可变键长的 Map 吗?

android - Min3D-白色纹理问题

android - 如何在Android中将音频文件发布到服务器

c# - 声音未在项目构建中播放

android - 是否存在Java类或函数来对样本数组进行一维卷积?

java - 如果应用程序的第二个实例已在 JAVA 中运行,如何停止执行?

java - Java NetBeans 中的 JXTitledPanel 是什么

android - 无法使用退格键删除 editText 中的内容?

android - 如何使用 Retrofit Android 上传图片?