我正在尝试在我的android应用程序中使用自定义通知声音。我已经在stackoverflow中检查了所有相关的问题和答案,但我不明白为什么它不在应用程序中播放我的自定义声音。我的通知代码如下。
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getNotification() != null) {
}
if (remoteMessage.getData().size() > 0) {
sendNotification(remoteMessage.getData().get("title"));
}
}
@Override
public void onNewToken(String token) {
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Context context = getBaseContext();
String channelId = "raj";
int sound_notification = context.getSharedPreferences("setting", MODE_PRIVATE).getInt("sound_notification", 1);
Uri MyCustomSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/raw/sound" + (sound_notification));
//Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(messageBody)
.setAutoCancel(true)
.setSound(MyCustomSound)
.setContentIntent(pendingIntent);
//notificationBuilder.setSound(MyCustomSound);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Raj",
NotificationManager.IMPORTANCE_HIGH);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
if (notificationManager != null) {
notificationManager.notify(0, notificationBuilder.build());
}
}
我试着用PHP从我的服务器以及Firebase控制台发送通知,但它总是播放默认声音,而不是我的自定义声音。如果有人能帮我解决这个问题,请告诉我。感谢
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/sound/" + R.raw.sound);
mBuilder.setSound(Uri.parse("android.resource://" + getPackageName() + "/sound/" + R.raw.sound));