NewFirebaseCaller.php 1.4 KB

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