Android 通知变更记录
Android从5.0 对于通知栏图标的设计进行了修改。通知栏的大图标没有变化,主要是右下角的小图标,只能使用alpha图层来进行绘制,而不应该包括RGB图层。
什么叫作只使用alpha图层来进行绘制呢?其实通俗点来讲,就是让我们的通知栏图标不要带颜色就可以了。
这里的效果可参考博客:https://blog.csdn.net/guolin_blog/article/details/50945228
Android 8.0 通知渠道的添加 可参考 https://www.jianshu.com/p/559c30cdeed0
下面直接贴出代码 适配4.0 - 8.0 下面三段代码 直接拷贝走使用即可 第一段代码放在你想创建通知的地方 第二、三段代码是两个方法,可以拷贝到你当前类或者工具类里使用 里面的context 和图标资源替换成自己的就可以使用了
//TODO 生成一条通知 并显示
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//大于等于8.0时 设置通知渠道 这里只写了一个渠道 id 是 "notify" name "重要通知"
String channelId = "notify";
String channelName = "重要通知";
int importance = NotificationManager.IMPORTANCE_HIGH;
createNotificationChannel(channelId, channelName, importance);
}
sendMsg("您的免费试用已到期");
/**
* 生成通知消息 EasyVariable.mContext 替换成Application的context就行
*/
public static void sendMsg(String content) {
LogUtil.i(" ============= sendMsg");
NotificationManager manager = (NotificationManager) EasyVariable.mContext.getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(EasyVariable.mContext, "notify");
builder.setContentTitle("通知测试");
builder.setContentText(content);
builder.setWhen(System.currentTimeMillis());
//调用系统默认响铃,设置此属性后setSound()会无效
builder.setDefaults(Notification.DEFAULT_SOUND);
//图片资源请自行替换成自己的
builder.setSmallIcon(R.drawable.notification);
builder.setColor(Color.parseColor("#F2C239"));
//图片资源请自行替换成自己的
builder.setLargeIcon(BitmapFactory.decodeResource(EasyVariable.mContext.getResources(), R.drawable.webcheck_logo));
builder.setAutoCancel(true);
Intent intent = new Intent(EasyVariable.mContext, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(EasyVariable.mContext, 0, intent, 0);
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
manager.notify(1, notification);
}
//大于等于8.0 创建通知渠道的方法 这里的EasyVariable.mContext替换成Application的context就可以
@TargetApi(Build.VERSION_CODES.O)
public static void createNotificationChannel(String channelId, String channelName, int importance) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
NotificationManager notificationManager = (NotificationManager) EasyVariable.mContext.getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}