NotificationCompat.Builder过时和无法显示通知的问题

1,569 阅读1分钟
 @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_notice:
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                //高版本需要渠道
                if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    //只在Android O之上需要渠道,这里的第一个参数要和下面的channelId一样
                    NotificationChannel notificationChannel = new NotificationChannel("1", "name", NotificationManager.IMPORTANCE_HIGH);
                    //如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道,通知才能正常弹出
                    manager.createNotificationChannel(notificationChannel);
                }
                //这里的第二个参数要和上面的第一个参数一样
                Notification notification = new NotificationCompat.Builder(this, "1")
                        .setContentTitle("这是一个内容标题")
                        .setContentText("这是一个内容文本")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                        .build();
                manager.notify(1, notification);
                break;
            default:
                break;
        }

参考:blog.csdn.net/hbfuas/arti…