Notification notification = new NotificationCompat.Builder(this);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notification.setSmallIcon(R.drawable.icon_transperent);
notification.setColor(getResources().getColor(R.color.notification_color));
} else {
notification.setSmallIcon(R.drawable.icon);
I found a link where we can generate our own white icon,
try this link to generate white icon of your launcher icon.
Open this Link Android Asset Studio and upload your ic_launcher or notification icon
android 5.0以上通知栏、状态栏图标变成白色 - 晕菜一员 - 博客园
因为google在android5.0上面做了限制,为了统一系统风格。之后的状态栏icon就不能够随便用一张色彩丰富的图片了,只能够有白色和透明两个颜色出现。
5.0以上(不包含5.0),系统默认通知栏图标为系统启动图标,会自动将通知栏的图标(有色区域)全部填充为白色,
Android 的 notification 图标问题 - V2EX
因为原先的那种形式的图标不好看, 颜色太花, 在状态栏上一点也不好看.
并且这也是 Material Design 的规范了 ( https://www.google.com/design/spec/patterns/notifications.html) 实际上这样的改变在 Android 5.0 里好看了很多,
如果你看到有些厂商还不知道的, 那么就是他们菜或者是上班混日子.
Android 8.0 Oreo(API 26) 及更高版本 如何更新 消息通知渠道 设置的问题
解决方案:无!
谷歌官方文档说明了,一旦渠道创建完毕,那么最终管理权利就移交给用户了. 程序最多可以将其删除, 想修改是不可能的了.
ByYe:间接解决方案=根据title或者sound的不同,创建不同的渠道.来间接实现.
想重用一个渠道, 只是修改不同的部分,比如提示声音铃声不同.
https://developer.android.com/guide/topics/ui/notifiers/notifications#limits
从 Android 8.1(API 级别 27)开始,应用无法每秒发出一次以上的通知提示音。如果应用在一秒内发出了多条通知,这些通知都会按预期显示,但是每秒中只有第一条通知发出提示音。
不过,Android 还对通知更新频率设定了限制。如果您过于频繁地发布有关某条通知的更新(不到一秒内发布多个),系统可能会放弃部分更新。
How to properly update notification channel android oreo - Stack Overflow
Once a notification channel is created, the user has ultimate control over it's settings.
As the developer, you can only change the channel's title and description.
If you want to recreate the channel, you have to use a different id.
See: https://developer.android.com/training/notify-user/channels
Create and Manage Notification Channels | Android Developers
Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel
After you create a notification channel, you cannot change the notification behaviors—the user has complete control at that point. Though you can still change a channel's name and description.
But remember that once you create the channel, you cannot change these settings and the user has final control of whether these behaviors are active.
详情参见 参考资料1
采用的是使用自定义ContentProvider The Axe: Use a Custom ContentProvider
实际测试效果: Android 8.1 + Android 4.4.4 版本都正常.
解决步骤:
下载最新版本的cwac-provider-0.5.3.jar
并集成到项目中
GitHub - commonsguy/cwac-provider: CWAC-Provider: Helping to Make Content Providers Sane
按文档要求在清单文件里AndroidManifest.xml
添加了以下代码:
<provider
android:name="com.commonsware.cwac.provider.StreamProvider"
android:authorities="${applicationId}.DataFilesSoundsStreamProvider"
android:exported="true">
<meta-data
android:name="com.commonsware.cwac.provider.STREAM_PROVIDER_PATHS"
android:resource="@xml/data_files_sounds_paths" />
</provider>
将代码中原使用Uri.fromFile(file)
的地方替换为com.commonsware.cwac.provider.StreamProvider.getUriForFile(mContext.getPackageName() + ".DataFilesSoundsStreamProvider", file)
即可
问题还原步骤:
使用 NotificationCompat.Builder.setSound
设置自定义铃声
自定义铃声 位于 SDCard外置存储卡Environment.getExternalStorageDirectory()
某个目录里.
Android 6 之前的版本 正常 播放.
Android 7 以后的版本 无法 播放.
抛出异常:StrictMode: file:// Uri exposed through Notification.sound
java - Android 7.0 Notification Sound from File Provider Uri not playing - Stack Overflow
If you were using file:
Uri
values, they no longer work on Android 7.0 if your targetSdkVersion
is 24 or higher, as the sound Uri
is checked for compliance with the ban on file:
Uri
values.
假如你还在使用file:uri
形式的路径值, 那么Android 7.0以后的版本就不再支持了(只要targetSdkVersion
> 24)
However, if you try a content:
Uri
from, say, FileProvider
, your sound will not be played... because Android does not have read access to that content.
但是,假如你尝试使用content:
开头的Uri
对象,例如通过FileProvider
可以拿到这类对象,结果你的自定义通知铃声还是不能正常播放囧。原因是Android系统本身没有你指定的文件的读取权限。
解决方案有:
使用grantUriPermissions()
授权,让其具有读取权限The Scalpel: grantUriPermissions()
grantUriPermission("com.android.systemui", soundUri,Intent.FLAG_GRANT_READ_URI_PERMISSION);
缺点: 在于你不知道给谁授权,Android默认会是com.android.systemui
,至于其它厂商的系统是不是这个,就很难说了.
Thanks a lot for the thorough answer. To verify the robustness of the grantUriPermission solution ('The Scalpel'), I've tested it in different physical devices (including LG, Motorola, HTC, Samsung and Sony), from API 15 up to API 24. It works correctly on all of them, so the solution seems pretty solid (although certainly hacky). – jmart Sep 7 '16 at 18:24
不提供完全自定义的铃声,仅仅提供部分可选择的铃声 The Guillotine: No More User Files
原因是:android.resource
开头的Uri
对象路径Android系统默认是支持直接读取的,所以只要将部分可供选择的铃声放在APP的RAW资源目录里即可.
缺点:在于让用户无法完全自定义自己的铃声了,可能你的用户不一定会买账.
使用自定义ContentProvider The Axe: Use a Custom ContentProvider
FileProvider 不能直接用,因为一旦设置成 exported
那么一启动APP就会崩溃.
那么可用的方案就是自定义一个支持exported
且具有读取
权限的 ContentProvider
了.
可以参见: GitHub - commonsguy/cwac-provider: CWAC-Provider: Helping to Make Content Providers Sane
Using a read-only ContentProvider (see "The Axe") is the better answer. I'll probably rig up my StreamProvider such that if you mark it as exported, it treats everything as read-only, which would handle this problem fairly cleanly. – CommonsWare