Android

잠금화면에 알림내용(NotificationCompat) 노출하는 방법 (to show content in lock screen

안드로이드 폰에 탑재된 센서를 사용할 경우,  계속적인 추적이 필요한 경우가 있다. 백그라운드 서비스에서 실행할 경우 상단바 노티영역에 노출해주어야한다. ( 안드로이드 8 부터) 잠금화면을 해제해야하면 컨텐츠를 볼 수 있어 불편함이 많은 경우가 있다.  개인정보가 아니라면 굳이 감출 필요가 없다는 노출해주는 것도 좋다.

잠금화면에 알림내용(NotificationCompat) 보여주기

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

builder.setContentTitle(“Title”)
.setContentText(“content”)
.setSmallIcon(R.mipmap.ic_launcher)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); // 잠금화면에 컨텐츠 노출하기


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int NOTIFICATION_ID = (int) (System.currentTimeMillis()%10000);
String CHANNEL_ID = “channel_status_bar”;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
context.getResources().getString(R.string.cont_44),
NotificationManager.IMPORTANCE_NONE); //.IMPORTANCE_HIGH);//IMPORTANCE_LOW); //IMPORTANCE_NONE

((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

Notification notification = new androidx.core.app.NotificationCompat.Builder(this, CHANNEL_ID)
//.setContentTitle(“”)
.setAutoCancel(false)
//.setWhen(System.currentTimeMillis())
.setShowWhen(false)
//.setLargeIcon(null)
.setSmallIcon(R.drawable.baseline_pets_white_18)
.setContentIntent(pendingIntent)
.setContent(remoteViews)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)  
.build();

notification.flags |= Notification.FLAG_NO_CLEAR; // 지우기 버튼 눌렀을때 지워지지 않게

startForeground(NOTIFICATION_ID, notification);
}else{
Notification notification = new Notification.Builder(context)
.setAutoCancel(false)
//.setTicker(context.getResources().getString(R.string.info_contents_txt_5))
// .setContentTitle(“My notification”)
// .setContentText(“Look, white in Lollipop, else color!”)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.baseline_pets_white_18)
.setContentIntent(pendingIntent)
//.setPriority(Notification.PRIORITY_MAX) 상단위치값인듯,, 제일 앞으로 오게하기
.setContent(remoteViews)
// .addAction(R.mipmap.my_192,”show”,pendingIntent)
// .addAction(R.mipmap.my_192,”remove”,pendingIntent)

// .addAction(isLiked ? R.drawable.ic_like_filled_purple_26dp : R.drawable.ic_like_outline_purple_26dp, getString(isLiked?R.string.like:R.string.unlike), getPendingIntent(PlayerActions.ACTION_LIKE))
// .addAction(R.drawable.ic_previous_purple_18dp, getString(R.string.rewind), getPendingIntent(PlayerActions.ACTION_PREV))
// .addAction(playPauseRes, playPauseString, getPendingIntent(PlayerActions.ACTION_PLAY_PAUSE))
// .addAction(R.drawable.ic_next_purple_18dp, getString(R.string.next), getPendingIntent(PlayerActions.ACTION_NEXT))
// .addAction(R.drawable.ic_radio_filled_26dp, getString(R.string.radio), getPendingIntent(PlayerActions.ACTION_CURRENT_SONG_RADIO));
.build();
notification.flags |= Notification.FLAG_NO_CLEAR; // 지우기 버튼 눌렀을때 지워지지 않게
myNotificationManager.notify(NOTIFICATION_ID, notification);

}

NotificationCompat클래스에 선언된 부분을 가져왔다.

/**
* Notification visibility: Show this notification in its entirety on all lockscreens.
*
* {@see android.app.Notification#visibility}
*/
public static final int VISIBILITY_PUBLIC = Notification.VISIBILITY_PUBLIC;

/**
* Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
* private information on secure lockscreens.
*
* {@see android.app.Notification#visibility}
*/
public static final int VISIBILITY_PRIVATE = Notification.VISIBILITY_PRIVATE;

/**
* Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
*
* {@see android.app.Notification#visibility}
*/
public static final int VISIBILITY_SECRET = Notification.VISIBILITY_SECRET;

Leave a Reply

error: Content is protected !!