1
1
<?php
2
2
defined ('BASEPATH ' ) or exit ('No direct script access allowed ' );
3
3
4
+ /**
5
+ * FCM simple server side implementation in PHP
6
+ *
7
+ * @author Abhishek
8
+ */
4
9
class Fcm
5
10
{
6
11
7
- // push message title
12
+ /** @var string push message title */
8
13
private $ title ;
14
+
15
+ /** @var string message */
9
16
private $ message ;
17
+
18
+ /** @var string URL String */
10
19
private $ image ;
11
- // push message payload
20
+
21
+ /** @var array Custom payload */
12
22
private $ data ;
13
- // flag indicating whether to show the push
14
- // notification or not
15
- // this flag will be useful when perform some opertation
16
- // in background when push is recevied
17
- private $ is_background ;
18
23
19
- public function __construct ()
20
- {
24
+ /**
25
+ * flag indicating whether to show the push notification or not
26
+ * this flag will be useful when perform some opertation
27
+ * in background when push is recevied
28
+ */
21
29
22
- }
30
+ /** @var bool set background or not */
31
+ private $ is_background ;
23
32
33
+ /**
34
+ * Function to set the title
35
+ *
36
+ * @param string $title The title of the push message
37
+ */
24
38
public function setTitle ($ title )
25
39
{
26
40
$ this ->title = $ title ;
27
41
}
28
42
43
+ /**
44
+ * Function to set the message
45
+ *
46
+ * @param string $message Message
47
+ */
29
48
public function setMessage ($ message )
30
49
{
31
50
$ this ->message = $ message ;
32
51
}
33
52
53
+ /**
54
+ * Function to set the image (optional)
55
+ *
56
+ * @param string $imageUrl URI string of image
57
+ */
34
58
public function setImage ($ imageUrl )
35
59
{
36
60
$ this ->image = $ imageUrl ;
37
61
}
38
62
63
+ /**
64
+ * Function to set the custom payload (optional)
65
+ *
66
+ * eg:
67
+ * $payload = array('user' => 'user1');
68
+ *
69
+ * @param array $data Custom data array
70
+ */
39
71
public function setPayload ($ data )
40
72
{
41
73
$ this ->data = $ data ;
42
74
}
43
75
76
+ /**
77
+ * Function to specify if is set background (optional)
78
+ *
79
+ * @param bool $is_background
80
+ */
44
81
public function setIsBackground ($ is_background )
45
82
{
46
83
$ this ->is_background = $ is_background ;
47
84
}
48
85
49
- /*
50
- Generating the push message array
51
- */
86
+ /**
87
+ * Generating the push message array
88
+ *
89
+ * @return array array of the push notification data to be send
90
+ */
52
91
public function getPush ()
53
92
{
54
93
$ res = array ();
@@ -61,7 +100,14 @@ public function getPush()
61
100
return $ res ;
62
101
}
63
102
64
- // sending push message to single user by firebase reg id
103
+ /**
104
+ * Function to send notification to a single device
105
+ *
106
+ * @param string $to registration id of device (device token)
107
+ * @param array $message push notification array returned from getPush()
108
+ *
109
+ * @return array array of notification data and to address
110
+ */
65
111
public function send ($ to , $ message )
66
112
{
67
113
$ fields = array (
@@ -71,7 +117,14 @@ public function send($to, $message)
71
117
return $ this ->sendPushNotification ($ fields );
72
118
}
73
119
74
- // Sending message to a topic by topic name
120
+ /**
121
+ * Function to send notification to a topic by topic name
122
+ *
123
+ * @param string $to topic
124
+ * @param array $message push notification array returned from getPush()
125
+ *
126
+ * @return array array of notification data and to address (topic)
127
+ */
75
128
public function sendToTopic ($ to , $ message )
76
129
{
77
130
$ fields = array (
@@ -81,7 +134,14 @@ public function sendToTopic($to, $message)
81
134
return $ this ->sendPushNotification ($ fields );
82
135
}
83
136
84
- // sending push message to multiple users by firebase registration ids
137
+ /**
138
+ * Function to send notification to multiple users by firebase registration ids
139
+ *
140
+ * @param array $to array of registration ids of devices (device tokens)
141
+ * @param array $message push notification array returned from getPush()
142
+ *
143
+ * @return array array of notification data and to addresses
144
+ */
85
145
public function sendMultiple ($ registration_ids , $ message )
86
146
{
87
147
$ fields = array (
@@ -92,11 +152,17 @@ public function sendMultiple($registration_ids, $message)
92
152
return $ this ->sendPushNotification ($ fields );
93
153
}
94
154
95
- // function makes curl request to firebase servers
155
+ /**
156
+ * Function makes curl request to firebase servers
157
+ *
158
+ * @param array $fields array of registration ids of devices (device tokens)
159
+ *
160
+ * @return string returns result from FCM server as json
161
+ */
96
162
private function sendPushNotification ($ fields )
97
163
{
98
164
99
- $ CI =& get_instance ();
165
+ $ CI = & get_instance ();
100
166
$ CI ->load ->config ('androidfcm ' ); //loading of config file
101
167
102
168
// Set POST variables
0 commit comments