4

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;
  }
}

1 Answers1

3

You were nearly there - this is a namespacing issue.

catch(Exception $e), within a namespaced file (i.e. there's namespace App\Foo\Bar up the top of the file), doesn't catch much - it's namespaced too, so you'd only be catching App\Foo\Bar\Exception.

Putting a use Exception alias at the top of the file will tell PHP to use the root Exception rather than a namespaced one.

Alternatively, catch(\Exception $e), with the leading \, will do the same thing.

Side note: You can catch specific exceptions in the same way, i.e.:

catch(\Kreait\Firebase\Exception\Messaging\NotFound $e)
ceejayoz
  • 176,543
  • 40
  • 303
  • 368