首页 > 解决方案 > 即使我不构建通知,通知声音也会播放

问题描述

我已经在我的 onMessageReceived 方法中检查了应用程序是在前台还是后台,并且我只在应用程序在后台或应用程序不可见时显示通知,通知工作正常,但在这两种情况下,通知声音都会播放我不'不希望在应用程序处于前台时播放通知声音。这是 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) {

        }


    }

标签: androidfirebasenotifications

解决方案


推荐阅读