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()); }
/** * Show the form for creating a new resource. * * @return Response */ public function postCreate() { $push = new Push(); $push->user_id = Auth::user()->id; $push->title = Input::get('title'); $push->content = Input::get('content'); $push->target = "全部用户"; $push->save(); self::pushMessageToSingleBatch(); return redirect('/admin/push/record'); }