NewFirebaseCaller.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. require __DIR__ . "/vendor/autoload.php";
  3. use Kreait\Firebase\Factory;
  4. use Kreait\Firebase\Messaging\Notification;
  5. use Kreait\Firebase\Messaging\CloudMessage;
  6. $factory = (new Factory)->withServiceAccount("narkampen-firebase-adminsdk-k42j5-d3d0354e37.json");
  7. $messaging = $factory->createMessaging();
  8. $deviceToken = "ck4OuDu3RweBW5s8ncj7FV:APA91bHo58TUsXVgTB8cLOKHkpVaSydej_TFZLJyuPMrAEhgWNxLRRd83a_6Mz1YnZYtzz5sTjnowlbYVwyp3JudKa-jl3UY1xNGae9FpYU7bn6hSHLzW-OuzM87w149lQg6zYC6PXJg";
  9. // NOTIFICATION EXAMPLE
  10. $title = 'My Notification Title';
  11. $body = 'My Notification Body';
  12. $imageUrl = 'http://lorempixel.com/400/200/';
  13. $notification = Notification::fromArray([
  14. 'title' => $title,
  15. 'body' => $body,
  16. 'image' => $imageUrl,
  17. ]);
  18. $notification = Notification::create($title, $body);
  19. $changedNotification = $notification
  20. ->withTitle('Changed title')
  21. ->withBody('Changed body')
  22. ->withImageUrl('http://lorempixel.com/200/400/');
  23. // $message->withNotification($notification);
  24. // NOTIFICATION EXAMPLE END
  25. // DATA Example
  26. $data = [
  27. 'first key' => "First value",
  28. "second key" => "Second value"];
  29. // $message->withData($data);
  30. // DATA EXAMPLE END
  31. $message = CloudMessage::withTarget('token', $deviceToken)
  32. ->withNotification($notification) // OPTIONAL
  33. ->withData($data); // OPTIONAL
  34. /*
  35. $message = CloudMessage::fromArray([
  36. 'token' => $deviceToken,
  37. 'notification' => [],
  38. 'data' => []
  39. ]);
  40. */
  41. $messaging->send($message);