Search code examples
androidandroid-notificationsandroid-notification-bar

Why do notification SOUNDS make the phone VIBRATE?


If I create a regular notification without sound, it works correctly. But if I add a sound, it vibrates. This is illogical so I don't understand it.

Here is an example of the code that works correctly (doesn't vibrate)

        Notification.Builder builder = new Notification.Builder(this);
    builder.setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0))
            .setOngoing(false);

    builder.setCategory(Notification.CATEGORY_SERVICE);
    builder.setVisibility(Notification.VISIBILITY_SECRET);
    builder.setPriority(Notification.PRIORITY_MAX);
    builder.setLights(0xFF00FF00, 1000, 0);
    Uri theUri = Uri.parse(uriString);
    //builder.setSound(theUri);
    builder.setContentText("Android sucks");
    builder.setAutoCancel(false);
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(FOREGROUND_ID, builder.build());

Here is the code that causes the phone to vibrate. Notice that vibration is NOT set in my notification:

        Notification.Builder builder = new Notification.Builder(this);
    builder.setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0))
            .setOngoing(false);

    builder.setCategory(Notification.CATEGORY_SERVICE);
    builder.setVisibility(Notification.VISIBILITY_SECRET);
    builder.setPriority(Notification.PRIORITY_MAX);
    builder.setLights(0xFF00FF00, 1000, 0);
    Uri theUri = Uri.parse(uriString);
    builder.setSound(theUri);
    builder.setContentText("Go buy an iPhone");
    builder.setAutoCancel(false);
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(FOREGROUND_ID, builder.build());

Call me irrational, but when I want the sound I'll set the sound and when I want vibration, I'll set the vibration. If I want both, I'll set both. So why is it that when I set the sound, I automatically get vibration?


Solution

  • This seemed to do the trick:

                long[] nullify = {0,0,0};
                builder.setVibrate(nullify);
    

    Although it lead to a number of additional frustrating anomalies. There are seven audio streams. They are:

        STREAM_RING;
        STREAM_NOTIFICATION;
        STREAM_ALARM;
        STREAM_MUSIC;
        STREAM_DTMF;
        STREAM_SYSTEM;
        STREAM_VOICE_CALL;
    

    Now, if I had to choose which stream I thought the notification sound would use, which stream do you think I'd guess? You are correct, I expect it to use STREAM_NOTIFICATION. What stream does it use? STREAM_SYSTEM. So if I want to be able to control the volume of my notification, I have to set the volume for STREAM_SYSTEM. But that of course has unwanted side effects like now that I changed that setting you can hear an audible sound when I click "like" on something in the facebook app, for example.

    Android is crazy messed up.