BarkChannel.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace App\Channels;
  3. use Helpers;
  4. use Http;
  5. use Illuminate\Notifications\Notification;
  6. use Log;
  7. class BarkChannel
  8. {
  9. public function send($notifiable, Notification $notification)
  10. {
  11. if (method_exists($notification, 'toBark')) {
  12. $message = $notification->toBark($notifiable);
  13. } else {
  14. $message = $notification->toCustom($notifiable);
  15. }
  16. $response = Http::timeout(15)->get('https://api.day.app/'.sysConfig('bark_key').'/'.$message['title'].'/'.$message['content']);
  17. if ($response->ok()) {
  18. $ret = $response->json();
  19. // 发送成功
  20. if ($ret['code'] === 200) {
  21. Helpers::addNotificationLog($message['title'], $message['content'], 3);
  22. return $ret;
  23. }
  24. // 发送失败
  25. Helpers::addNotificationLog($message['title'], $message['content'], 3, 'admin', -1, $message);
  26. return false;
  27. }
  28. // 发送错误
  29. Log::error('Bark消息推送异常:'.var_export($response, true));
  30. return false;
  31. }
  32. }