Android8.0通知栏不显示
【Android8.0通知栏不显示】把targetSdkVersion改成26后通知栏不显示。
对于旧的代码,要修改两个地方
- 创建通知渠道
- 改用NotificationCompat
public class MainActivity extends AppCompatActivity {@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "chat";
String channelName = "聊天消息";
int importance = NotificationManager.IMPORTANCE_HIGH;
createNotificationChannel(channelId, channelName, importance);
channelId = "subscribe";
channelName = "订阅消息";
importance = NotificationManager.IMPORTANCE_DEFAULT;
createNotificationChannel(channelId, channelName, importance);
}
}@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel(String channelId, String channelName, int importance) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}public void sendChatMsg(View view) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this, "chat")
.setContentTitle("收到一条聊天消息")
.setContentText("今天中午吃什么?")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setAutoCancel(true)
.build();
manager.notify(1, notification);
}public void sendSubscribeMsg(View view) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this, "subscribe")
.setContentTitle("收到一条订阅消息")
.setContentText("地铁沿线30万商铺抢购中!")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setAutoCancel(true)
.build();
manager.notify(2, notification);
}
}
源码:https://gitee.com/obarong/TestNotification-android
参考 Android通知栏微技巧,8.0系统中通知栏的适配 - 郭霖的专栏 - CSDN博客
https://blog.csdn.net/guolin_blog/article/details/79854070
android 实现Service上传并在通知栏显示进度条 -
https://www.jianshu.com/p/2d0a93d1e197
推荐阅读
- 不舍
- Android|Android O 8.0及其以上系统的通知(Notification)、安装apk问题更新后的简单兼容写法
- 2018年执业药师考试各省份报名通知!(7月19日更新版)
- 紧急通知,活动无效
- KVO与通知的相同点与区别
- 麦穰村幼儿园五一劳动节放假通知及温馨提示
- Android-发送通知/含Android8.0+耐心照着官方教程实现,不是很难
- IOS|IOS swift设置通知、监听事件
- 万达电影5月线上活动通知汇总表(截止5.17)
- windows10定时调起系统通知,每一个小时提醒该喝水了...