I implemented sending push notifications from the Firebase console. When I set up a notification in the Firebase console, I turn on the sound, but the sound does not work when the application is in the foreground and when closed. I tested on from Android 10 to Android 12 there is no notification sound anywhere. Any help
My code:
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onNewToken(token: String) {
super.onNewToken(token)
}
override fun onMessageReceived(message: RemoteMessage) {
if (message.notification != null) {
createFCMNotification(title = message.notification!!.title, message = message.notification!!.body)
}
}
private fun createFCMNotification(title: String?, message: String?) {
val intent = Intent(this@MyFirebaseMessagingService, SplashActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val defaultSoundUri: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
if (Build.VERSION.SDK_INT >= 23) {
val pendingIntent = PendingIntent.getActivity(
this@MyFirebaseMessagingService, 777, intent,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE)
val builder: NotificationCompat.Builder =
NotificationCompat.Builder(applicationContext, channelId)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.icon_notify_old)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_ALL)
.setCategory(Notification.CATEGORY_CALL)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setFullScreenIntent(pendingIntent, true)
builder.setContent(getRemoteView(title, message))
builder.setCustomHeadsUpContentView(getRemoteView(title, message))
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(notificationChannel)
}
notificationManager.notify(0, builder.build())
} else {
val pendingIntent = PendingIntent.getActivity(
this@MyFirebaseMessagingService, 777, intent,
PendingIntent.FLAG_ONE_SHOT)
val builder: NotificationCompat.Builder =
NotificationCompat.Builder(applicationContext, channelId)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.icon_notify_old)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_ALL)
.setCategory(Notification.CATEGORY_CALL)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setFullScreenIntent(pendingIntent, true)
builder.setContent(getRemoteView(title, message))
builder.setCustomHeadsUpContentView(getRemoteView(title, message))
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(notificationChannel)
}
notificationManager.notify(0, builder.build())
}
}
private fun getRemoteView(title: String?, message: String?): RemoteViews {
val remoteView = RemoteViews(BuildConfig.APPLICATION_ID, R.layout.notification_fcm)
remoteView.setTextViewText(R.id.fcm_title, title)
remoteView.setTextViewText(R.id.fcm_message, message)
remoteView.setImageViewResource(R.id.fcm_icon, R.drawable.icon_notify_old)
return remoteView
}
}
My Manifest:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/icon_notify_old" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/text_dark_blue" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
<!-- FCM Service -->
<service
android:name=".service.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>