-
-
Save ryandoss/725ddade49667831b7fe5292aa0fbaaf to your computer and use it in GitHub Desktop.
Push Notifications for Laravel and Expo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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