- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题:我的应用程序需要可由用户自定义的通知。例如。为通知设置不同的声音、标题、文本
问题:我知道通知 channel 只设置一次并且不能修改,所以我想我可以只使用变量,但是即使使用变量,一旦我选择了一个声音它就会保持那个声音并且根本无法改变。我能想到的唯一解决方案是每次更改看起来很愚蠢的东西时都会有一个新 channel 。当然还有另一种方法??
此外,在 Android Pie 上,通知声音根本不起作用,我不明白为什么。
Uri soundUri = Uri.parse(notificationSound);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CH_ID")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(customTextTitle)
.setContentText(customTextBody)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if(soundUri != null){
// Changing Default mode of notification
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
// Creating an Audio Attribute
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
// Creating Channel
NotificationChannel notificationChannel = new NotificationChannel("CH_ID","Testing_Audio",NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(soundUri,audioAttributes);
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
mNotificationManager.notify(0, notificationBuilder.build());
}
最佳答案
我已经解决了这个问题,尽管是以一种 hacky 的方式:( 我最终将声音设置为 null if >=Oreo 并使用媒体播放器播放通知声音并将 audioStream 设置为 STREAM_NOTIFICATION。显然不理想,但作为这是一个通知而不是警报,音频不应超过在 onReceive 期间设置的执行任务的时间。
我可以看到此解决方案的唯一问题是用户是否决定将通知静音/调整手机上的 channel 。在那里静音显然不会影响媒体播放器播放的声音,而且很可能会显示为没有声音,这很不幸。
try {
mp.setDataSource(context.getApplicationContext(), soundUri);
mp.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
mp.prepare();
mp.start();
} catch (Exception e) {
//exception caught in the end zone
}
对于遇到此问题的任何其他人,我在这里找到了一个非常棒的帖子,它与我的解决方案相同,但更强大,更深入。 Android O - Notification Channels - Change Vibration Pattern or Sound Type
关于java - 奥利奥和馅饼通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54804290/
我创建了一项服务,可以在设备移动时跟踪其位置。该服务由与其绑定(bind)的 Activity 启动,在该 Activity 中有一个“Start Tracking”按钮。按下此按钮时,我需要服务在前
在 oreo 中如何启用通知 LED 闪烁而没有通知声音,如果我禁用通知声音 LED 不闪烁 Notification notification = new NotificationCompat.
我将 NotificationCompat.Builder 设置为: .setSound(getNotificationSound(), AudioManager.STREAM_ALARM) .set
我正在 Oreo AOSP 中开发一个简单的系统应用程序,以使用预定义的 SSID 和预共享 key 打开 wifi 热点。 因为我的应用程序是作为系统应用程序构建的,所以我不需要反射。 主 Acti
我正在开发一个物联网应用程序,其中有一个登录流程,用户连接到一个没有互联网连接的接入点,配置设备,然后连接到他的家庭 Wifi 网络。 Android 8 设备导致了一些问题,与接入点断开连接并重新连
根据 "developer.android.com" If the app targets Android 8.0 (API level 26), the system grants only REA
针对 Android O 的应用程序在使用服务时有一些新规则,其中之一是我们不能在应用程序处于后台时启动服务。 成为默认 SMS 应用程序的要求之一是:(来自 Telephony.java javad
作为测试,我从我们拥有的工作服务中删除了“startForeground(通知)”,它似乎仍在工作。 我们是否不需要在 Android O 及更高版本上发布工作服务的前台通知? 我无法找到这方面的具体
每当用户调用新电话时,我都试图在我的应用程序上触发通知。我在我的 Activity 中注册接收器并在 onDestroy() 方法中销毁它。以下是注册的代码 fragment registerRece
当应用程序位于前台时,我正在尝试使用 firebase 显示通知。当我从服务器推送通知时调用了 onMessageReceived 方法,但是通知没有显示。 这里是我的代码: public class
我正在尝试从相机 Intent 获取图像。它运行良好,但仅限于我的设备(oreo/8.1.0)。我生成未签名的 apk 并尝试在其他设备(pie 和 lolipop)中运行,但它不起作用。 我搜索了
我需要为我的应用程序中的所有屏幕禁用 Activity 过渡动画。以前的解决方案适用于所有 Android 版本: @null ...但对于 Android 8“Oreo”,每次转换(向前
我有一个相对简单的设置,应该在一天中的特定时间触发警报并向用户显示通知。这是相关代码, 设置闹钟 long inTime = /*expirationTime*/ Calendar.getInsta
https://android-developers.googleblog.com/2017/07/seccomp-filter-in-android-o.html 正如本文的“seccomp 过滤器
我在 google play 商店收到这条消息 - 这是这个应用程序的 list - Gradle 配置- defaultConfig { minSdkVersion 16
我有一个监视 wifi 连接的小部件,因此我启动了一个服务来启动广播接收器来检测网络变化。一切正常,除非我退出主应用程序:服务停止。 因此,我在小部件中启动了一个警报管理器,该管理器大约每分钟唤醒一次
我的代码: mNotifyBuilder.setSmallIcon(R.drawable.ic_status_bar) .setContentTitle("")
我的应用在 Google Play 上似乎存在某些 Android 8.0 设备的特定错误。 不幸的是,到目前为止我还无法自己复制该错误。 这是错误日志: android.content.res.Re
根据background execution limits在 Android Oreo 中引入,调用 startService当应用程序在后台时应该抛出一个 IllegalArgumentExcept
我目前正在使用 android api 级别 26(Nexus 6P)中可用的电话管理器(USSD 响应)。对于单步 ussd session ,它正在运行。 引用: http://codedrago
我是一名优秀的程序员,十分优秀!