天天看点

Notification通知不能被正常显示的问题android中通知不能显示的问题

android中通知不能显示的问题

由于版本更新后,在高版本中,需要一个channel属性。所以高版本的手机按照之前的设置方法将不会显示通知。需要为NotificationManager设置一个channel

低版本

//高版本需要渠道
        NotificationManager notifyManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        //创建一个通知
        Notification notification=new NotificationCompat.Builder(this)
                .setContentTitle("标题")
                .setContentText("通知内容")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .build();
        //创建你一个通知管理器

        notifyManager.notify(1,notification);
           

高版本

NotificationManager notifyManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
            //这里的第一个参数要和下面的channelId一样
            NotificationChannel notificationChannel =
                    new NotificationChannel("1","name",NotificationManager.IMPORTANCE_HIGH);
                    notifyManager.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();
        //创建你一个通知管理器
        notifyManager.notify(1,notification);
           

继续阅读