android - 即使我不构建通知,通知声音也会播放
问题描述
我已经在我的 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) {
}
}
解决方案
推荐阅读
- reactjs - 即使问题已在 React 应用程序中修复,TypeScript 也不会更改错误(终端输出)
- python - 正则表达式 python \1 \2 将字符串添加到结果文本的可能性
- node.js - 为什么在 res.redirect 之前使用 res.location?
- c++ - 列出与擦除不兼容的迭代器
- image - FPDF 在 fancytable 中包含图像
- python - 如何检查sqlite 3 python中的列中是否存在值
- raspberry-pi - 在 Raspberry Pi 上从 mubi.com 读取视频失败并出现 videojs 错误
- python - 使用数据透视表重新采样和重新组合
- android - Flutter:查找已停用小部件的祖先是不安全的
- django - 将 index.html 链接到 Django 中的其他 html 页面