0

Since I got to have push notifications working as required when app was in both background and foreground, I have executed 30-40 notifications while testing (in 2 days), all of them sounding properly when arrived to android device (my custom notification channel also appeared in device settings as expected).

Suddenly, notifications continue arriving but without the sound (and custom notification channel does not appear in settings as before did). Since this, it is impossible for me to have sound back on notifications (neither background nor foreground).

Have not changed any involved code. I think sound has stopped because the notification channel is not being created for some reason. Have anyone experienced this or can help me?

Key code for case 'App in background':

1. Manifest.

     <meta-data
          android:name="com.google.firebase.messaging.default_notification_channel_id"
          android:value="my_fcm_default_channel" />

2. Launching activity - onCreate():

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Create channel to show notifications.
            val channelName = "Custom notification channel"
            val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
            val notificationManager = getSystemService(NotificationManager::class.java)
            val channel = NotificationChannel("my_fcm_default_channel",
                channelName,
                NotificationManager.IMPORTANCE_HIGH)
            channel.setSound(defaultSoundUri, AudioAttributes.Builder().build()) // Not working
            channel.enableLights(true)
            channel.lightColor = Color.WHITE
            channel.enableVibration(true)
            channel.vibrationPattern = longArrayOf(100, 200, 100, 200, 100, 200, 100)
            notificationManager.createNotificationChannel(channel)
    }

3. Cloud function node.js snippet code:

   // Notification details.
         const payload = {
           notification: {
             title: 'Some title',  
             body: 'Some message',
             sound: 'default',
             click_action: "OPEN_ACTIVITY_3"
           },
           data: {
             name: 'notification_3'
           }
         };

UPDATE:

Key code for case 'App in foreground':

1. MyFirebaseMessagingService - onMessageReceived():

        val name = remoteMessage.data["name"] ?: ""
        var intent: Intent? = null
        when (name) {
            "notification_1" -> {
                intent = Intent(this, Activity1::class.java)
            }
            "notification_2" -> {
                intent = Intent(this, Activity2::class.java)
            }
            "notification_3" -> {
                val clickAction = remoteMessage.notification?.clickAction
                clickAction?.let {
                    intent = Intent(clickAction)
                    intent?.putExtra("name", name)
                }
            }
            "notification_4" -> {
                intent = Intent(this, Activity4::class.java)
            }
        }
        intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT)

        val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val notificationBuilder = NotificationCompat.Builder(this, "my_fcm_default_channel")
            .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark) // Dummy icon
            .setContentTitle(remoteMessage.notification?.title ?: "")
            .setContentText(remoteMessage.notification?.body ?: "")
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setVibrate(longArrayOf(100, 200, 100, 200, 100, 200, 100))
            .setContentIntent(pendingIntent)
            .setDefaults(Notification.DEFAULT_ALL) // this line sets the default vibration and sound for notification

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel("my_fcm_default_channel",
                "Custom notification channel",
                NotificationManager.IMPORTANCE_HIGH)
            channel.setSound(defaultSoundUri, AudioAttributes.Builder().build()) // Not working
            channel.enableLights(true)
            channel.lightColor = Color.WHITE
            channel.enableVibration(true)
            channel.vibrationPattern = longArrayOf(100, 200, 100, 200, 100, 200, 100)
            notificationManager.createNotificationChannel(channel)
        }

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())

Rigonpa
  • 1
  • 1
  • Try this way to manage notification with sound https://stackoverflow.com/a/68487811/4042384 – gpuser Aug 02 '21 at 21:25
  • Thanks for the answer gpuser. As you can see in the payload I am using both “notification” and “data” keys in the cloud function (can not change it as that function is used also for ios devices). When both keys are used, entry notifications are managed directly by FCM. In this case onMessageReceived is not involved. – Rigonpa Aug 03 '21 at 08:06
  • https://stackoverflow.com/questions/41562823/fcm-remote-notifications-payload-for-ios-and-android#:~:text=But%20for%20some%20reason%2C%20the,receiving%20when%20app%20is%20active. – gpuser Aug 03 '21 at 09:48
  • Thanks for the link. You're right. I will change my cloud function. Actually I am using your initial answer for case 'App in foreground' where onMessageReceived() is involved. And still notifications arrive to device without sounding. I update my initial question with this case. Maybe you can help. Thanks any way! – Rigonpa Aug 03 '21 at 11:24

0 Answers0