PHP에서 GCM 멀티캐스트로 발송하는 방법은 정말 간단하게 구현할 수 있습니다.
GCM에서는 한번 발송에 1000개의 디바이스 토큰을 일괄 발송할 수 있는 멀티캐스트 기능이 포함되어 있어, 무척 편리해 졌는데요
100만건을 발송하더라도, 1000회만 커넥션을 맺으면 되므로 발송 속도가 무척이나 빠르겠죠..
우선, PHP에서 GCM발송 라이브러리를 작성합니다.
아래 라이브러리는 Github에서 가져왔습니다.
GCMPushMessage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
| <?php/* Class to send push notifications using Google Cloud Messaging for Android Example usage ----------------------- $an = new GCMPushMessage($apiKey); $an->setDevices($devices); $response = $an->send($message); ----------------------- $apiKey Your GCM api key $devices An array or string of registered device tokens $message The mesasge you want to push out @author Matt Grundy Adapted from the code available at:*/class GCMPushMessage { var $serverApiKey = ""; var $devices = array(); function GCMPushMessage($apiKeyIn){ $this->serverApiKey = $apiKeyIn; } function setDevices($deviceIds){ if(is_array($deviceIds)){ $this->devices = $deviceIds; } else { $this->devices = array($deviceIds); } } function send($title, $message, $link){ if(!is_array($this->devices) || count($this->devices) == 0){ $this->error("No devices set"); } if(strlen($this->serverApiKey) < 8){ $this->error("Server API Key not set"); } $fields = array( 'registration_ids' => $this->devices, 'data' => array( 'title' => $title, 'message' => $message, 'link' => $link ), ); //echo json_encode($fields); //exit; $headers = array( 'Authorization: key=' . $this->serverApiKey, 'Content-Type: application/json' ); // Open connection $ch = curl_init(); // Set the url, number of POST vars, POST data curl_setopt( $ch, CURLOPT_URL, $this->url ); curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) ); // Execute post $result = curl_exec($ch); // Close connection curl_close($ch); return $result; } function error($msg){ echo "Android send notification failed with error:"; echo "\t" . $msg; exit(1); }}?> |
발송은 send 페이지에서 다음과 같이 작성합니다.
$devices는 Array변수로 디바이스 토큰값을 담아주면 됩니다. (최대 1000개)
1
2
3
| $gcm = new GCMPushMessage(API_KEY);$gcm->setDevices($devices);$response = $gcm->send($title, $message, $link); |
만약, 등록건수가 1만건이다... 어떻게 1000으로 나눌수가 있을까요??
PHP에서는 아주 간단한 Array함수를 가지고 있더군요..
1만건을 $devices에 담아 array_chunk 함수로 나누면 되는거죠... 1000건씩...
array_chunk 함수는 아주 간편하게...나눠주니.. 계산할 필요없이 아주 편리하게 사용하면 됩니당@!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| define("MULTICAST_COUNT" , 1000);//Array를 MULTICAST_COUNT값으로 나누자.$divide_array = array_chunk($devices, MULTICAST_COUNT);for($j=0; $j < count($divide_array); $j++) { $gcm = new GCMPushMessage(API_KEY); $gcm->setDevices($divide_array[$j]); $response = $gcm->send($title, $message, $link); echo $j . ". " . $response . "";} |
댓글 없음:
댓글 쓰기