美文网首页
NotificationCompat 助力显示锁屏通知 和设置通

NotificationCompat 助力显示锁屏通知 和设置通

作者: 阡陌昏晨 | 来源:发表于2022-07-21 15:40 被阅读0次
      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);
        }
    

    相关文章

      网友评论

          本文标题:NotificationCompat 助力显示锁屏通知 和设置通

          本文链接:https://www.haomeiwen.com/subject/dqguirtx.html