How to send a notification with sound and vibration in do not disturb(DND) mode in android

我只是一个虾纸丫 提交于 2020-05-27 12:20:57

问题


How can I send a notification with sound and vibration when phone is in do not disturb mode on Android Devices. I use the following code, and it is working when my application is currently in foreground.

PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
            resultIntent, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle("Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText("You've received new message."))
            .setContentText("You've received new message.");

    // FOR SILENT MODE
    AudioManager am = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
    // For Normal mode
    am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    am.setStreamVolume(AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);

    mBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });

    // Set Vibrate, Sound and Light
    int defaults = 0;
    defaults = defaults | Notification.DEFAULT_LIGHTS;
    // defaults = defaults | Notification.DEFAULT_VIBRATE;
    // defaults = defaults | Notification.DEFAULT_SOUND;
    mBuilder.setDefaults(defaults);
    mBuilder.setSound(Uri.parse("android.resource://" + getPackageName()
    + "/" + R.raw.siren));

    // Cancel the notification after its selection
    mBuilder.setAutoCancel(true);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

I also want notifications with sound and vibration when my app is in background.


回答1:


Generally speaking you can't. Even notification with MAX priority wont be shown in DND mode.

The possible workaround that I can imagine is to use android.permission.SYSTEM_ALERT_WINDOW to draw custom notification over system window (FB Messanger works in similar way) : Creating a system overlay window (always on top) . But this method is suitable only in rare cases and most of the time it's a violation of Android UI/UX best practices.




回答2:


Ok good news you are half way there.

I achieved this previously by using AudioManager & VIBRATOR_SERVICE (yea I know ;)

The idea is not to use NotificationCompat.Builder because it will rely on system settings and if device is in silent mode, it won't vibrate nor play sound. You have to manually play a sound using AudioManager & vibrate using VIBRATOR_SERVICE.

You are using AudioManager & setting the properties but you are never actually asking it to play the sound. So, in your code its never actually utilized.

Here's an example of how to play a sound using AudioManager, it will also ignore silent mode:

Here's an example of using VIBRATOR_SERVICE:

Combine these 2 approaches and ditch NotificationCompat.Builder




回答3:


If you can get right permissions to change settings of device, then you could show notifications with sound and vibration at your apps. Also check this out.



来源:https://stackoverflow.com/questions/35700319/how-to-send-a-notification-with-sound-and-vibration-in-do-not-disturbdnd-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!