설정별 푸시 발송

작성자: admin 작성일: 2025-07-08 수정일: 2025-07-08
Function Guide

send_notification()

사용자별 알림 설정을 확인하여 푸시 알림을 발송하는 고급 함수입니다. 알림 타입에 따라 사용자가 설정한 수신 여부를 자동으로 확인합니다.
function send_notification($notification_type, $member_ids, $title, $message, $options = array())

Parameters

$notification_type

알림의 종류를 지정합니다. 각 타입별로 사용자가 개별 설정할 수 있으며, 시스템 타입은 강제 발송됩니다.
  • 'comment' - 댓글 알림 (게시판, 블로그 댓글)
  • 'message' - 쪽지 알림 (개인 메시지)
  • 'inquiry' - 1:1 문의 답변 알림
  • 'system' - 시스템 알림 (설정 무시하고 강제 발송)

$member_ids

알림을 받을 회원 ID 목록입니다. 문자열 또는 배열로 전달할 수 있으며, 빈 값이나 null 값은 자동으로 제외됩니다.

$title

푸시 알림의 제목입니다. 알림 타입에 맞는 명확한 제목을 작성하는 것이 좋습니다.

$message

푸시 알림의 본문 메시지입니다. 사용자에게 전달할 구체적인 내용을 포함합니다.

$options

추가 옵션 배열입니다. 알림 타입별로 기본값이 자동 설정되며, 사용자 옵션으로 덮어쓸 수 있습니다.
공통 옵션:
  • icon (String) - 알림 아이콘 URL (기본값: 설정된 아이콘)
  • badge (String) - 배지 아이콘 URL (기본값: 설정된 배지)
  • click_action (String) - 클릭 시 이동할 URL
타입별 기본값:
  • message: require_interaction=true, tag='message_시간'
  • comment: require_interaction=false, tag='comment_시간'
  • inquiry: require_interaction=true, tag='inquiry_시간'

Return Value

발송 결과와 상세 정보를 담은 배열을 반환합니다.

성공 시 반환값

array(
  'success' => true,
  'message' => 'Multiple push notifications processed',
  'data' => array(
    'success_count' => 45
  ),
  'details' => array(
    'notification_type' => 'comment',
    'total_members' => 100,
    'valid_members' => array('user1', 'user2'),
    'skipped_members' => array('user3', 'user4'),
    'sent_count' => 45
  )
)

설정으로 인한 실패 시

array(
  'success' => false,
  'message' => '알림 설정으로 인해 발송할 구독자가 없습니다.',
  'details' => array(
    'total_members' => 50,
    'checked_members' => array(),
    'skipped_members' => array('user1', 'user2'),
    'notification_type' => 'comment'
  )
)

사용 예시

댓글 알림 발송

// 쪽지 수신 알림
$sender = 'admin';
$receiver = 'user456';

$options = array(
  'click_action' => '/bbs/memo.php',
  'require_interaction' => true
);

$result = send_notification(
  'message',
  $receiver,
  '새 쪽지가 도착했습니다',
  $sender . '님으로부터 쪽지가 왔습니다.',
  $options
);

1:1 문의 답변 알림

// 관리자가 문의에 답변했을 때
$inquirer = 'customer123';
$inquiry_title = '결제 관련 문의';

$options = array(
  'click_action' => '/bbs/board.php?bo_table=qna&wr_id=456'
);

$result = send_notification(
  'inquiry',
  $inquirer,
  '문의 답변이 등록되었습니다',
  '\'$inquiry_title\'에 대한 답변을 확인해주세요.',
  $options
);

시스템 알림 (강제 발송)

// 긴급 시스템 공지 (사용자 설정 무시)
$all_members = array('user1', 'user2', 'user3');

$options = array(
  'icon' => '/img/emergency.png',
  'require_interaction' => true
);

$result = send_notification(
  'system',
  $all_members,
  '긴급 시스템 점검 안내',
  '오늘 밤 12시부터 시스템 점검이 있습니다.',
  $options
);

// system 타입은 사용자 설정을 무시하고 무조건 발송됩니다

결과 처리 및 로깅

$result = send_notification('comment', $member_ids, $title, $message);

if ($result['success']) {
  $details = $result['details'];
  echo '발송 성공: ' . $details['sent_count'] . '명 발송';
  echo '건너뛴 회원: ' . count($details['skipped_members']) . '명';
} else {
  echo '발송 실패: ' . $result['message'];
  
  // 실패 상세 정보 확인
  if (isset($result['details'])) {
    $details = $result['details'];
    echo '전체 대상: ' . $details['total_members'] . '명';
    echo '알림 비활성화: ' . count($details['skipped_members']) . '명';
  }
}

알림 설정 동작 원리

이 함수는 각 회원의 개별 알림 설정을 확인하여 발송 여부를 결정합니다.
알림 타입 확인 설정 기본값 비활성화 시 동작
comment comment_notification 활성화 해당 회원 건너뜀
message message_notification 활성화 해당 회원 건너뜀
inquiry inquiry_notification 활성화 해당 회원 건너뜀
system 설정 무시 - 무조건 발송

그누보드5 훅 활용

그누보드5의 각종 훅(Hook)과 연동하여 자동 알림을 구현할 수 있습니다.

댓글 등록 시 자동 알림

// write_comment_update.php 파일에 추가
if ($w == '' && !$comment['wr_is_comment']) {
  // 새 댓글이 등록된 경우
  $post_author = $write['mb_id'];
  $commenter = $member['mb_id'];

  // 본인이 단 댓글은 제외
  if ($post_author != $commenter && !empty($post_author)) {
    $options = array(
      'click_action' => G5_BBS_URL . '/board.php?bo_table=' . $bo_table . '&wr_id=' . $wr_id . '#c_' . $comment_id
    );

    send_notification(
      'comment',
      $post_author,
      '새 댓글이 달렸습니다',
      $commenter . '님이 회원님의 글에 댓글을 남겼습니다.',
      $options
    );
  }
}

쪽지 발송 시 자동 알림

// memo_form_update.php 파일에 추가
if ($result) {
  // 쪽지 발송 성공 시
  $options = array(
    'click_action' => G5_BBS_URL . '/memo.php',
    'require_interaction' => true
  );

  send_notification(
    'message',
    $me_recv_mb_id,
    '새 쪽지가 도착했습니다',
    $member['mb_id'] . '님으로부터 쪽지가 왔습니다.',
    $options
  );
}

관리자 답변 시 자동 알림

// 1:1 문의 게시판에서 관리자가 답변할 때
if ($w == '' && $member['mb_level'] >= 10 && $wr_reply) {
  // 답변글이고 관리자가 작성한 경우
  $original_post = sql_fetch("SELECT * FROM {$write_table} WHERE wr_id = '{$wr_parent}'");
  $inquirer = $original_post['mb_id'];

  if (!empty($inquirer) && $inquirer != $member['mb_id']) {
    $options = array(
      'click_action' => G5_BBS_URL . '/board.php?bo_table=' . $bo_table . '&wr_id=' . $wr_parent
    );

    send_notification(
      'inquiry',
      $inquirer,
      '문의 답변이 등록되었습니다',
      '회원님의 문의에 대한 답변이 등록되었습니다.',
      $options
    );
  }
}

주의사항

설정 확인: 각 회원별로 알림 설정을 개별 확인하므로 처리 시간이 소요될 수 있습니다
로그 기록: 건너뛴 회원 정보도 로그에 기록됩니다
system 타입: 긴급 상황에만 사용하며, 사용자 설정을 완전히 무시합니다
빈 결과: 모든 대상자가 알림을 비활성화한 경우 발송이 실패할 수 있습니다
배열 처리: member_ids는 문자열 또는 배열 모두 허용하며, 자동으로 배열로 변환됩니다

관련 함수

// 게시글 작성자에게 댓글 알림
$post_author = 'admin';
$commenter = 'user123';

$options = array(
  'click_action' => '/bbs/board.php?bo_table=free&wr_id=123#comment'
);

$result = send_notification(
  'comment',
  $post_author,
  '새 댓글이 달렸습니다',
  $commenter . '님이 회원님의 글에 댓글을 남겼습니다.',
  $options
);

if ($result['success']) {
  echo '댓글 알림이 발송되었습니다.';
} else {
  echo '사용자가 댓글 알림을 비활성화했습니다.';
}

쪽지 알림 발송

문서 검색