Android8.0 新增通知栏功能

又到了和guolin大神学习的时间,遥想上次仿佛就在昨年。

8.0版本还有一个改变的是界面图标默认是圆形,如果没有对8.0图标进行适配的话很丑

这个新功能出现的背景用一句话来概括就是。我只想要我需要的通知。

Android sdk26版本加入了通知渠道的概念。开发者可以自由创建通知渠道,但是这些通知渠道的控制权都是掌握在用户手上的。用户可以自由地选择这些通知渠道的重要程度,是否响铃、是否振动、或者是否要关闭这个渠道的通知。

直接附上代码

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);
//这样写不用写两套,即8.0以上 8.0以下
        Notification notification = new NotificationCompat.Builder(this, "chat")
                .setContentTitle("收到一条聊天消息")
                .setContentText("今天中午吃什么?")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.icon)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
                .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.drawable.icon)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
                .setAutoCancel(true)
                .build();
        manager.notify(2, notification);
    }
    
}

渠道名称是给用户看的,渠道id全局唯一,重要等级的不同则会决定通知的不同行为,比如


直接弹出

只在小窗口弹出

有个问题

如果用户错手将非常重要的订阅消息的渠道关闭后,那么这显然是不合理的。

解决方法就是先判断渠道是否被关闭,如果关闭的话提示用户手动开发渠道。

     if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
                Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
                intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId());
                startActivity(intent);
                Toast.makeText(this, "请手动将通知打开", Toast.LENGTH_SHORT).show();
            }

如果想了解 关于通知的延时提醒和打开全部权限还有未读图标的,可以查看https://blog.csdn.net/guolin_blog/article/details/50945228

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,007评论 25 709
  • 摘自郭神的文章: 从Android 8.0系统开始,Google引入了通知渠道这个概念。 什么是通知渠道呢?顾名思...
    D___Will阅读 3,367评论 0 1
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,107评论 19 139
  • 今天写个小故事。 钓鱼的人喜欢到河边钓鱼说是钓鱼但是每次都是把钓起来的鱼放了回去… 后来钓鱼的人得了怪病日益病重的...
    式小小阅读 323评论 0 0
  • 有一句话是这样说的,每个人表面看着光鲜亮丽,其实内心都各有各的烦恼。谁能说,不是呢?每个阶段有每个阶段的烦恼:读书...
    青简风阅读 1,853评论 0 4