我发布了用于检索消息并显示来自 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/