PHP에서 푸시 알림 호출 방법

안드로이드 개별 알림 전송
PHP 호출 코드
<?php
// 안드로이드 개별 알림 전송
$data = [
    'action' => 'individual',
    'app' => 'android',
    'user_token' => 'FCM_토큰값',
    'title' => '알림 제목',
    'memo' => '알림 내용',
    'url' => 'https://naver.com',
    'file_url' => 'https://cdn.safetimes.co.kr/news/photo/202103/93041_72859_5525.jpg'
];

$ch = curl_init('http://124.111.85.242/push_sesanso/push_api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

$result = json_decode($response, true);
if ($result['status'] === 'success') {
    echo '안드로이드 알림 전송 성공';
} else {
    echo '오류: ' . $result['message'];
}
?>
안드로이드 필드 매핑
설정에서 정의된 필드명:
• 제목: title
• 내용: message
• 이미지: image_url
• URL: site
앱에서 받는 데이터
실제로 안드로이드 앱이 받게 되는 JSON:
{
  "title": "알림 제목",
  "message": "알림 내용",
  "image_url": "이미지URL",
  "site": "이동할URL"
}
안드로이드 PHP 파일 생성
예: android_push_20241218_143022.php
iOS 개별 알림 전송
PHP 호출 코드
<?php
// iOS 개별 알림 전송
$data = [
    'action' => 'individual',
    'app' => 'ios',
    'user_token' => 'FCM_토큰값',
    'title' => '알림 제목',
    'memo' => '알림 내용',
    'url' => 'https://naver.com',
    'file_url' => 'https://cdn.safetimes.co.kr/news/photo/202103/93041_72859_5525.jpg'
];

$ch = curl_init('http://124.111.85.242/push_sesanso/push_api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

$result = json_decode($response, true);
if ($result['status'] === 'success') {
    echo 'iOS 알림 전송 성공';
} else {
    echo '오류: ' . $result['message'];
}
?>
iOS 필드 매핑
설정에서 정의된 필드명:
• 제목: title
• 내용: body
• 이미지: image-url
• URL: goto
앱에서 받는 데이터
실제로 iOS 앱이 받게 되는 JSON:
{
  "title": "알림 제목",
  "body": "알림 내용",
  "image-url": "이미지URL",
  "goto": "이동할URL"
}
iOS 주의사항
• APNs 인증서가 Firebase에 업로드되어야 함
• iOS 앱에서 푸시 알림 권한 허용 필요
• 필드명이 안드로이드와 다를 수 있음
iOS PHP 파일 생성
예: ios_push_20241218_143022.php
전체 알림 전송 (토픽)
PHP 호출 코드
<?php
// 전체 알림 전송 (모든 기기)
$data = [
    'action' => 'topic',
    'device_type' => 'all',     // 'all', 'android', 'ios'
    'title' => '전체 공지사항',
    'memo' => '모든 사용자에게 보내는 중요한 공지사항입니다.',
    'url' => 'https://naver.com',
    'file_url' => 'https://cdn.safetimes.co.kr/news/photo/202103/93041_72859_5525.jpg'
];

$ch = curl_init('http://124.111.85.242/push_sesanso/push_api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

$result = json_decode($response, true);
if ($result['status'] === 'success') {
    echo '전체 알림 전송 성공';
} else {
    echo '오류: ' . $result['message'];
}
?>
기기별 전체 알림
안드로이드만
$data = [
    'action' => 'topic',
    'device_type' => 'android',
    'title' => '안드로이드 전용 알림',
    'memo' => '안드로이드 사용자만 받는 알림'
];
iOS만
$data = [
    'action' => 'topic',
    'device_type' => 'ios',
    'title' => 'iOS 전용 알림',
    'memo' => 'iOS 사용자만 받는 알림'
];
전체 알림 주의사항
• 모든 앱 사용자에게 알림이 전송됩니다
• 토픽 구독이 필요합니다
• 신중하게 사용하세요
device_type 옵션
all: 모든 기기
android: 안드로이드만
ios: iOS만
토픽 설정
현재 설정된 토픽:
• 안드로이드: android_seosanso2
• iOS: ios_seosanso2
매개변수 상세 설명
매개변수 타입 필수여부 설명 예시값
action string 필수 알림 전송 방식 'individual' (개별) 또는 'topic' (전체)
app string 개별 시 필수 개별 알림 시 기기 타입 'android' 또는 'ios'
user_token string 개별 시 필수 개별 알림 시 사용자의 FCM 토큰 'dA1B2c3D4e5F6g7H8i9J...'
device_type string 전체 시 필수 전체 알림 시 대상 기기 'all', 'android', 'ios'
title string 필수 알림 제목 '새로운 공지사항'
memo string 필수 알림 내용 '중요한 업데이트가 있습니다.'
url string 선택 알림 클릭 시 이동할 URL 'https://naver.com'
file_url string 선택 알림에 표시할 이미지 URL 'https://cdn.safetimes.co.kr/...'
성공 응답
{
  "status": "success",
  "message": "푸시 알림 전송 성공"
}
실패 응답
{
  "status": "error",
  "message": "오류 메시지 내용"
}
개발 팁
  • cURL 대신 Guzzle HTTP, file_get_contents() 등 다른 HTTP 클라이언트 사용 가능
  • 테스트 시에는 개별 알림으로 먼저 확인 후 전체 알림 사용 권장
  • FCM 토큰은 앱에서 가져와서 서버에 저장 후 사용
  • 이미지 URL은 HTTPS 사용 권장 (HTTP는 일부 기기에서 차단될 수 있음)
  • 설정 페이지에서 필드 매핑을 앱 개발에 맞게 조정 가능