0

I have checked in my onMessageReceived method whether the app is in foreground or background and I only show the notification when app is in background or the app is not visible, the notification is working fine but in both cases the notification sound gets played I don't want the notification sound to get played when the app is in foreground. Here is the code for onMessageReceived

   override fun onMessageReceived(p0: RemoteMessage?) {
        var sh = SharedPreferencesHelper.getInstance(applicationContext, Constants.PREFS)
        if (sh.getInt(Constants.LOGIN_STATE)!=Constants.STATE_COMPLETED) {
            return
        }

        var data = p0?.data
        val send = Intent(Constants.NOTIFICATION_RECEIVED)
        // You can also include some extra data.
        send.putExtra(Constants.COUNT, data?.get(Constants.COUNT))
        LocalBroadcastManager.getInstance(this).sendBroadcast(send)
        var title = p0?.data?.get("title")
        var message = p0?.data?.get("message")

        var intent: Intent? = null
        if (data?.get("status") == "follow") {
            intent = Intent(this, ViewProfileActivity::class.java)
            intent.putExtra(Constants.PROFILE_ID, data?.get("userid"))
            intent.putExtra(Constants.SEEN_ID, data?.get("id"))

            intent.putExtra(Constants.SEEN, "")
        } else if (data?.get("status") == "reply" || data?.get("status") == "direct reply") {

            val notify = Intent(Constants.REPLIES_AVAILABLE)
            // You can also include some extra data.
            LocalBroadcastManager.getInstance(this).sendBroadcast(notify)
            intent = Intent(this, NowwtReplyActivity::class.java)
            intent.putExtra(Constants.NOWWT_ID, data?.get("nowwtid"))
            intent.putExtra(Constants.SEEN_ID, data?.get("id"))

            intent.putExtra(Constants.SEEN, "")
        } else if (data?.get("status") == "direct nowwt") {
            intent = Intent(this, NowwtReplyActivity::class.java)
            intent.putExtra(Constants.DIRECT_NOWWT, true)
            intent.putExtra(Constants.NOWWT_ID, data?.get("nowwtid"))
            intent.putExtra(Constants.SEEN_ID, data?.get("id"))
            intent.putExtra(Constants.SEEN, "")
        } else if(data?.get("status") == "people"){
            intent = Intent(this, DashboardActivity::class.java)
            intent.putExtra(Constants.NAVIGATE_PEOPLE,true)


        } else if (data?.get("status") == "simple") {
            intent = Intent(this, SplashActivity::class.java)
        }

        intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        try {
            if (FireNotificationUtils.isAppIsInBackground(applicationContext)) {
                val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
                val notificationBuilder = NotificationCompat.Builder(applicationContext, "yoyo")
                notificationBuilder.setContentTitle(title)
                notificationBuilder.setSmallIcon(R.mipmap.ic_launcher_round_nowwt)
                notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(applicationContext.resources,
                        R.drawable.logo_))
                notificationBuilder.setContentText(message)
                notificationBuilder.setSmallIcon(R.mipmap.ic_launcher)
                notificationBuilder.setDefaults(0)
                notificationBuilder.priority = NotificationCompat.PRIORITY_MAX
                notificationBuilder.setAutoCancel(true)
//        notificationBuilder.setSound(defaultSoundUri)
                notificationBuilder.setContentIntent(pendingIntent)
                val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    val CHANNEL_ID = "my_channel_01"// The id of the channel.
                    val name = getString(R.string.channel_name)// The user-visible name of the channel.
                    val importance = NotificationManager.IMPORTANCE_HIGH
                    val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
                    notificationBuilder.setChannelId(CHANNEL_ID)
                    notificationManager.createNotificationChannel(mChannel)
                }

                notificationManager.notify(0, notificationBuilder.build())
                super.onMessageReceived(p0)
            }
        } catch (e: Exception) {

        }


    }
Rohit Sharma
  • 1,164
  • 9
  • 17
  • are you sure your onMessageReceived is called in foreground as well as background?? which version are u using?? – Pemba Tamang Dec 03 '18 at 05:52
  • yes the logic which I have written inside onMessageReceived gets excecuted in both cases and even if the app is not running – Rohit Sharma Dec 03 '18 at 05:59
  • everything is working fine only the notification sound plays even if it surpasses the if (FireNotificationUtils.isAppIsInBackground(applicationContext)) – Rohit Sharma Dec 03 '18 at 06:00
  • implementation 'com.google.firebase:firebase-messaging:11.8.0' implementation 'com.google.firebase:firebase-core:11.8.0' – Rohit Sharma Dec 03 '18 at 06:03
  • I am using the above versions of firebase apis – Rohit Sharma Dec 03 '18 at 06:03
  • did you disable the sound in the console under the "addtional options"? – Pemba Tamang Dec 03 '18 at 06:12
  • ok console has option to disable the notification sound.?? – Rohit Sharma Dec 03 '18 at 07:50
  • https://ibb.co/hX73jKW – Pemba Tamang Dec 03 '18 at 07:54
  • I am am making the notification in my onMessageReceived method I am using an Api there to send push notifications to the user I am not sending the notification from the console – Rohit Sharma Dec 03 '18 at 09:58
  • which api is it? – Pemba Tamang Dec 03 '18 at 10:05
  • I have to manage it at the frontend btw I am using https://fcm.googleapis.com/fcm/send – Rohit Sharma Dec 03 '18 at 10:14
  • ok. I am surprised that onMessage is called everytime as according to what I have seen and the accepted answer here https://stackoverflow.com/questions/37358462/firebase-onmessagereceived-not-called-when-app-in-background onMessageReceived is not called in the background. The data is sent in the intent opening the launcher activity. That being said you are pushing **notifications** and it seems like the default sound of your device is being played from there. check the json sent by your api you might find your problem there. – Pemba Tamang Dec 03 '18 at 10:36
  • I have just tested that while the app is in background the onMessageReceived is getting called , it is just that when the app is killed we cant set debugger in onMessageReceived, It is a service and it runs for lifetime and receives the callbacks from the Fcm Api – Rohit Sharma Dec 03 '18 at 12:03

0 Answers0