File size: 3,511 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
 * Class PushMessageAndroid
 * Class to send push notifications using Google Cloud Messaging for Android
 * Example usage
 *
 * $an = new PushMessageAndroid();
 * $an->setKey($apiKey);
 * $an->setDevices($devices);
 * $response = $an->send($message);
 *
 */

namespace ProcessMaker\BusinessModel\Light;

use ProcessMaker\Core\System;

class PushMessageAndroid
{
    private $url = 'https://android.googleapis.com/gcm/send';
    private $serverApiKey = "AIzaSyBO-VLXGhjf0PPlwmPFTPQEKIBfVDydLAk";
    private $devices = array();
    private $numberDevices = 0;

    /**
     * @param $url string the url server
     */
    public function setUrl($url)
    {
        $this->$url = $url;
    }

    /**
     * @param $apiKeyIn string the server API key
     */
    public function setKey($apiKeyIn)
    {
        $this->serverApiKey = $apiKeyIn;
    }

    /**
     *    Set the devices to send to
     * @param $deviceIds string or array of device tokens to send
     */
    public function setDevices($deviceIds)
    {
        if (is_array($deviceIds)) {
            $this->devices = $deviceIds;
        } else {
            $this->devices = array($deviceIds);
        }
    }

    /**
     * Set the setting value config
     */
    public function setSettingNotification()
    {
        $conf = System::getSystemConfiguration(PATH_CONFIG . 'mobile.ini');
        $this->setUrl($conf['android']['url']);
        $this->setKey($conf['android']['serverApiKey']);
    }

    /**
     * Send the message to the device
     * @param $message string the message to send
     * @param $data array send extra information for app
     * @return mixed
     */
    public function send($message, $data)
    {
        $this->numberDevices = count($this->devices);
        if (!is_array($this->devices) || $this->numberDevices == 0) {
            $this->error("No devices set");
        }
        if (strlen($this->serverApiKey) < 8) {
            $this->error("Server API Key not set");
        }

        if (!is_null($data)) {
            $fields = array(
                'registration_ids' => $this->devices,
                'data' => array(
                    "message" => $message,
                    "data" => $data
                ),
            );
        } else {
            $fields = array(
                'registration_ids' => $this->devices,
                'data' => array("message" => $message),
            );
        }

        $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));

        // Avoids problem with https certificate
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        // Execute post
        $result = curl_exec($ch);

        // Close connection
        curl_close($ch);
        return $result;
    }

    public function getNumberDevices()
    {
        return $this->numberDevices;
    }

    public function error($msg)
    {
        echo "Android send notification failed with error:";
        echo "\t" . $msg;
        exit(1);
    }
}