Android notification, vibrates but no sound

2.2k views Asked by At

I've read some of the other posts on this subject and I think my code should be sounding an alarm, but it's not. It does vibrate, but no sound. Any suggestions on how to get this to convey sound ?

Another part of the program is able to play a ringtone so the problem seems to be specific this routine.

This is in a class that extends Service

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    if (sound == null) { Log.i("RECEIVER", "SOUND IS NULL"); }

    NotificationManager myNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    Intent intentMain = new Intent(this.getApplicationContext(), MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intentMain, 0);

    long[] pattern = {500,500,500,500,500,500,500,500,500};

    Notification myNote = new Notification.Builder(this)
            .setContentTitle("NotificationDemo")
            .setContentText("NotificatinDemo")
            .setSmallIcon(R.drawable.ic_launcher)
            .setSound(sound)
            .setVibrate(pattern)
            .setContentIntent(pIntent)
            .build();

    myNM.notify(1, myNote);
    return super.onStartCommand(intent, flags, startId);
}
3

There are 3 answers

1
Vijay On

First Check your device notification sound settings.

Then try this

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(someText)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setAutoCancel(true);
0
J.K On

Try this code

Notification myNote = new Notification.Builder(this)
            .setContentTitle("NotificationDemo")
            .setContentText("NotificatinDemo")
            .setSmallIcon(R.drawable.ic_launcher)
            .setSound(Notification.DEFAULT_SOUND)
            .setVibrate(pattern)
            .setContentIntent(pIntent)
            .build();
0
Ahmadul Hoq On

Try this:

Notification.Builder mBuilder = new Notification.Builder(this)
                .setContentTitle("NotificationDemo")
                .setContentText("NotificatinDemo")
                .setSmallIcon(R.drawable.ic_launcher)
                .setVibrate(pattern)
                .setContentIntent(pIntent);

Notification myNote = mBuilder.build();

if(Build.VERSION.SDK_INT >= 21) {
    myNote.sound = sound;
    myNote.category = Notification.CATEGORY_ALARM;

    AudioAttributes.Builder attrs = new AudioAttributes.Builder();
    attrs.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION);
    attrs.setUsage(AudioAttributes.USAGE_ALARM);
    myNote.audioAttributes = attrs.build();
} else  {
    mBuilder.setSound(sound, AudioManager.STREAM_ALARM);
    myNote = mBuilder.build();
}

myNM.notify(1, myNote);

Also check these: Lollipop notification feature, AudioAttributes class used in new notification system, and usage of Audio Attributes.