Notification Channel

Lee HyeongJong·2023년 2월 6일
0

안드로이드

목록 보기
33/43

1. Importance

채널 생성에 앞서 Importance에 따른 채널 중요도를 설정할 수 있다.

여기서 중요한 점은 헤드업 알람의 유무

2. 인자

id : 임의의 채널 id
name : 알람 이름
importance level : 알람 중요도

3. 순서

Channel과 NotificationManager 초기화 후 마지막에 .createNotificationChannel을 해야한다.

4. 코드


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel notificationChannel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
    notificationChannel.setDescription("channel description");
    notificationChannel.enableLights(true);
    notificationChannel.setLightColor(Color.GREEN);
    notificationChannel.enableVibration(true);
    notificationChannel.setVibrationPattern(new long[]{100, 200, 100, 200});
    notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    notificationManager.createNotificationChannel(notificationChannel);
}

5. 옵션

무음

	mChannel.setSound(null, null);
	mChannel.setVibrationPattern(new long[]{0});
	mChannel.enableVibration(false);

소리

int notificationId = 0;
final NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mCompatBuilder = new NotificationCompat.Builder(context);
Uri alertSound;

if (com.ucware.util.Config.sharedInstance().enablePushSoundConfig) {
	int res_id = com.ucware.util.Config.sharedInstance().getPushSoundResourceId(context);
	alertSound = Uri.parse("android.resource://" + context.getPackageName() + "/" + res_id);
} else {
	alertSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
            
mCompatBuilder.setSound(alertSound);
nm.notify(notificationId, mCompatBuilder.build());
            
profile
코딩을 시작해보자

0개의 댓글