├── .github └── ISSUE_TEMPLATE │ ├── feature_request.md │ └── bug_report.md ├── config └── androidfcm.php ├── README.md ├── LICENSE ├── example_controller.php ├── CODE_OF_CONDUCT.md └── library └── Fcm.php /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /config/androidfcm.php: -------------------------------------------------------------------------------- 1 | load->library('fcm'); 22 | $this->fcm->setTitle('Test FCM Notification'); 23 | $this->fcm->setMessage($message); 24 | $json = $this->fcm->getPush(); 25 | $p = $this->fcm->send($token, $json); 26 | } 27 | ```` 28 | Examples are in the [example_controller.php](https://github.com/owizeo/Codeigniter-FCM-Library/blob/master/example_controller.php) file. 29 | 30 | ## License 31 | [MIT](https://github.com/owizeo/Codeigniter-FCM-Library/blob/master/LICENSE) 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 owizeo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /example_controller.php: -------------------------------------------------------------------------------- 1 | load->library('fcm'); 15 | $this->fcm->setTitle('Test FCM Notification'); 16 | $this->fcm->setMessage($message); 17 | 18 | /** 19 | * set to true if the notificaton is used to invoke a function 20 | * in the background 21 | */ 22 | $this->fcm->setIsBackground(false); 23 | 24 | /** 25 | * payload is userd to send additional data in the notification 26 | * This is purticularly useful for invoking functions in background 27 | * ----------------------------------------------------------------- 28 | * set payload as null if no custom data is passing in the notification 29 | */ 30 | $payload = array('notification' => ''); 31 | $this->fcm->setPayload($payload); 32 | 33 | /** 34 | * Send images in the notification 35 | */ 36 | $this->fcm->setImage('https://firebase.google.com/_static/9f55fd91be/images/firebase/lockup.png'); 37 | 38 | /** 39 | * Get the compiled notification data as an array 40 | */ 41 | $json = $this->fcm->getPush(); 42 | 43 | $p = $this->fcm->send($token, $json); 44 | 45 | print_r($p); 46 | } 47 | 48 | /** 49 | * Send to multiple devices 50 | */ 51 | public function sendToMultiple() 52 | { 53 | $token = array('Registratin_id1', 'Registratin_id2'); // array of push tokens 54 | $message = "Test notification message"; 55 | 56 | $this->load->library('fcm'); 57 | $this->fcm->setTitle('Test FCM Notification'); 58 | $this->fcm->setMessage($message); 59 | $this->fcm->setIsBackground(false); 60 | // set payload as null 61 | $payload = array('notification' => ''); 62 | $this->fcm->setPayload($payload); 63 | $this->fcm->setImage('https://firebase.google.com/_static/9f55fd91be/images/firebase/lockup.png'); 64 | $json = $this->fcm->getPush(); 65 | 66 | /** 67 | * Send to multiple 68 | * 69 | * @param array $token array of firebase registration ids (push tokens) 70 | * @param array $json return data from getPush() method 71 | */ 72 | $result = $this->fcm->sendMultiple($token, $json); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at pfhostacc@outlook.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /library/Fcm.php: -------------------------------------------------------------------------------- 1 | title = $title; 41 | } 42 | 43 | /** 44 | * Function to set the message 45 | * 46 | * @param string $message Message 47 | */ 48 | public function setMessage($message) 49 | { 50 | $this->message = $message; 51 | } 52 | 53 | /** 54 | * Function to set the image (optional) 55 | * 56 | * @param string $imageUrl URI string of image 57 | */ 58 | public function setImage($imageUrl) 59 | { 60 | $this->image = $imageUrl; 61 | } 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 | */ 71 | public function setPayload($data) 72 | { 73 | $this->data = $data; 74 | } 75 | 76 | /** 77 | * Function to specify if is set background (optional) 78 | * 79 | * @param bool $is_background 80 | */ 81 | public function setIsBackground($is_background) 82 | { 83 | $this->is_background = $is_background; 84 | } 85 | 86 | /** 87 | * Generating the push message array 88 | * 89 | * @return array array of the push notification data to be send 90 | */ 91 | public function getPush() 92 | { 93 | $res = array(); 94 | $res['data']['title'] = $this->title; 95 | $res['data']['is_background'] = $this->is_background; 96 | $res['data']['message'] = $this->message; 97 | $res['data']['image'] = $this->image; 98 | $res['data']['payload'] = $this->data; 99 | $res['data']['timestamp'] = date('Y-m-d G:i:s'); 100 | return $res; 101 | } 102 | 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 | */ 111 | public function send($to, $message) 112 | { 113 | $fields = array( 114 | 'to' => $to, 115 | 'data' => $message, 116 | ); 117 | return $this->sendPushNotification($fields); 118 | } 119 | 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 | */ 128 | public function sendToTopic($to, $message) 129 | { 130 | $fields = array( 131 | 'to' => '/topics/' . $to, 132 | 'data' => $message, 133 | ); 134 | return $this->sendPushNotification($fields); 135 | } 136 | 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 | */ 145 | public function sendMultiple($registration_ids, $message) 146 | { 147 | $fields = array( 148 | 'registration_ids' => $registration_ids, 149 | 'data' => $message, 150 | ); 151 | 152 | return $this->sendPushNotification($fields); 153 | } 154 | 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 | */ 162 | private function sendPushNotification($fields) 163 | { 164 | 165 | $CI = &get_instance(); 166 | $CI->load->config('androidfcm'); //loading of config file 167 | 168 | // Set POST variables 169 | $url = $CI->config->item('fcm_url'); 170 | 171 | $headers = array( 172 | 'Authorization: key=' . $CI->config->item('key'), 173 | 'Content-Type: application/json', 174 | ); 175 | // Open connection 176 | $ch = curl_init(); 177 | 178 | // Set the url, number of POST vars, POST data 179 | curl_setopt($ch, CURLOPT_URL, $url); 180 | 181 | curl_setopt($ch, CURLOPT_POST, true); 182 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 183 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 184 | 185 | // Disabling SSL Certificate support temporarly 186 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 187 | 188 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 189 | 190 | // Execute post 191 | $result = curl_exec($ch); 192 | if ($result === false) { 193 | die('Curl failed: ' . curl_error($ch)); 194 | } 195 | 196 | // Close connection 197 | curl_close($ch); 198 | 199 | return $result; 200 | } 201 | 202 | } 203 | --------------------------------------------------------------------------------