/** * 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) { } }
/** * 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()); }
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); }
/** * android,按id识别发送; * @param $title string 消息内容 * @param $registration_ids array 设备id列表; */ public function sendByIds($title, $registration_ids) { $this->setPlatform = M\platform('android'); $this->setAudience = M\registration_id($registration_ids); $this->setNotification = M\notification($title); return $this->send(); }
/** * 推送 push_message */ public function push_message_registration($ids = array(), $notification = array()) { if ($ids && $notification) { vendor('jpush.autoload'); $app_key = C('API_PUSH_JPUSH.key'); $master_secret = C('API_PUSH_JPUSH.secret'); $client = new JPushClient($app_key, $master_secret); $client->push()->setPlatform(M\all)->setAudience(M\registration_id($ids))->setNotification(M\notification($notification['title'], M\android($notification['title'], $notification['title'], NULL, $notification['extras']), M\ios($notification['title'], 'default', 1)))->setOptions(M\options(NULL, NULL, NULL, true))->send(); } }
public function testPayload() { $array = array("platform" => array("android", "ios", "winphone"), "audience" => array("tag" => array("tag1", "tag2"), "tag_and" => array("tag3"), "alias" => array("alias1", "alias2"), "registration_id" => array("id1", "id2")), "notification" => array("alert" => "notification alert", "ios" => array("alert" => "ios alert", "sound" => "happy", "badge" => 1, "content-available" => 1, "extras" => array("key1" => "value1", "key2" => "value2")), "android" => array("alert" => "android alert", "title" => "android title", "builder_id" => 1, "extras" => array("key1" => "value1", "key2" => "value2")), "winphone" => array("alert" => "winphone alert", "title" => "winphone title", "_open_page" => "/friends.xaml", "extras" => array("key1" => "value1", "key2" => "value2"))), "message" => array("msg_content" => "message msg content", "title" => "message title", "content_type" => "message content type", "extras" => array("key1" => "value1", "key2" => "value2")), "options" => array("sendno" => 654321, "time_to_live" => 60, "override_msg_id" => 123456, "apns_production" => false)); $json = json_encode($array); $payload = new M\PushPayload(null); $result = $payload->setPlatform(M\platform('android', 'ios', 'winphone'))->setAudience(M\audience(M\tag(array("tag1", "tag2")), M\tag_and(array("tag3")), M\alias(array("alias1", "alias2")), M\registration_id(array("id1", "id2"))))->setNotification(M\notification('notification alert', M\ios('ios alert', 'happy', 1, true, array("key1" => "value1", "key2" => "value2")), M\android('android alert', 'android title', 1, array("key1" => "value1", "key2" => "value2")), M\winphone('winphone alert', 'winphone title', '/friends.xaml', array("key1" => "value1", "key2" => "value2"))))->setMessage(M\message('message msg content', 'message title', 'message content type', array("key1" => "value1", "key2" => "value2")))->setOptions(M\options(654321, 60, 123456, false))->getJSON(); $this->assertEquals($json, $result); }
if (isset($uid) && isset($msg) && isset($role)) { $rs_arr = $d2b->select("user_main", ["channelid"], ["uid" => $uid]); $channelid = $rs_arr[0]['channelid']; switch ($role) { case 1: $client = new JPushClient($app_key_c, $master_secret_c); try { $result = $client->push()->setPlatform(M\platform('ios', 'android'))->setAudience(M\audience(M\registration_id(array($channelid))))->setNotification(M\notification($msg, M\android($msg), M\ios($msg, 'happy', 1, true, null, 'THE-CATEGORY')))->setMessage(M\message($msg, null, null, array('key' => 'value')))->setOptions(M\options(123456, null, null, false, 0))->send(); $return_arr = ["channelid" => $channelid]; include "../include/return_data.php"; } catch (APIRequestException $e) { echo 'code : ' . $e->code; } break; case 2: $client = new JPushClient($app_key_p, $master_secret_p); try { $result = $client->push()->setPlatform(M\platform('ios', 'android'))->setAudience(M\audience(M\registration_id(array($channelid))))->setNotification(M\notification($msg, M\android($msg), M\ios($msg, 'happy', 1, true, null, 'THE-CATEGORY')))->setMessage(M\message($msg, null, null, array('key' => 'value')))->setOptions(M\options(123456, null, null, false, 0))->send(); $return_arr = ["channelid" => $channelid]; include "../include/return_data.php"; } catch (APIRequestException $e) { echo 'code : ' . $e->code; } break; default: echo json_encode(0); break; } } else { echo json_encode(0); }
public function testSendByTagRegistrationID_0() { $client = new JPushClient($this->appKey, $this->masterSecret); $response = $client->push()->setPlatform(M\all)->setAudience(M\audience(M\tag(array($this->tag_no)), M\registration_id(array($this->registration_id1))))->setNotification(M\notification($this->alert))->send(); $this->assertTrue($response->isOk === true); }
echo 'code : ' . $e->code . $br; echo 'Error Message : ' . $e->message . $br; echo 'Response JSON : ' . $e->json . $br; echo 'rateLimitLimit : ' . $e->rateLimitLimit . $br; echo 'rateLimitRemaining : ' . $e->rateLimitRemaining . $br; echo 'rateLimitReset : ' . $e->rateLimitReset . $br; } catch (APIConnectionException $e) { echo 'Push Fail: ' . $br; echo 'Error Message: ' . $e->getMessage() . $br; //response timeout means your request has probably be received by JPUsh Server,please check that whether need to be pushed again. echo 'IsResponseTimeout: ' . $e->isResponseTimeout . $br; } echo $br . '-------------' . $br; //full push try { $result = $client->push()->setPlatform(M\platform('ios', 'android'))->setAudience(M\audience(M\registration_id(array('0802f1b5895'))))->setNotification(M\notification('Hi, JPush', M\android('Hi, android'), M\ios('Hi, ios', 'happy', 1, true, null, 'THE-CATEGORY')))->setMessage(M\message('msg content', null, null, array('key' => 'value')))->setOptions(M\options(123456, null, null, false, 0))->printJSON()->send(); echo 'Push Success.' . $br; echo 'sendno : ' . $result->sendno . $br; echo 'msg_id : ' . $result->msg_id . $br; echo 'Response JSON : ' . $result->json . $br; } catch (APIRequestException $e) { echo 'Push Fail.' . $br; echo 'Http Code : ' . $e->httpCode . $br; echo 'code : ' . $e->code . $br; echo 'message : ' . $e->message . $br; echo 'Response JSON : ' . $e->json . $br; echo 'rateLimitLimit : ' . $e->rateLimitLimit . $br; echo 'rateLimitRemaining : ' . $e->rateLimitRemaining . $br; echo 'rateLimitReset : ' . $e->rateLimitReset . $br; } catch (APIConnectionException $e) { echo 'Push Fail: ' . $br;