Я проверил в своем методе 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) {
}
}