【问题标题】:Notifications vibration are not working in Android通知振动在 Android 中不起作用
【发布时间】:2021-06-18 11:26:11
【问题描述】:

我正在尝试为 android 实现通知。我可以触发通知,但手机没有收到通知振动。这是示例代码。

String CHANNEL_ID = "Channel1";
            inboxStyle.addLine("Alex Faarborg  Check this out");
            inboxStyle.setBigContentTitle(messagesCount + " new messages");
            inboxStyle.setSummaryText("Chat room");

            Notification summaryNotification =
                    new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
                            .setContentTitle("Chatroom has new messages")
                            .setContentText(messagesCount+" new messages")
                            .setSmallIcon(R.drawable.ic_launcher_background)
                            .setStyle(inboxStyle)
                            .setGroupSummary(true)
                            .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);;

            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableVibration(true);
            channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            notificationManager.createNotificationChannel(channel);
            notificationManager.notify(SUMMARY_ID, summaryNotification);

【问题讨论】:

  • 确保您的手机没有处于静音状态
  • 尝试卸载应用并重新安装。
  • @Ctrl_see ,感谢您的建议。它现在正在工作。
  • 很高兴我能帮上忙。
  • @SamChen,谢谢,当我卸载并重新安装该应用程序时它正在工作。

标签: android android-notifications


【解决方案1】:

试试这个 振动器 customVibration = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(2000);

您可以将振动器对象设置为通知对象或通知构建器。 玩得开心:)!

【讨论】:

    【解决方案2】:

    尝试使用以下自定义振动器。

     private fun customVibrate() {
      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)
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-25
      • 1970-01-01
      • 1970-01-01
      • 2022-11-06
      相关资源
      最近更新 更多