Skip to content

Instantly share code, notes, and snippets.

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 mkdesignn/7a80a17a695f1eab761ad0777e07862d to your computer and use it in GitHub Desktop.
Save mkdesignn/7a80a17a695f1eab761ad0777e07862d to your computer and use it in GitHub Desktop.
Send notification in laravel
$this->notificationService($device->device_token, $notification['target_screen'], $message['body'], $message['title']);
function notificationService($token, $targetScreen, $body = "", $title = "")
{
$optionBuilder = new OptionsBuilder();
$optionBuilder->setTimeToLive(60 * 20);
$optionBuilder->setCollapseKey('type_a');
$optionBuilder->setContentAvailable(true);
try {
$optionBuilder->setPriority(OptionsPriorities::high);
} catch (InvalidOptionsException $e) {
Log::error("Could not set priority to send notification");
}
$dataBuilder = new PayloadDataBuilder();
$dataBuilder->addData([
'body' => $body,
'targetScreen' => $targetScreen,
'id' => 'view',
'group' => 'GROUP',
'show_in_foreground' => true,
'content_available' => true,
'priority' => 'high'
]);
$notificationBuilder = new PayloadNotificationBuilder($title);
$notificationBuilder
->setTitle($title)
->setBody($body)
->setSound('default')
->setClickAction('fcm.ACTION.HELLO');
$option = $optionBuilder->build();
$notification = $notificationBuilder->build();
$data = $dataBuilder->build();
Log::info("Creating a notification with the following data: ", $data->toArray());
$downstreamResponse = $this->sendNotification($token, $option, $notification, $data);
Log::info('FCM DownStream Response', [$downstreamResponse]);
$this->deleteTokens($downstreamResponse->tokensToDelete());
$this->modifyTokens($downstreamResponse->tokensToModify());
foreach ($downstreamResponse->tokensToRetry() as $token) {
$this->sendNotification($token, $option, $notification, $data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment