I face the issue that if device no longer exists the fcm response containing the error like that
{
"message": "Client error: `POST https://fcm.googleapis.com/v1/projects/internationalfriendspusher/messages:send'resulted in a `404 Not Found` response:\n{\n \"error\": {\n \"code\":
404,\n \"message\": \"Requested entity was not found.\",\n \"status\":
\"NOT_FOUND\",\n \"detail (truncated...)\n",
"exception": "Kreait\\Firebase\\Exception\\Messaging\\NotFound",
"file": "/var/www/vhosts/lvps87-230-85-
233.dedicated.hosteurope.de/pusherang/MainApp/vendor/kreait/firebase-
php/src/Firebase/Exception/MessagingException.php"
}
i actually send bulk notification having device ids inside an array and loop through it when any device id to the corresponding token were no longer exist it break my code so i want to handle that and continue to the next device id
my request payload
{
"message": {
"content":"My Test notification",
"content_available":"zXCzCzDXs",
"message_url":"www.example.com",
"priority":"dfgdfgfd",
"title":"Test"
},
"device_ids":[
"4706277e9565496",
"f02f1f4558206539"
]
}
code
foreach($input['device_ids'] as $deviceId)
{
$pusher = Push::where('device_id' , $deviceId )
->where('push_enable' , 'true')
->first();
if($pusher)
{
if(strtolower($pusher->push_enable) == "true")
{
$deviceToken = $pusher->registration_id;
$message = CloudMessage::withTarget('token', $deviceToken);
$title = $input['message']['title'];
$body = $input['message']['content'];
$notification = Notification::fromArray([
'title' => $title,
'body' => $body
]);
$message = $message->withNotification($notification);
try
{
// Here notification send to device and here my code breaks if device token not validate or user install app
$this->messaging->send($message));
$device = new Device;
$device->deviceId = $deviceId;
$device->title = $title;
$device->content = $body;
$device->message_url = $input['message']['message_url'];
$device->priority = $input['message']['priority'];
$device->content_available = $input['message']['content_available'];
$status = $device->save();
if($status)
{
continue;
}
}
catch(Exception $e)
{
echo "Permission denied for Device: ".$deviceId." having token ".$deviceToken." from Firebase";
continue;
}
}
else
{
continue;
}
}
else
{
echo "Device having id ".$deviceId." were not found";
continue;
}
}