1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2015 Alexey Maslov <[email protected]> |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
* |
17
|
|
|
* Script for GCM notification sending |
18
|
|
|
* @author alxmsl |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
include __DIR__ . '/../vendor/autoload.php'; |
22
|
|
|
|
23
|
|
|
use alxmsl\Cli\CommandPosix; |
24
|
|
|
use alxmsl\Cli\Exception\RequiredOptionException; |
25
|
|
|
use alxmsl\Cli\Option; |
26
|
|
|
use alxmsl\Google\GCM\Client; |
27
|
|
|
use alxmsl\Google\GCM\Message\CustomPayloadData; |
28
|
|
|
use alxmsl\Google\GCM\Message\PayloadMessage; |
29
|
|
|
|
30
|
|
|
$data = ''; |
31
|
|
|
$id = ''; |
32
|
|
|
$key = ''; |
33
|
|
|
|
34
|
|
|
$Command = new CommandPosix(); |
35
|
|
|
$Command->appendHelpParameter('show help'); |
36
|
|
|
$Command->appendParameter(new Option('data', 'd', 'payload data', Option::TYPE_STRING) |
37
|
|
|
, function($name, $value) use (&$data) { |
38
|
|
|
$data = json_decode($value, true); |
39
|
|
|
}); |
40
|
|
|
$Command->appendParameter(new Option('id', 'i', 'device registration id', Option::TYPE_STRING, true) |
41
|
|
|
, function($name, $value) use (&$id) { |
42
|
|
|
$id = $value; |
43
|
|
|
}); |
44
|
|
|
$Command->appendParameter(new Option('key', 'k', 'authorization key', Option::TYPE_STRING, true) |
45
|
|
|
, function($name, $value) use (&$key) { |
46
|
|
|
$key = $value; |
47
|
|
|
}); |
48
|
|
|
try { |
49
|
|
|
$Command->parse(true); |
50
|
|
|
|
51
|
|
|
// Create payload instance |
52
|
|
|
$Data = new CustomPayloadData($data); |
|
|
|
|
53
|
|
|
|
54
|
|
|
// Create and initialize message instance |
55
|
|
|
$Message = new PayloadMessage(); |
56
|
|
|
$Message->setRegistrationIds($id) |
57
|
|
|
->setType(PayloadMessage::TYPE_JSON) |
58
|
|
|
->setData($Data); |
59
|
|
|
|
60
|
|
|
// Create GCM client |
61
|
|
|
$Client = new Client(); |
62
|
|
|
$Client->getRequest()->setTimeout(5); |
63
|
|
|
$Client->getRequest()->setConnectTimeout(60) |
64
|
|
|
->setSslVersion(6); |
65
|
|
|
$Client->setAuthorizationKey($key); |
66
|
|
|
|
67
|
|
|
// ...and send the message |
68
|
|
|
$Response = $Client->send($Message); |
69
|
|
|
printf(<<<'EOD' |
70
|
|
|
success: %s |
71
|
|
|
failure: %s |
72
|
|
|
|
73
|
|
|
EOD |
74
|
|
|
, $Response->getSuccessCount() |
75
|
|
|
, $Response->getFailureCount()); |
76
|
|
|
} catch (RequiredOptionException $Ex) { |
77
|
|
|
$Command->displayHelp(); |
78
|
|
|
} |
79
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: