如果设备令牌不再有效,我想处理从fcm生成的异常

时间:2018-09-24 13:06:56

标签: laravel firebase-cloud-messaging

我遇到的问题是,如果设备不再存在,则fcm响应中将包含此类错误

{
    "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"
}

我实际上发送了一个在数组内具有设备ID的批量通知,并在不再存在对应令牌的任何设备ID时循环遍历它,这破坏了我的代码,因此我想处理并继续下一个设备ID

我的请求有效载荷

{
    "message": {
    "content":"My Test notification",
    "content_available":"zXCzCzDXs",
    "message_url":"www.example.com",
    "priority":"dfgdfgfd",
    "title":"Test"
    },
    "device_ids":[
    "4706277e9565496",
    "f02f1f4558206539"
    ]
}

代码

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 个答案:

答案 0 :(得分:2)

您快到了-这是一个命名空间问题。

catch(Exception $e)在一个命名空间文件中(即文件顶部是namespace App\Foo\Bar),它捕获的内容并不多-也是命名空间,因此您只会捕获{{1} }。

在文件顶部放置一个App\Foo\Bar\Exception别名将告诉PHP使用 root 异常而不是命名空间的异常。

或者,use Exception以\开头,将执行相同的操作。

侧面说明:您可以采用相同的方式捕获特定于 的异常,即:

catch(\Exception $e)