Skip to content

Instantly share code, notes, and snippets.

@ryandoss
Forked from CarterBland/PushTokenTrait.php
Last active November 8, 2018 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryandoss/725ddade49667831b7fe5292aa0fbaaf to your computer and use it in GitHub Desktop.
Save ryandoss/725ddade49667831b7fe5292aa0fbaaf to your computer and use it in GitHub Desktop.
Push Notifications for Laravel and Expo
<?php
namespace App\Services;
/**
* Model that holds the Expo Push Tokens
*
* Related to App\User
*/
use App\PushToken;
class NotificationHandler
{
/**
* Send Notifications
*
* @param String $title
* @param String $body
* @param Array $users
*/
public function send($title, $body, $users = [])
{
$query = count($users) > 0
? PushToken::whereIn('user_id', collect($users)->pluck('id')->toArray())
: PushToken::query();
$query->chunk(100, function ($tokens) use ($title, $body) {
$data = [];
foreach ($tokens as $token) {
$data[] = [
'to' => $token->token,
'title' => $title,
'body' => $body
];
}
$request = curl_init();
curl_setopt_array($request, [
CURLOPT_URL => 'https://exp.host/--/api/v2/push/send',
CURLOPT_HTTPHEADER => [
'Content-Type: application/json'
],
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => json_encode($data)
]);
curl_exec($request);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment