diff --git a/addPengumuman.php b/addPengumuman.php index 529d2c5a75e9eded8fdecf3d64695071d50fbd53..a652ed630237c0564f024990190d72e481473bdb 100755 --- a/addPengumuman.php +++ b/addPengumuman.php @@ -94,5 +94,5 @@ $stmt->execute(); $stmt->close(); $conn->close(); -sendNotif("Ada pengumuman baru", "Klik untuk membuka"); +\pmotraining\FirebaseNotif::sendNotif("Ada pengumuman baru", $judul, 1, 1); echo 'Sukses, pengumuman ' . $judul . ' berhasil ditambahkan.'; diff --git a/sendNotif.php b/sendNotif.php index 3f9dc20ba3ae548ea1c1f505c5769491550f7d1d..cb72de3ba862bd1b29599492d1cb3edb26370666 100755 --- a/sendNotif.php +++ b/sendNotif.php @@ -10,7 +10,11 @@ * @link http://pear.php.net/package/komunitas-pmo */ - /** +namespace pmotraining; + +class FirebaseNotif +{ +/** * Mengirimkan notifikasi ke firebase * * @param string $title judul notifikasi @@ -18,31 +22,33 @@ * * @return null */ -function sendNotif($title, $body) -{ - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send"); - curl_setopt($ch, CURLOPT_POST, 1); - $vars = [ - "to" => "/topics/all", - "notification" => [ - "title" => $title, - "body" => $body - ] - ]; - // $cat = $_POST["kategori"]; - // if ($cat == "pengumuman" ) { - $vars["notification"]["click_action"] = "mainActivity"; //testing - // $vars["data"] = array("id" => $_POST["id"]); - // } - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($vars)); //Post Fields - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - $headers = [ - 'Authorization: key=AIzaSyDhcKAiH6wOm8QhHLnYZzrri8SyZZtcoe4', - 'Content-Type: application/json' - ]; - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - $response = curl_exec($ch); - curl_close($ch); - // echo $response; + public static function sendNotif($title, $body, $kode, $id) + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send"); + curl_setopt($ch, CURLOPT_POST, 1); + $vars = [ + "to" => "/topics/all", + "notification" => [ + "title" => $title, + "body" => $body + ] + ]; + // $cat = $_POST["kategori"]; + // if ($cat == "pengumuman" ) { + $vars["notification"]["click_action"] = "mainActivity"; //testing + // $vars["data"] = array("id" => $_POST["id"]); + // } + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($vars)); //Post Fields + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $headers = [ + 'Authorization: key=AIzaSyDhcKAiH6wOm8QhHLnYZzrri8SyZZtcoe4', + 'Content-Type: application/json' + ]; + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + $response = curl_exec($ch); + curl_close($ch); + return true; + } } + diff --git a/tests/unit/FNotifTest.php b/tests/unit/FNotifTest.php new file mode 100644 index 0000000000000000000000000000000000000000..239fc9ab5f881bfcd81e46cc5858f6b0513c75a5 --- /dev/null +++ b/tests/unit/FNotifTest.php @@ -0,0 +1,25 @@ +<?php + +require 'sendNotif.php'; + +class FNotifTest extends \Codeception\Test\Unit +{ + /** + * @var \UnitTester + */ + protected $tester; + + protected function _before() + { + } + + protected function _after() + { + } + + // tests + public function testNotification() + { + $this->assertTrue(\pmotraining\FirebaseNotif::sendNotif("Unit testing","Ini adalah testing",1,1)); + } +} \ No newline at end of file