gpt4 book ai didi

android - 如何在android中的请勿打扰(DND)模式下发送带有声音和振动的通知

转载 作者:可可西里 更新时间:2023-11-01 11:42:30 26 4
gpt4 key购买 nike

当手机在 Android 设备上处于请勿打扰模式时,如何发送带有声音和振动的通知。我使用以下代码,当我的应用程序当前处于前台时它可以正常工作。

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());

当我的应用程序处于后台时,我还需要声音和振动通知。

最佳答案

一般来说你不能。甚至通知 MAX免打扰模式下不会显示优先级。

我能想到的可能的解决方法是使用 android.permission.SYSTEM_ALERT_WINDOW 在系统窗口上绘制自定义通知(FB Messanger 以类似的方式工作):Creating a system overlay window (always on top) .但此方法仅适用于极少数情况,大多数情况下它违反了 Android UI/UX 最佳实践。

关于android - 如何在android中的请勿打扰(DND)模式下发送带有声音和振动的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35700319/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com