java - 如何让通知声音持续播放并仅在单击通知的“取消通知”按钮时停止
问题描述
我有此代码用于为我的应用程序创建通知,问题是当我收到通知时,当我打开通知面板时声音立即停止!如果手机被锁定,按电源按钮唤醒它也会停止声音。我想要的是获得一个持续的通知声音,只有当我点击触发 CancelNotification.class' onReceive 的通知的“取消通知”按钮时才会停止
//这是我的 SDK > 0 的通知通道
@TargetApi(Build.VERSION_CODES.O)
private void createChannel(String channelDesc, Boolean isSoundDisabled, String channelID, String channelName) {
Uri uri = null;
AudioAttributes attributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build();
if (!isSoundDisabled)
uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/" + R.raw.adhan);
NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
channel.setDescription(channelDesc);
channel.enableVibration(true);
channel.setSound(uri, attributes);
channel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
getManager().createNotificationChannel(channel);
}
//通知生成器
public NotificationCompat.Builder getChannelNotification(String title, String description, String channelID) {
PendingIntent mainIntentPending = PendingIntent.getActivity(this, 2, new Intent(), 0);
Intent buttonIntent = new Intent(this, CancelNotification.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 3, buttonIntent, 0);
return new NotificationCompat.Builder(getApplicationContext(), channelID)
.setContentTitle(title)
.setContentText(description)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setContentIntent(mainIntentPending)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(false)
.setOngoing(true)
.addAction(0, "Cancel notification", pendingIntent)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.icon);
}
//接收
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPreferences = context.getSharedPreferences("settings_prefs", Context.MODE_PRIVATE);
boolean isSoundDisabled= sharedPreferences.getBoolean("enableSound", false);
sharedPreferences.edit().putInt("notificationId", 1).apply();
notificationHelper = new NotificationHelper(context, "Channel description", isSoundDisabled, "NotifID", "ChannelName");
nb = notificationHelper.getChannelNotificationFajr("Notification Name", "Notification description", "NotifID");
Uri uri = null;
if (!isSoundDisabled)
uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.notificationsound);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
nb.setSound(uri);
}
notificationHelper.getManager().notify(1, nb.build());
}
解决方案
推荐阅读
- azure-sql-database - Sql Server Management Studio v18.4 - 无法在 Azure SQL Server 上编辑属性
- arrays - Swift从二维数组中获取字符串数组
- flutter - 在颤动上导航时如何收听流?
- snowflake-cloud-data-platform - Am I reclustering this table correctly? Variant type
- java - 使用 MimeMultipart、Java 8 发送带有多个嵌入图像的邮件
- snowflake-cloud-data-platform - Snowflake 存储过程中的最大 JavaScript 字符串大小
- css - 旋转选择器输入隐藏下拉菜单
- excel - 我如何快速创建逗号分隔的 ID 列表,而不是遍历行?
- java - 在 Java 中将列表数组转换为数据表
- python - 点击另一个按钮后井字游戏按钮图像消失