FCMMessageing.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. // Send notifications One device
  3. // Hämta token från sql användaren
  4. $token = "eMvkzPwkTheSeFLFAE5fho:APA91bE-pb5_0xoMn_Bza6-HrgpI5ufKPZ7zhF4NM_leYdfgYTk4MZJdfpfiKDkijUuQAiK7iaT7rXJucF20uh1GvmgjiomiuQehnDHcjUkGjaSvkQtVfj_s0AtLSPjIJz8EQ5Aujkfk";
  5. $message = "TEST MEDDELANDE FRÅN SERVERN";
  6. $title = "TEST TITEL";
  7. $url = "https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send";
  8. $fields = array(
  9. "message" => array(
  10. "token" => $token,
  11. "notification" =>
  12. array(
  13. "body" => $message,
  14. "title" => $title
  15. )
  16. )
  17. );
  18. $fields = json_encode($fields);
  19. $headers = array(
  20. 'Content-Type: application/json',
  21. 'Authorization: Bearer ' . "AAAAGStp88I:APA91bERuLuU7DuAyW2yhoLQeiJizGZVsezzEdLhRa9v9Ehy7uYmHFvBTMrQooMDg0oaGNZE3DXd3Tl3E10JP58OCtuL7z1UQWo071tOCR8Ede3YUXDErs6Zkt8z1VxcBAkOxzVInId3"
  22. );
  23. $curl = curl_init();
  24. curl_setopt($curl, CURLOPT_URL, $url);
  25. curl_setopt($curl, CURLOPT_POST, true);
  26. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  27. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  28. curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
  29. $response = curl_exec($curl);
  30. var_dump($response);
  31. curl_close($curl);
  32. /*
  33. curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{
  34. "message":{
  35. "notification":{
  36. "title":"FCM Message",
  37. "body":"This is an FCM Message"
  38. },
  39. "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
  40. }}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send
  41. */