【问题标题】:Notification not vibrating and led isn't blinking通知不振动且 LED 不闪烁
【发布时间】: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)

【问题讨论】:

    标签: android firebase kotlin notifications


    【解决方案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 也不能正常工作
    猜你喜欢
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    相关资源
    最近更新 更多