Rigonpa
Rigonpa

Reputation: 1

FCM Push Notifications Stop Sounding In Android Device

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())

Upvotes: 0

Views: 196

Answers (0)

Related Questions