SoulRayder
SoulRayder

Reputation: 5166

How to disable/re-enable the vibration that occurs on receiving notification?

I am defining a custom vibration for a specific functionality when notification is received.

However, when the phone screen is off, the custom vibration plays along with the default notification vibration.

I tried to put phone to silent mode and remove it from silent mode programmatically and also tried to use a partial wakelock, suspecting that when the CPU is off, then the default vibration is also thrown, but both approaches don't seem to work.

I also tried to mute the default audio and the vibration and restore it upon completion of my task on receiving a notification, but that only helps in masking the default notification sound, not the default vibration.

//Trying to mute the notification sound and vibration
audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
 //Restoring the defaults
audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, defaultNotifVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);

Please let me know how to disable/enable the default notification vibration programmatically.

Upvotes: 11

Views: 6499

Answers (5)

Ajay Venugopal
Ajay Venugopal

Reputation: 1710

Try this to make the disable the vibration for receiving notifications

notificationBuilder.setDefaults(Notification.DEFAULT_LIGHT | Notification.DEFAULT_SOUND)
                   .setVibrate(new long[]{0l}); // Passing null here silently fails

Upvotes: 0

Assem Mahrous
Assem Mahrous

Reputation: 421

try to do this

NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this);

this will lead to a pattern of vibrate use the notification Builder and remove the set vibrate ,this will disable the vibrate

this is my example of handling the sound and vibrate ex:

Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText(body)
                .setContentTitle(title)
                .setSound(soundUri)
                .setLargeIcon(icon)
                .setSound(soundUri)
                .setLights(Color.parseColor("#ffb400"), 50, 10)
                .setVibrate(new long[]{500, 500, 500, 500})
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());

Upvotes: 8

Q2x13
Q2x13

Reputation: 534

What I assume is that you want to toggle the ringer mode to vibrate or ringer or silent.

For your notification, set the RINGER_MODE to SILENT before the notification and set it back to NORMAL after the notification.

In Android you need the permission to change the device settings to toggle any setting parameter. Start with adding,

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>,

to the manifest.

Now, use AudioManager to get what you want to achieve,

AudioManager aManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
aManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

You can have, RINGER_MODE_VIBRATE, RINGER_MODE_SILENT or RINGER_MODE_NORMAL.

Also add, Permission android.permission.VIBRATE in the manifest.

Upvotes: 1

shelll
shelll

Reputation: 3373

This is not possible (maybe with root?).

Apps cannot interfere with other apps so even if it would be possible it would be against the Google Play rules.

Upvotes: 1

albeee
albeee

Reputation: 1472

Here you go

public void enableVibration(NotificationCompat.Builder builder, long[] customPattern){
    builder.setVibrate(customPattern);
}

public void disableVibration(NotificationCompat.Builder builder){
    builder.setVibrate(new long[]{0L});
}

Hope this helps.

Upvotes: 0

Related Questions