Android Notification Builder 不播放自定义声音

标签 android android-studio audio push-notification android-notifications

我发布了用于检索消息并显示来自 firebase 的通知的代码。我收到带有正确文本和标题的通知,但此通知是无声的。即使我设置了默认声音或自定义声音。
如何播放正确的声音?

公共(public)类 NotificationMessagingService 扩展 FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d("sb.fbv", "NotificationMessaginService.onmessageReceived");
    String title = remoteMessage.getNotification().getTitle();
    String subText = remoteMessage.getNotification().getBody();
    String message = remoteMessage.getData().get("subtext");

    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);

    NotificationCompat.Builder nb = new NotificationCompat.Builder(this);
    nb.setContentTitle(title);
    nb.setSubText(message);
    nb.setContentText(subText);
    nb.setSmallIcon(R.drawable.notificavedelago);
    nb.setAutoCancel(true);
    nb.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
    nb.setLights(Color.BLUE, 3000, 3000);

    //SOUND DEFAULT
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    nb.setSound(alarmSound);

    //CUSTOM SOUND
    //Uri customSound= Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sound);
    //nb.setSound(customSound);

    nb.setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, nb.build());

}

}

最佳答案

要在通知生成器中设置自定义警报声音,请执行以下操作:

int soundResId = R.raw.myCustomSoundResource;
Uri soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + soundResId);
nb.setSound(soundUri, AudioManager.STREAM_ALARM);

使用文件的资源 ID 号,如 res/raw/myCustomSoundResource.ogg .可以在any of these supported file formats .

关于Android Notification Builder 不播放自定义声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39757707/

相关文章:

android webview 打开谷歌地图应用程序

android - PagerSlidingTabStrip 显示 viewPager 项目的错误位置

java.io.FileNotFoundException :/storage/emulated/0/Android/data/MyApplication/MyFile. ICS。 (没有这样的文件或目录)/Android Studio/Ical4j

android - 三星远程测试实验室 (RTL) 可以与 Android Studio 一起使用吗

c# - 在 .net 中将 MIDI 转换为 .wav 文件

firefox - 在Firefox中播放声音按钮。它不起作用,但应该

audio - 使用openSMILE音频特征提取器获取特征集的平均值

android - 在android应用程序中,是否可以使用aarch64 cpu的arm库?

android - 为 Android 创建自定义锁屏

android - 有没有办法在 Android 上使用随机森林模型进行预测?