NotificationCompat 助力显示锁屏通知 和设置通知为最重要

  private Notification buildNotification() {
        String channelId;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channelId = "GaoDeLocationService";
            String channelName = "GaoDeLocationService";
            createNotificationChannel(channelId, channelName);
        } else {
            channelId = "";
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
        NotificationCompat.Builder builder =
                notificationBuilder.setOngoing(true)
                        .setSmallIcon(R.drawable.icon_vas_logo)
                        .setVibrate(null)
                        .setSound(null)
                        .setContentTitle("大虾师傅")
                        .setContentText("正在后台运行")
                        .setVisibility(NotificationCompat.VISIBILITY_SECRET)
                        .setPriority(NotificationCompat.PRIORITY_MAX);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder.setCategory(Notification.CATEGORY_SERVICE);
        }
        return builder.build();
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName) {
        NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (service == null || service.getNotificationChannel(channelId) != null) {
            return;
        }
        NotificationChannel chan = new NotificationChannel(channelId,
                channelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.BLUE);
        chan.enableVibration(false);
        chan.setVibrationPattern(new long[]{0});
        chan.setSound(null, null);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        service.createNotificationChannel(chan);
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容