通知不振动且指示灯不闪烁



这是我的代码,它由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)

在获得通知时尝试自定义振动

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)
}
}

最新更新