1)这是我在收到此类消息时的火基类。
public class FirebaseMessagingPushService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("Sound Bell", "From: " + remoteMessage.getNotification().getSound());
if (remoteMessage.getData().size() > 0) {
sendNotification(remoteMessage);
}
}
private void sendNotification(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
final String ChannelID = "my_channel_01"; //this is my channel id name
String title = data.get("title");
String message = data.get("body");
String sound = remoteMessage.getNotification().getSound();
String action = remoteMessage.getNotification().getClickAction();
String Notification = remoteMessage.getNotification().getChannelId();
Uri sounduri = Uri.parse(String.format("android.resource://%s/%s/%s", this.getPackageName(), "raw", sound));
Log.i("hellosound", String.valueOf(sounduri));
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.getDefaultType(sounduri));
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, ChannelID)
.setSmallIcon(R.drawable.logomadhuras)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setAutoCancel(true);
int notificationId = (int) System.currentTimeMillis();
mBuilder.setSound(alarmSound);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (manager != null) {
***********Here i create channel*******
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(ChannelID,
"Human Readable channel id",
NotificationManager.IMPORTANCE_HIGH);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
channel.setShowBadge(true);
channel.setSound(alarmSound, attributes);
manager.createNotificationChannel(channel);
}
manager.notify(notificationId, mBuilder.build());
}
}
}
2)下面的代码在清单文件中定义。
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="my_channel_01" />
<service
android:name=".FirebaseMessagingPushService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
3)我在fire base的json文件中同时传递了通道ID和声音。
问题-我希望在应用程序中自定义声音,但是当我收到来自Firebase的消息时,我 无法获得自定义声音,但我的手机铃声正在接收到的消息上播放。.请给我解决方案,如何设置自定义声音。而且我的手机版本是android pie。
答案 0 :(得分:1)
尝试一下:
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.notification_mp3);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel("YOUR_CHANNEL_ID",
"YOUR CHANNEL NAME",
NotificationManager.IMPORTANCE_DEFAULT)
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID,
context.getString(R.string.app_name),
NotificationManager.IMPORTANCE_HIGH);
mChannel.setDescription(msg);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setSound(sound, attributes); // IMPORTANT
if (mNotificationManager != null)
mNotificationManager.createNotificationChannel(mChannel);
}
希望这会起作用!