Пожалуйста, используйте следующий фрагмент кода для 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);
}