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

5

11

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.

H answered 12/2, 2017 at 9:23 Comment(0)
K
8

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());
Kinghorn answered 22/2, 2017 at 14:20 Comment(0)
S
1

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.

Soutane answered 20/2, 2017 at 15:44 Comment(0)
N
1

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.

Nissen answered 22/2, 2017 at 4:57 Comment(5)
It looks like you and I think alike..this is similar to what I had tried...but two problems with this approach..this is not a clean solution since the code is interfering with system operation (if we have set phone to silent mode...once we recieve notification...phone will be in normal mode) and secondly...when i set back to normal or vibrate mode...there is an accompanying sound and vibrate pattern with that ringer mode transition respectively..which is interfering with the custom vibration i am setting..which is precisely what i want to bypassH
@H Then I would suggest you to cancel the system Vibration, Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); and then vibration.cancel(); during the notification. This shall cancel the vibration of system and then you can generate your custom vibration.Nissen
That is precisely my question :)H
@H Also, try notificationBuilder.setVibrate(new long[] { -1 }); and setting the type to Sound only, setDefaults(Notification.DEFAULT_SOUND) and not DEFAULT_ALL. I am assuming you are using a NotificationBuilder.Nissen
Nope i am not using that...and even then..this will control custom notification vibrate behaviour...not the system notification behaviourH
R
0

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.

Remembrance answered 13/2, 2017 at 18:52 Comment(2)
This is for a custom notification that we build. I am asking about overriding the system notification vibration and disabling/masking and re-enabling itH
What you're asking is disable/enable the vibration in android system? According to my knowledge, Android framework APIs does not have an option to disable all vibrations for all apps. Someone made an app that can turn them off system-wide - play.google.com/store/apps/… Note: A rooted phone is required, as there is no API for disabling vibrations. Hope this helps.Remembrance
W
0

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
Wilke answered 23/2, 2017 at 11:47 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.