I am sending a notification to a user and want to return a value to know if the notification was sent successfully or not.
The notification is a FCM push notification, and there are multiple reasons why it could fail and need to act accordingly based on the success/failure.
I call the notification from a controller like this:
$return = $user->notify(new AlertNotification([
'title' => $data['title'],
'body' => $data['body'],
'url' => $data['url'],
'method' => $data['method'],
'token' => $token,
'sound' => $sound
]));
The notification works succesfully, but there is nothing in the $return variable.
Here is the notification code:
public function toFirebase($notifiable)
{
if ($this->token) {
$firebaseMessage = (new FirebaseMessage)
->fromRaw([
'message' => [
"data" => [
'deeplink' => $this->url
],
"notification" => [
"title" => $this->title,
'body' => $this->body,
],
"android" => [
'collapse_key' => 'general_key',
"ttl" => "86400s",
'notification' => [
'sound' => $this->sound ? 'sound.wav' : null,
'channel_id' => 'alert-channel',
'notification_count' => 1
]
],
"apns" => [
"payload" => [
'aps' => [
'sound' => $this->sound ? 'alert_sound.wav' : null,
'badge' => 1
]
]
],
"token" => $this->token,
]
]
)->asNotification();
if ($firebaseMessage) {
$statusCode = $firebaseMessage->status();
if (in_array($statusCode,[400,403])) {
Log::error('The Firebase Message returned a 400 error for token: '.$this->token.' message: '.print_r($firebaseMessage,true));
return false;
} else if ($statusCode === 200) {
return true;
} else {
Log::error('The Firebase Message returned an known status: '.$statusCode.' for token: '.$this->token.' message: '.print_r($firebaseMessage,true));
return false;
}
} else
return null;
} else
return false;
}
The notification is successful, and should return true, but no value is returned. Can someone help me understand how to get a return value from a notification?