public static function send($devices, Package $package, $user_id) { $client = app('JPush\\JPushClient'); $installIds = $devices->pluck('install_id')->toArray(); if (count($installIds) == 0) { throw new \Exception(Lang::get('errors.device_required'), 400); } try { $result = $client->push()->setPlatform(M\all)->setAudience(M\registration_id($installIds))->setMessage(M\message($package->toJson(), null, "package"))->send(); } catch (\Exception $e) { throw new \Exception(Lang::get('errors.push_failed'), 500); } if ($result->isOk) { $push = new Push(); $push->package_id = $package->id; $push->user_id = $user_id; $push->sendno = $result->sendno; $push->msg_id = $result->msg_id; $push->is_ok = $result->isOk; $push->save(); foreach ($devices as $device) { $pd = new PushDevice(); $pd->device_id = $device->id; $pd->push_id = $push->id; $pd->status = 1; // $pd->user_id = Auth::id(); $pd->save(); } return $push; } throw new \Exception(Lang::get('errors.push_failed'), 500); }
/** * Execute the console command. * * @return mixed */ public function handle() { /** * @var Package $package */ $package = Package::all()->random(); $devices = Device::all(); $installIds = $devices->pluck('push_id')->toArray(); $this->info('App: ' . $package->app_name . '(' . $package->apk . ')'); $this->info('Devices: ' . $devices->pluck('alias')); $this->line('==========================='); $result = $this->client->push()->setPlatform(M\all)->setAudience(M\registration_id($installIds))->setMessage(M\message($package->toJson(), null, "package"))->send(); $this->info('Push success'); // print_r($result->response->body); // $msg_ids = $result->msg_id; // $report = $this->client->report($msg_ids); // print_r($report->received_list); $push = new Push(); $push->package_id = $package->id; $push->user_id = 4; $push->sendno = $result->sendno; $push->msg_id = $result->msg_id; $push->is_ok = $result->isOk; $push->save(); print_r($push->toJson()); }
/** * Execute the job. * * @param JPushClient $client */ public function handle(JPushClient $client) { $error = ''; try { if ($this->download($this->url)) { $package = Package::createFromFile($this->tmpFile, basename($this->url), $this->user->id); $result = Push::send($this->devices, $package, $this->user->id); try { if ($this->devices->count() > 1) { $msg = "已在向{$this->devices->count()}台设备发出推送..."; } else { $msg = "已向{$this->devices->first()->alias}发出推送..."; } Log::info($msg); } catch (\Exception $e) { Log::error('[PushApk Job]推送失败' . $e->getMessage()); return; } } else { $error = trans('errors.download_failed'); Log::error('[PushApk Job]下载文件失败: ' . $this->url); } } catch (Exception $e) { $error = $e->getMessage(); Log::error('[PushApk]' . $e->getMessage() . ' ' . $this->url); } $installIds = $this->devices->pluck('install_id')->toArray(); try { $result = $client->push()->setPlatform(M\all)->setAudience(M\registration_id($installIds))->setNotification(M\notification('出现错误,请检查URL是否正确'))->send(); } catch (Exception $ignored) { } }
/** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Request $request) { $device = Device::current(); if (Input::hasFile('file')) { $inputFile = Input::file('file'); $package = Package::createFromInputFile($inputFile, $device->user_id); } else { $package = Package::findOrFailFromArg(Input::get('package'), $device->user_id); } $ids = explode(',', Input::get('devices')); if (count($ids) == 1 && !is_numeric($ids[0])) { $token = Token::whereValue($ids[0])->valid()->first(); if ($token) { $devices = Device::whereId($token->owner)->get(); } else { return Response::error(trans('errors.expired_device_qrcode') . $ids[0], 400); } } if (empty($devices)) { $devices = Device::whereIn('id', $ids)->where(function ($query) use($device) { $query->whereUserId($device->user_id)->orWhere(function ($query) use($device) { $authed_device_ids = DUAuth::whereUserId($device->user_id)->lists('device_id'); $query->whereIn('id', $authed_device_ids); }); })->get(); } try { $push = Push::send($devices, $package, $device->user_id); return Response::json($push); } catch (\Exception $e) { return Response::exception($e); } }
/** * Display a listing of the resource. * * @return Response */ public function getIndex() { // Package::join('pushes', 'packages.id', '=', 'pushes.package_id') // ->join('push_devices', 'pushes.id', '=', 'push_devices.push_id') // ->where('push_devices.device_id', '=', $device->id) // ->orderBy('pushes.created_at', 'desc') // ->groupBy('packages.id') // ->select('packages.*', 'pushes.id as push_id') $pushes = Push::join('push_devices', 'pushes.id', '=', 'push_devices.push_id')->join('devices', 'devices.id', '=', 'push_devices.device_id')->where('devices.user_id', '=', Auth::id())->with('package')->orderBy('created_at', 'desc')->select('pushes.*')->paginate(15); return view('push.index')->with('pushes', $pushes); }
/** * 提交Push */ public function postPush() { $data = Input::all(); $devices_ids = Input::get('devices'); $devices = Device::whereUserId(Auth::id())->whereIn('devices.id', $devices_ids)->get(); if (!Input::has('package') && Input::has('url')) { $this->dispatch(new App\Jobs\PushApk(Auth::user(), $data['url'], $devices)); return redirect('/install')->withToast('服务器正在努力处理中,请稍等...'); } $package = Package::findOrFailFromArg($data['package'], Auth::id()); $result = Push::send($devices, $package, Auth::id()); try { if ($devices->count() > 1) { $msg = "已在向{$devices->count()}台设备发出推送..."; } else { $msg = "已向{$devices->first()->alias}发出推送..."; } return redirect('/install')->withToast($msg); } catch (\Exception $e) { return Redirect::back()->withToast($result); } }
public function getRecord() { $pushs = Push::paginate(15); return View::make('backend.pages.push-record')->withPushs($pushs); }