Guest User

Untitled

a guest
Jan 7th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.93 KB | None | 0 0
  1. <?php
  2.  
  3. class Firebase {
  4.  
  5. // sending push message to single user by firebase reg id
  6. public function send($to, $message) {
  7. $fields = array(
  8. 'to' => $to,
  9. 'data' => $message,
  10. );
  11. return $this->sendPushNotification($fields);
  12. }
  13.  
  14. // Sending message to a topic by topic name
  15. public function sendToTopic($to, $message) {
  16. $fields = array(
  17. 'to' => '/topics/' . $to,
  18. 'data' => $message,
  19. );
  20. return $this->sendPushNotification($fields);
  21. }
  22.  
  23. // sending push message to multiple users by firebase registration ids
  24. public function sendMultiple($registration_ids, $message) {
  25. $fields = array(
  26. 'to' => $registration_ids,
  27. 'data' => $message,
  28. );
  29.  
  30. return $this->sendPushNotification($fields);
  31. }
  32.  
  33. // function makes curl request to firebase servers
  34. private function sendPushNotification($fields) {
  35.  
  36. require_once __DIR__ . '/config.php';
  37.  
  38. // Set POST variables
  39. $url = 'https://fcm.googleapis.com/fcm/send';
  40.  
  41. $headers = array(
  42. 'Authorization: key=' . FIREBASE_API_KEY,
  43. 'Content-Type: application/json'
  44. );
  45. // Open connection
  46. $ch = curl_init();
  47.  
  48. // Set the url, number of POST vars, POST data
  49. curl_setopt($ch, CURLOPT_URL, $url);
  50.  
  51. curl_setopt($ch, CURLOPT_POST, true);
  52. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  53. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  54.  
  55. // Disabling SSL Certificate support temporarly
  56. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  57.  
  58. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
  59.  
  60. // Execute post
  61. $result = curl_exec($ch);
  62. if ($result === FALSE) {
  63. die('Curl failed: ' . curl_error($ch));
  64. }
  65.  
  66. // Close connection
  67. curl_close($ch);
  68.  
  69. return $result;
  70. }
  71. }
  72. ?>
  73.  
  74. <?php
  75.  
  76. class Push {
  77.  
  78. // push message title
  79. private $title;
  80. private $message;
  81. private $image;
  82. // push message payload
  83. private $data;
  84. // flag indicating whether to show the push
  85. // notification or not
  86. // this flag will be useful when perform some opertation
  87. // in background when push is recevied
  88. private $is_background;
  89.  
  90. function __construct() {
  91.  
  92. }
  93.  
  94. public function setTitle($title) {
  95. $this->title = $title;
  96. }
  97.  
  98. public function setMessage($message) {
  99. $this->message = $message;
  100. }
  101.  
  102. public function setImage($imageUrl) {
  103. $this->image = $imageUrl;
  104. }
  105.  
  106. public function setPayload($data) {
  107. $this->data = $data;
  108. }
  109.  
  110. public function setIsBackground($is_background) {
  111. $this->is_background = $is_background;
  112. }
  113.  
  114. public function getPush() {
  115. $res = array();
  116. $res['data']['title'] = $this->title;
  117. $res['data']['is_background'] = $this->is_background;
  118. $res['data']['message'] = $this->message;
  119. $res['data']['image'] = $this->image;
  120. $res['data']['payload'] = $this->data;
  121. $res['data']['timestamp'] = date('Y-m-d G:i:s');
  122. return $res;
  123. }
  124.  
  125. }
  126.  
  127. <?php
  128.  
  129. $host = "localhost";
  130. $db_user = "root";
  131. $db_password = "";
  132. $db_name = "notification";
  133.  
  134. $con = mysqli_connect($host, $db_user, $db_password, $db_name);
  135. ?>
  136.  
  137. <?php
  138.  
  139. if($_SERVER['REQUEST_METHOD']=='POST'){
  140. $full_name = $_POST['full_name'];
  141. $contact_number = $_POST['contact_number'];
  142.  
  143. require_once('dbConnect.php');
  144. $sql = "INSERT INTO notification (full_name,,contact_number) VALUES (
  145. '$full_name'
  146. '$contact_number')";
  147.  
  148.  
  149.  
  150. $check = "SELECT * from notification where full_name='$full_name' AND contact_number='$contact_number'";
  151. $checkData = mysqli_query($con,$check);
  152. if (mysqli_num_rows($checkData) > 0) {
  153. echo "Request already posted";
  154. }else{
  155.  
  156. if(mysqli_query($con,$sql)){
  157.  
  158.  
  159. $notiTitle = "notification request";
  160. $notiMessage ="by".$full_name;
  161.  
  162. sendNotification($notiTitle, $notiMessage);
  163. echo "sucessfully added";
  164.  
  165. }else{
  166. echo "error in sending request";
  167. }
  168. }
  169.  
  170. }else{
  171. echo 'error';
  172. }
  173.  
  174.  
  175. function sendNotification($title, $msg) {
  176. require 'init.php';
  177. $titlee = $title;
  178. $message = $msg;
  179. $path_to_fcm = 'https://fcm.googleapis.com/fcm/send';
  180. $server_key = "your_server_key";
  181. $sql = "SELECT app_id FROM user_app_id";
  182. $result = mysqli_query($con,$sql);
  183.  
  184.  
  185. // fetch all key of devices
  186.  
  187. $finalKey=array();
  188. while($row= mysqli_fetch_array($result)){
  189. $finalKey[]=$row['app_id'];
  190. }
  191. $headers = array(
  192. 'Authorization:key=' .$server_key,
  193. 'Content-Type : application/json');
  194.  
  195. $fields = array('registration_ids'=>$finalKey, 'notification'=>array('title'=>$title, 'body'=>$message));
  196.  
  197. $payload = json_encode($fields);
  198.  
  199.  
  200. $curl_session = curl_init();
  201. curl_setopt($curl_session, CURLOPT_URL, $path_to_fcm);
  202. curl_setopt($curl_session, CURLOPT_POST, true);
  203. curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);
  204. curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
  205. curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, false);
  206. curl_setopt($curl_session, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  207. curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload);
  208. $result = curl_exec($curl_session);
  209. curl_close($curl_session);
  210. echo $result;
  211. mysqli_close($con);
  212.  
  213. }
  214. ?>
  215.  
  216. public class FirebaseInstantIdService extends FirebaseInstanceIdService {
  217. @Override
  218. public void onTokenRefresh() {
  219. String token = FirebaseInstanceId.getInstance().getToken();
  220. SharedPreferences.Editor editor = ReversifyApplication.getSharedPreference().edit();
  221. editor.putString(Utils.FCM_TOKEN, token);
  222. editor.apply();
  223.  
  224. }
  225. }
  226.  
  227. public class FirebaseMessagingServices extends FirebaseMessagingService {
  228.  
  229. public static NotificationManager notificationManager;
  230.  
  231. private String id = "";
  232. private String ji = "";
  233. private String badge = "";
  234. private String image = "";
  235. private String title = "";
  236. private String video = "";
  237. private String message = "";
  238. private String name = "";
  239.  
  240.  
  241. @SuppressLint("ApplySharedPref")
  242. @Override
  243. public void onMessageReceived(RemoteMessage remoteMessage) {
  244.  
  245. Log.e("onMessageReceived: ", remoteMessage.getData().toString());
  246. for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
  247. String key = entry.getKey();
  248. String value = entry.getValue();
  249. switch (key) {
  250. case "ji":
  251. ji = determinePushNotificationSource(value);
  252. break;
  253.  
  254. case "title":
  255. title = value;
  256. break;
  257.  
  258. case "message":
  259. message = value;
  260. break;
  261.  
  262. case "id":
  263. id = value;
  264. break;
  265. case "badge":
  266. badge = value;
  267. break;
  268. case "image":
  269. image = value;
  270. break;
  271. case "video":
  272. video = value;
  273. break;
  274. case "name":
  275. name = value;
  276. break;
  277.  
  278.  
  279. }
  280. Utils.saveNotificationCount(badge);
  281. }
  282. if (ReversifyApplication.isActivityVisible()) {
  283. Intent intent = new Intent("1000");
  284. intent.putExtra("title", title);
  285. intent.putExtra("message", message);
  286. intent.putExtra("image", image);
  287. intent.putExtra("videoid", video);
  288. intent.putExtra("ji", ji);
  289. intent.putExtra("id", id);
  290. intent.putExtra("notification_count", badge);
  291. LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
  292. } else {
  293. int idCurrentTime = (int) System.currentTimeMillis();
  294. sendNotification(idCurrentTime, title, message, ji, video, image, name, id);
  295. //set up badge count for supported devices
  296. if (ShortcutBadger.isBadgeCounterSupported(getApplicationContext())) {
  297. ShortcutBadger.applyCount(getApplicationContext(), Integer.parseInt(getSharedPreference().getString(Utils.NOTIFICATION_COUNT, null)));
  298. }
  299. }
  300. }
  301.  
  302. /**
  303. * generating push notification
  304. */
  305. private void sendNotification(int id, String title, String messageBody, String type, String openID, String image, String name, String notificaiton_id) {
  306. Intent broadcastIntent = new Intent(this, SplashScreen.class);
  307. broadcastIntent.putExtra("type", type);
  308. broadcastIntent.putExtra("image", image);
  309. broadcastIntent.putExtra("message", messageBody);
  310. broadcastIntent.putExtra("id", notificaiton_id);
  311. broadcastIntent.putExtra("openID", openID);
  312. broadcastIntent.setAction(Intent.ACTION_MAIN);
  313. broadcastIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  314. broadcastIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  315. broadcastIntent.putExtra("name", name);
  316.  
  317. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  318. Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  319.  
  320. NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
  321. .setSmallIcon(R.drawable.push_notification)
  322. .setContentTitle(title)
  323. .setContentText(messageBody)
  324. .setAutoCancel(true)
  325. .setPriority(Notification.PRIORITY_HIGH)
  326. .setSound(defaultSoundUri)
  327. .setContentIntent(pendingIntent);
  328. notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  329. notificationManager.notify(id, notificationBuilder.build());
  330. }
  331.  
  332.  
  333. }
Add Comment
Please, Sign In to add comment