zoukankan      html  css  js  c++  java
  • Android 8.0+ 通知不显示的适配

    最近在 写项目的时候  发现 通知并不会显示的问题,查看资料发现 从Android 8.0开始通知必须加上ChannelId 

    Android O 引入了 通知渠道(Notification Channels),以提供统一的系统来帮助用户管理通知,如果是针对 android O 为目标平台时,必须实现一个或者多个通知渠道,以向用户显示通知。比如聊天软件,为每个聊天组设置一个通知渠道,指定特定声音、灯光等配置
    String id = "my_id";
    String name="my_name";
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notification = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
        Toast.makeText(this, mChannel.toString(), Toast.LENGTH_SHORT).show();
        Log.i(TAG, mChannel.toString());
        notificationManager.createNotificationChannel(mChannel);
        notification = new Notification.Builder(this)
                .setChannelId(id)
                .setContentTitle("5 new messages")
                .setContentText("hahaha")
                .setSmallIcon(R.mipmap.ic_launcher).build();
    } else {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("5 new messages")
                .setContentText("hahaha")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setOngoing(true)
                .setChannel(id);//无效
        notification = notificationBuilder.build();
    }
    notificationManager.notify(111123, notification);
  • 相关阅读:
    CF G. Running Competition (NTT, 思维)
    ABC 177 F
    牛客练习赛68 D.牛牛的粉丝 (期望DP,矩阵快速幂)
    CF E
    HDU 6761 Minimum Index (字符串--Lyndon分解)
    D. GameGame (思维、博弈)
    P2533 最小圆覆盖
    P4049 [JSOI2007]合金
    P2510 [HAOI2008]下落的圆盘
    P3205 [HNOI2010]合唱队
  • 原文地址:https://www.cnblogs.com/bimingcong/p/10344400.html
Copyright © 2011-2022 走看看