-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirebase_messaging.php
118 lines (106 loc) · 3.5 KB
/
Firebase_messaging.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
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
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* CodeIgniter Instagram Class
*
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author Achmad Rozikin
*/
Class Firebase_messaging{
public $key = '';
private $request_url = 'https://fcm.googleapis.com/fcm/send';
private $destination = null;
private $count_destionation = 0;
/**
* Property for message content
*
*/
private $title = 'php-simple-firebase-messaging';
private $text = 'php-simple-firebase-messaging';
private $sound = 'default';
private $priority = 'high';
private $data = [];
private $request_body = null;
private $ci;
private $debug_mode = true;
public function __construct(){
$this->ci =& get_instance();
$this->ci->load->library('curl');
}
public function addDestination($inp_destination){
if(is_array($inp_destination)){
foreach($inp_destion as $each_destination){
$this->destination[] = $each_destination;
$this->count_destionation++;
}
}else{
$this->destination[] = $inp_destination;
$this->count_destionation++;
}
}
public function setData($name, $value = 0){
if(is_array($name)){
foreach($name as $dataName => $dataValue):
$this->data[$dataName] = $dataValue;
endforeach;
}else{
$this->data[$name] = $value;
}
}
public function getData(){
return $this->data;
}
public function setText($inp_text){
$this->text = $inp_text;
}
public function getText(){
return $this->text;
}
public function setTitle($inp_tite){
$this->title = $inp_tite;
}
public function getTitle(){
return $this->title;
}
public function setSound($inp_sound){
$this->sound = $inp_sound;
}
public function getSound(){
return $this->sound;
}
public function sendMessage(){
$this->ci->curl->setUrl($this->request_url);
$this->ci->curl->setHeaderData('Authorization', 'key='.$this->key);
$this->ci->curl->setHeaderData('Content-Type', 'application/json');
$this->ci->curl->setRequestMethod('POST');
$message_respon = [];
$i = 0;
foreach($this->destination as $destination){
$this->ci->curl->setBody(json_encode($this->buildRequestBody($destination)));
$respon = $this->ci->curl->getResponse();
if($this->debug_mode){
@$respon_data = json_decode($respon);
if(json_last_error() != JSON_ERROR_NONE){
$respon_data['firebase_messaging_error'] = $respon;
}
$message_respon[$i]['destination'] = $destination;
$message_respon[$i]['response'] = $respon_data;
$i++;
}
}
return $message_respon;
}
private function buildRequestBody($destination){
$request_body = [];
$request_body['to'] = $destination;
$request_body['notification']['title'] = $this->getTitle();
$request_body['notification']['text'] = $this->getText();
$request_body['notification']['sound'] = $this->getSound();
if(!empty($this->getData())){
$request_body['data'] = $this->getData();
}
return $request_body;
}
}