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
|
|
|
* GCM message sending example |
18
|
|
|
* @author alxmsl |
19
|
|
|
* @date 5/28/13 |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
include '../vendor/autoload.php'; |
23
|
|
|
|
24
|
|
|
use alxmsl\Google\GCM\Client; |
25
|
|
|
use alxmsl\Google\GCM\Message\PayloadData; |
26
|
|
|
use alxmsl\Google\GCM\Message\PayloadMessage; |
27
|
|
|
|
28
|
|
|
// Create new payload class |
29
|
|
|
final class NewPayloadData extends PayloadData { |
|
|
|
|
30
|
|
|
protected function getDataFields() { |
31
|
|
|
return array( |
32
|
|
|
'test' => 'test_01', |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// Create payload instance |
38
|
|
|
$Data = new NewPayloadData(); |
39
|
|
|
|
40
|
|
|
// Create and initialize message instance |
41
|
|
|
$Message = new PayloadMessage(); |
42
|
|
|
$Message->setRegistrationIds('DeV1CeT0kEN') |
43
|
|
|
->setType(PayloadMessage::TYPE_JSON) |
44
|
|
|
->setData($Data); |
45
|
|
|
|
46
|
|
|
// Create GCM client |
47
|
|
|
$Client = new Client(); |
48
|
|
|
$Client->getRequest()->setConnectTimeout(60); |
49
|
|
|
$Client->setAuthorizationKey('aUTH0R1Z4t1oNKEy'); |
50
|
|
|
|
51
|
|
|
// ...and send the message |
52
|
|
|
$Response = $Client->send($Message); |
53
|
|
|
var_dump($Response); |
|
|
|
|
54
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.