-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_worker.php
executable file
·66 lines (50 loc) · 2.13 KB
/
push_worker.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
<?php
// Put your device token here (without spaces):
$deviceToken = 'your device token';
// $deviceToken = $argv[1];
// Put your private key's passphrase here:
$passphrase = 'your passphrase';
// Put your alert message here:
$message = 'My first push notification!';
////////////////////////////////////////////////////////////////////////////////w
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
//app测试push地址
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
//app正式push地址,上线后修改成下面的地址
// $fp = stream_socket_client(
// 'ssl://gateway.push.apple.com:2195', $err,
// $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp){
exit("Failed to connect: $err $errstr" . PHP_EOL);
}
echo 'Connected to APNS' . PHP_EOL;
$message = array(
"body" => "恭喜您中了一千万",
"refresh" => "PLAY",
);
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'll',
'badge' => 3
);
$body['acm'] = array("acmvalue");
// Encode the payload as JSON
$payload = json_encode($body);
//$payload = json_encode($message);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
?>