Created
May 17, 2019 09:32
-
-
Save mkdesignn/7a80a17a695f1eab761ad0777e07862d to your computer and use it in GitHub Desktop.
Send notification in laravel
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
$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