Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotificationCompat setSound(sound, STREAM_ALARM) not working

Im building a ping feature for finding a lost phone through bluetooth. I need the phone to sound even though it is set to mute/silent like how the alarm usually works. I thought I could put the streamtype of my notification to AudioManager.STREAM_ALARM but its not working. It only sounds when the phones sound is on. This is how I set it:

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSmallIcon(R.drawable.ic_spenwallet)
        .setContentTitle("Ping")
        .setContentText("Device is trying to find your phone.")
        .setAutoCancel(false)
        .setSound(sound, STREAM_ALARM)
        .setVibrate(vibratePattern)
        .addAction(cancelAction);

If I try :

    Notification notification = builder.build();
    notification.audioStreamType = AudioManager.STREAM_ALARM;

Im getting a warning from Android Studio that audioStreamType is deprecated. Is this the case? Any other way to make the notificaiton sound even though silent mode is on? (preferable also vibrate)

I got it working by creating a dedicated mediaplayer for the purpose but I don't think this should be needed. Heres how I did it anyway:

    MediaPlayer mediaPlayer = new MediaPlayer();
    final String packageName = getApplicationContext().getPackageName();
    Uri sound = Uri.parse("android.resource://" + packageName + "/" + R.raw.ping_sound);

    try {
        mediaPlayer.setDataSource(this, sound);
    } catch (IOException e) {
        e.printStackTrace();
    }

    final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        mediaPlayer.setLooping(false);
        try {
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer.start();
    } 
like image 845
nilsi Avatar asked Nov 08 '22 02:11

nilsi


1 Answers

Using builder.setSound(alarmSound, AudioManager.STREAM_AUDIO) was exactly what I needed to keep my alarm going! Perhaps your issue is with the R.raw.ping_sound sound sample that you are using. After trying a bunch of terrible implementations I found online for alarm notifications (which is where I found Settings.System.DEFAULT_RINGTONE_URI) I followed the official notification documentation and then used the NotificationCompat.Builder documentation for customization.

Here is my working alarm notification:

private void showNotification(){
    // Setup Intent for when Notification clicked
    Intent intent = new Intent(mContext, MedsActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // See https://developer.android.com/training/notify-user/navigation for better navigation
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);

    // Setup Ringtone & Vibrate
    Uri alarmSound = Settings.System.DEFAULT_RINGTONE_URI;
    long[] vibratePattern = { 0, 100, 200, 300 };

    // Setup Notification
    String channelID = mContext.getResources().getString(R.string.channel_id_alarms);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, channelID)
            .setContentText(notificationMessage)
            .setContentTitle(notificationTitle)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setCategory(NotificationCompat.CATEGORY_ALARM)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentIntent(pendingIntent)
            .setSound(alarmSound, AudioManager.STREAM_ALARM)
            .setOnlyAlertOnce(true)
            .setVibrate(vibratePattern)
            .setAutoCancel(true);

    // Send Notification
    NotificationManager manager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
    manager.notify(NOTIFICATION_ID, mBuilder.build());
}
like image 185
brenthompson2 Avatar answered Nov 14 '22 23:11

brenthompson2