Android вибрация уведомления не работает - PullRequest
0 голосов
/ 18 марта 2020

Я пытаюсь установить уведомление, но что-то пошло не так, когда я установил вибрацию для своего уведомления. Даже при том, что это показывает уведомление, нет никакой вибрации. Я уже добавил пользовательское разрешение, android .permission.VIBRATE в манифесте вот код,

class MainActivity : AppCompatActivity() {

lateinit var notificationManager: NotificationManager
lateinit var notificationChannel: NotificationChannel
lateinit var builder: Notification.Builder
val channelId = "com.example.notificationtest"
val descr = "My notification"
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    init()
}

fun init() {

    val show = findViewById<Button>(R.id.show)
    notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    show.setOnClickListener {

        val intent = Intent(applicationContext, MainActivity::class.java)
        val pendingIntent =
            PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationChannel =
                NotificationChannel(channelId, descr, NotificationManager.IMPORTANCE_HIGH)
            notificationChannel.enableLights(true)
            notificationChannel.lightColor = Color.RED
            notificationChannel.enableVibration(true)
            notificationManager.createNotificationChannel(notificationChannel)

            builder = Notification.Builder(this, channelId)
                .setContentTitle("Android")
                .setContentText("new message")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentIntent(pendingIntent)
                .setCategory(NotificationCompat.CATEGORY_ALARM)
        }
        notificationManager.notify(0, builder.build())
    }
}}

1 Ответ

1 голос
/ 18 марта 2020

Пожалуйста, используйте следующий фрагмент кода для Android Уведомления с настройкой вибрации.

NotificationManager notificationManager;
NotificationCompat.Builder mBuilder = null;
String channelId = "com.example.notificationtest";
String descr = "My notification";
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent =
                PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel notificationChannel = new NotificationChannel(channelId, descr, importance);
            notificationChannel.enableLights(true);
            notificationChannel.enableVibration(true);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            assert notificationManager != null;
            notificationManager.createNotificationChannel(notificationChannel);

            mBuilder = buildNotification(this, "new Message", "Android", null, R.drawable.home, channelId);

            mBuilder.setChannelId(channelId);
        } else {
            mBuilder = new NotificationCompat.Builder(this, channelId);
            mBuilder.setSmallIcon(R.drawable.home);
            mBuilder.setContentTitle("Android")
                    .setContentText("new Message")
                    .setAutoCancel(false)
                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
        }
        notificationManager.notify(0, mBuilder.build());

И создайте новый метод, например,

 @TargetApi(Build.VERSION_CODES.O)
    public NotificationCompat.Builder buildNotification(Context mContext, String text, String fromnumber, PendingIntent pendingIntent, int icon, String channelId) {
        return new NotificationCompat.Builder(mContext, channelId)
                .setSmallIcon(icon)
                .setContentTitle(fromnumber)
                .setSubText(mContext.getString(R.string.app_name))
                .setContentText(text)
                .setAutoCancel(true)
                .setOngoing(true)
                .setAutoCancel(true);
    }
...