Reputation: 21
I am trying to set notification but something gone wrong when I set vibration for my notification. Even though it still shows notification there is no vibration. I have already added user-permission, android.permission.VIBRATE in manifest here is the code,
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())
}
}}
Upvotes: 1
Views: 2658
Reputation: 231
Please use the following code snippet for Android Notification with Vibration setup.
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());
And create a new method like,
@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);
}
Upvotes: 1