Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Notification Builder: How to setSound so that sound plays in a looper

I am implementing Notification as below. Default Alarm sound is playing fine, but only for once. All i want is to play it repeatedly until the tap is registered.

 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("I Am Fine")
                    .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(NOTIFICATION_MESSAGE))     
                    .setContentText(NOTIFICATION_MESSAGE)
                    .setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);

            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
            mBuilder.setSound(alarmSound,AudioManager.STREAM_MUSIC);

The second parameter to setSound doesn't showing any effect. Please help !

like image 910
inderbagga Avatar asked Oct 29 '14 13:10

inderbagga


1 Answers

You have to use FLAG_INSISTENT for the notification. From the documentation:-

public static final int FLAG_INSISTENT

Bit to be bitwise-ored into the flags field that if set, the audio will be repeated until the notification is cancelled or the notification window is opened.

Constant Value: 4 (0x00000004)

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    Uri soundUri = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_RINGTONE);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            getApplicationContext()).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("title").setContentText("message")
            .setSound(soundUri); // This sets the sound to play

    Notification mNotification = mBuilder.build();

    mNotification.flags |= Notification.FLAG_INSISTENT;

    // Display notification
    notificationManager.notify(1, mNotification);
like image 164
Code Avatar answered Nov 22 '22 08:11

Code