|
| 1 | +<?php |
| 2 | +// 출처: https://blog.naver.com/whdals0/222683916484 |
| 3 | +/* |
| 4 | +curl -sS https://getcomposer.org/installer | php |
| 5 | +php composer.phar require google/auth |
| 6 | +php composer.phar require google/apiclient:^2.12.1 |
| 7 | +*/ |
| 8 | + |
| 9 | +$url = 'https://fcm.googleapis.com/v1/projects/fcmsample-b5d93/messages:send'; |
| 10 | +require_once ('./vendor/autoload.php'); |
| 11 | +putenv('GOOGLE_APPLICATION_CREDENTIALS=./fcm_auth.json'); |
| 12 | +$scope = 'https://www.googleapis.com/auth/firebase.messaging'; |
| 13 | +$client = new Google_Client(); |
| 14 | +$client->useApplicationDefaultCredentials(); |
| 15 | +$client->setScopes($scope); |
| 16 | +$auth_key = $client->fetchAccessTokenWithAssertion(); |
| 17 | +echo $auth_key['access_token']; |
| 18 | + |
| 19 | +$ch = curl_init(); |
| 20 | +$headers = array |
| 21 | +( |
| 22 | + 'Authorization: Bearer ' . $auth_key['access_token'], |
| 23 | + 'Content-Type: application/json' |
| 24 | +); |
| 25 | + |
| 26 | +curl_setopt($ch, CURLOPT_HEADER, true); |
| 27 | +curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 28 | +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 29 | +curl_setopt($ch, CURLOPT_URL, $url); |
| 30 | +curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
| 31 | + |
| 32 | +$title = "제목입니다."; |
| 33 | +$message = "내용입니다."; |
| 34 | + |
| 35 | +$notification_opt = array ( |
| 36 | + 'title' => $title, |
| 37 | + 'body' => $message, |
| 38 | + 'image' => 'http://sowonbyul.com/original/totalAdmin/images/Icon-512.png' |
| 39 | +); |
| 40 | + |
| 41 | +$datas = array ( |
| 42 | + 'test1' => '테스트 데이터1', |
| 43 | + 'test2' => '테스트 데이터2', |
| 44 | + 'test3' => '테스트 데이터3' |
| 45 | +); |
| 46 | + |
| 47 | +$android_opt = array ( |
| 48 | + 'notification' => array( |
| 49 | + 'default_sound' => true |
| 50 | + ) |
| 51 | +); |
| 52 | + |
| 53 | +$message = array |
| 54 | +( |
| 55 | + 'token' => 'cFDXq8L7SMaU-mKlh3Yeev:APA91bGjmdOrtwcB1OR1I1hP0o3LXPChp6AiXKPQ7N9X5PQfNV5Uc7FS0m4y7N7Zm6Sq18JilurTlVQjN33PgmkqCRzlDaGxd18kNJrAKEsFPZzl7C_oqIG8ycfcYi0jQ8aOhx9kFIS-', |
| 56 | + 'notification' => $notification_opt, |
| 57 | + 'android' => $android_opt |
| 58 | +); |
| 59 | + |
| 60 | +$last_msg = array ( |
| 61 | + "message" => $message |
| 62 | +); |
| 63 | + |
| 64 | +curl_setopt($ch, CURLOPT_POST, 1); |
| 65 | +curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($last_msg)); |
| 66 | +$result = curl_exec($ch); |
| 67 | + |
| 68 | +if($result === FALSE){ |
| 69 | + // die('FCM Send Error: ' . curl_error($ch)); |
| 70 | + printf("cUrl error (#%d): %s<br>\n", |
| 71 | + curl_errno($ch), |
| 72 | + htmlspecialchars(curl_error($ch))); |
| 73 | +} |
| 74 | + |
| 75 | +echo $result; |
| 76 | + |
| 77 | +/* |
| 78 | +ubuntu@fcm:~/test$ php send_fcm.php |
| 79 | +ya29.c.b0Aaekm1IL7-3qkbvuCkziVcMsBeWFICLEaXJrX5qk_up9xwjlpxYv1_YcIvvBst-zHTTP/2 200 |
| 80 | +content-type: application/json; charset=UTF-8 |
| 81 | +vary: X-Origin |
| 82 | +vary: Referer |
| 83 | +vary: Origin,Accept-Encoding |
| 84 | +date: Fri, 22 Sep 2023 16:06:37 GMT |
| 85 | +server: scaffolding on HTTPServer2 |
| 86 | +cache-control: private |
| 87 | +x-xss-protection: 0 |
| 88 | +x-frame-options: SAMEORIGIN |
| 89 | +x-content-type-options: nosniff |
| 90 | +alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 |
| 91 | +accept-ranges: none |
| 92 | +
|
| 93 | +{ |
| 94 | + "name": "projects/test-free/messages/0:1695398797178485%bf5671a5b" |
| 95 | +} |
| 96 | +*/ |
| 97 | +?> |
0 commit comments