赞
踩
如有不对的地方,望各路大神指导,小女子在此谢过(*^__^*) 嘻嘻……
最近遇到一个通知栏的问题,费了我一天多的时间,哎,小姐姐我也表示很苦啊
经测试我这个目前在 vivo,华为 8.0,9.0 都是没有问题的, 后续有问题,再继续更新
默认通知栏设置
1. 安卓 9.0 and 8.0
主要是多了一个NotificationChannel的设置
NotificationChannel设置代码
- NotificationChannel channel = new NotificationChannel(context.getPackageName(), YOUR_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
- channel.enableLights(true);
- channel.setLightColor(context.getResources().getColor(R.color.color_CCCCCC));
- channel.setShowBadge(true);
- channel.setDescription(context.getString(R.string.app_name));
- // 设置显示模式
- channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
- notificationManager.createNotificationChannel(channel);
完整代码
- final Intent resultIntent = new Intent(context, BootActivity.class);
- resultIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
- PendingIntent resultPendingIntent = PendingIntent.getActivity(
- context, 0, resultIntent,
- PendingIntent.FLAG_UPDATE_CURRENT);
- try {
- notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- //android 8.0 兼容9.0
- NotificationChannel channel = new NotificationChannel(context.getPackageName(), YOUR_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
- channel.enableLights(true);
- channel.setLightColor(context.getResources().getColor(R.color.color_CCCCCC));
- channel.setShowBadge(true);
- channel.setDescription(context.getString(R.string.app_name));
- // 设置显示模式
- channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
- notificationManager.createNotificationChannel(channel);
- builder = new NotificationCompat.Builder(context, context.getPackageName());
- //设置小图标
- builder.setSmallIcon(R.drawable.icon);
- builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon));
- //设置优先级,低优先级可能被隐藏
- builder.setPriority(NotificationCompat.PRIORITY_HIGH);
- //设置通知时间,默认为系统发出通知的时间,通常不用设置
- builder.setWhen(System.currentTimeMillis());
- //设置通知栏能否被清楚,true不能被清除,false可以被清除
- builder.setOngoing(false);
- builder.setContentTitle("记账提醒");
- builder.setGroupSummary(true).setGroup(context.getString(R.string.app_name));
- builder.setContentText("记账时间到了,赶快记一笔");
- builder.setAutoCancel(true);//用户点击就自动消失
- builder.setChannelId(App.getContext().getPackageName());
- builder.setContentIntent(resultPendingIntent);
- builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
- builder.setDefaults(NotificationCompat.DEFAULT_ALL);
- builder.setCategory(Notification.CATEGORY_REMINDER);
- builder.setOnlyAlertOnce(true);
- notification = builder.build();
- } else {
- //其余版本
- builder = new NotificationCompat.Builder(context);
- //设置小图标
- builder.setSmallIcon(R.drawable.tip);
- //设置通知标题
- builder.setContentTitle("记账提醒");
- //设置通知类容
- builder.setContentText("记账时间到了,赶快记一笔");
- // 设置优先级,低优先级可能被隐藏
- builder.setPriority(NotificationCompat.PRIORITY_HIGH);
- //设置通知时间,默认为系统发出通知的时间,通常不用设置
- builder.setWhen(System.currentTimeMillis());
- //设置通知栏能否被清楚,true不能被清除,false可以被清除
- builder.setOngoing(false);
- builder.setAutoCancel(true);//用户点击就自动消失
- builder.setContentIntent(resultPendingIntent);
- notification = builder.build();
- }
- //发布通知
- notificationManager.notify(101, notification);
- } catch (Exception e) {
- e.printStackTrace();
- }
注意: 设置ICON的图片一定要够规范,最好就是使用app的log图片,(血泪教训啊,我就是设置的时候随便写了张,结果找了好久的才发现是你这个icon图片的原因)
不然会报错,
Couldn't expand RemoteViews for
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。