Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 324
SoulRayder Avatar asked Feb 12 '17 09:02

SoulRayder


2 Answers

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());
like image 132
Assem Mahrous Avatar answered Oct 01 '22 23:10

Assem Mahrous


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.

like image 40
shelll Avatar answered Oct 02 '22 01:10

shelll