通知不振动且 LED 不闪烁
Posted
技术标签:
【中文标题】通知不振动且 LED 不闪烁【英文标题】:Notification not vibrating and led isn't blinking 【发布时间】:2021-08-28 22:09:50 【问题描述】:这是我的代码,由onMessageReceived
函数调用。
我已经在清单文件中添加了振动权限。我错过了什么?手机在正常声音模式下没有振动,并且完全没有led闪烁。
private fun showNotification(title: String?, body: String?)
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(
this, 0, intent,
PendingIntent.FLAG_ONE_SHOT
)
val notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.applogoo_round)
.setLargeIcon(icon)
.setContentTitle(title)
.setColor(Color.argb(1, 92, 221, 198))
.setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))
.setLights(Color.BLUE, 3000, 3000)
.setContentText(body)
.setAutoCancel(true)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(pendingIntent)
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(0, notificationBuilder.build())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
val serviceChannel = NotificationChannel(
CHANNEL_ID, "Foreground Service Channel",
NotificationManager.IMPORTANCE_HIGH
)
serviceChannel.enableLights(true)
serviceChannel.lightColor=Color.YELLOW
serviceChannel.enableVibration(true)
serviceChannel.shouldVibrate()
serviceChannel.vibrationPattern
serviceChannel.lockscreenVisibility
val manager = getSystemService(NotificationManager::class.java)
manager!!.createNotificationChannel(serviceChannel)
【问题讨论】:
【参考方案1】:在收到通知时尝试使用自定义振动
private fun vibrate()
val mVibratePattern = longArrayOf(
0, 400, 800, 600, 800, 800, 800, 1000, 400, 800, 600,
800, 800, 800, 1000, 400, 800, 800, 600, 800, 800, 800, 1000, 400, 800
)
val v = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
Handler(Looper.getMainLooper()).postDelayed(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
v.vibrate(
VibrationEffect.createWaveform(
mVibratePattern,
VibrationEffect.DEFAULT_AMPLITUDE
)
)
else //deprecated in API 26
v.vibrate(1000)
, 1000)
【讨论】:
非常感谢 Dharmender Manral 它解决了我的问题你能推荐一些 LED 灯吗?我的 LED 也不能正常工作以上是关于通知不振动且 LED 不闪烁的主要内容,如果未能解决你的问题,请参考以下文章
Ionic 3 本地通知 LED/声音在 Android 中不起作用