Я попробовал все, чтобы добавить пользовательский тон уведомления в мое приложение. Все уведомления запускаются при изменениях в FirebaseDB.
Вот мой класс FirebaseIdService:
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
FirebaseMessaging.getInstance().subscribeToTopic("MyNotifications");
String Token = FirebaseInstanceId.getInstance().getToken();
soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.siren);
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("data", "fromoutside");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
String channelId = "MyNotifications";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_location_on_black_24dp).setColor(getResources().getColor(R.color.colorPrimaryDark))
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setVibrate(new long[]{0, 500, 1000})
.setDefaults(Notification.DEFAULT_LIGHTS)
.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/" + R.raw.siren))
.setAutoCancel(true).setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "MyNotifications", NotificationManager.IMPORTANCE_DEFAULT);
channel.setLightColor(Color.GRAY);
channel.enableLights(true);
channel.setDescription("This is description for Notification");
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
channel.setSound(soundUri, audioAttributes);
if (manager != null) {
manager.createNotificationChannel(channel);
}
}
manager.notify(0, builder.build());
Также вот мой Манифест:
<service
android:name=".MyFirebaseInstanceIdService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:value="MyNotifications"
android:resource="@drawable/ic_location_on_black_24dp" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:value="MyNotifications"
android:resource="@android:color/holo_red_light" />
Как Как только уведомление запускается, оно всегда воспроизводит звук уведомления по умолчанию, а не звук сирены, который я поместил в папку / res / raw / siren