Android 8.0系统的通知栏适配

资料
google 通知文档
一起来学习Android 8.0系统的通知栏适配吧

private void showNotification(Context context, String content) {
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder;
        //判断是否是8.0Android.
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel chan1 = new NotificationChannel(“通知渠道的id", "通知渠道的名称", NotificationManager.IMPORTANCE_HIGH);
            chan1.setLightColor(Color.GREEN);
            mNotificationManager.createNotificationChannel(chan1);
            mBuilder = new NotificationCompat.Builder(context, PRIMARY_CHANNEL);
        } else {
            mBuilder = new NotificationCompat.Builder(context);
        }
        Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, R.string.app_name, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        mBuilder.setContentTitle("xxx")//设置通知栏标题
                .setContentText(content)//设置通知栏内容
                .setContentIntent(intent) //设置通知栏点击意图
                .setSmallIcon(R.mipmap.xxx) //设置通知小ICON
                .setAutoCancel(true)
                .setNumber(2) //设置通知集合的数量
                .setTicker("xxx") //通知首次出现在通知栏,带上升动画效果的
                .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
                .setDefaults(Notification.DEFAULT_ALL);//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合
        Notification notify = mBuilder.build();
        mNotificationManager.notify(pushId, notify);
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,883评论 2 59
  • 摘自郭神的文章: 从Android 8.0系统开始,Google引入了通知渠道这个概念。 什么是通知渠道呢?顾名思...
    D___Will阅读 3,343评论 0 1
  • 由于历史原因,Android在发布之初对通知栏Notification的设计相当简单,而如今面对各式各样的通知栏玩...
    Linhaojian阅读 5,480评论 0 43
  • 晚上放学后,天空还是亮的,这让人看到希望不沮丧。小巷的里端深处与天际线相接的地方,晚霞正好,贴近房子的天空是紫色的...
    麦子飞呀飞阅读 164评论 0 0
  • 初识你,在夏末秋初。知了声渐渐退去,蟋蟀开始接班弹琴,《人民日报》夜读中,偶然遇见了你,你究竟长什么样子呀?好想见...
    子皿悠悠阅读 595评论 2 4