“What will be hot and what to learn in 2022? This article is participating in the” Talk about 2022 Technology Trends “essay campaign.

This article environment ThinkPHP5.0, PHP7.1, Mysql5.7 do not understand can comment or contact me email: [email protected] copyright belongs to OwenZhang. Commercial reprint please contact OwenZhang for authorization, non-commercial reprint please indicate the source.

Jpush/jpush is introduced

This is a PHP version of the JPush REST API, which is provided by aurora Push and generally supports the latest API features.

The corresponding REST API documentation: https://docs.jiguang.cn/jpush/server/push/server_overview/

Supported PHP versions: 5.3.3 ~ 5.6.x, 7.x

To be compatible with PHP versions below 5.3.3, you can use the v3 branch code. You can also download v3.4.x Composer by clicking on the link.

Jpush/jpush download

Install using Composer

Run $PHP Composer. Phar install or $composer install to install.Copy the code

Download the source code directly to install

Downloading the source code directly is another way to install the SDK, but this is not recommended due to maintenance issues with version updates, but for various reasons it is not possible to use Composer, so we provide an alternative in this case.

  • Download the source code package and unzip it into your project
  • Introducing Autoload into the project:
require 'path_to_sdk/autoload.php';
Copy the code

The code examples

Push interface parent class

application/common/JPush.php

<? PHP /** * namespace app\common; use JPush\Client; class JPush { private $key = ''; private $secret = ''; use InstanceTrait; public function _init() { $this->key = '3412f'; $this->secret = '2c23'; Public function push($registrationId, $title, $text, $url); $itemId) { $registrationId = array_filter($registrationId); \Log::info(var_export([$registrationId, $title, $text, $url, $itemId], true)); $this->_init(); $client = new Client($this->key, $this->secret); $push = $client->push(); $push->setPlatform('all'); $push->addRegistrationId($registrationId); $push->addAndroidNotification($title, $text); $push->androidNotification($text, ['title' => $title, 'alert_type' => 2, 'extras' => ['url' => $url, 'id' => $itemId]]);  $push->iosNotification(['title' => $title, 'body' => $text], ['extras' => ['url' => $url, 'id' => $itemId]]); $push->options(['apns_production' => false]); $push->send(); }}Copy the code

Push service class

application/lucky/push/service/PushService.php

<? PHP /** * push service */ namespace app lucky push service; use app\common\JPush; use app\lucky\follow\service\FollowService; use app\lucky\push\model\UserPushConfigModel; use app\lucky\subscribe\service\SubscribeService; use app\sports\match\service\FollowMatchService; use app\sports\match\service\SportsApiService; Class PushService {public function push() {try {$push = JPush::getInstance()->push([' 1517badF006e81e '], 'I am the title ', 'I'm content ', 'GameDetails:1909991'); var_dump($push); } catch (\Exception $e) { var_dump($e->getMessage()); Public function pushConfig($userId, $sys, $ext) {$userPushConfigModel = new userPushConfigModel (); $result = $userPushConfigModel->updateConfig($userId, $sys, ['ext' => json_encode($ext), 'update_time' => time()]); If ($result) {return [' code '= > 0,' MSG '= >' add success]. } return ['code' => -1, 'MSG' => 'add failed ']; } public function getPushConfig($userId, $sys) {$userPushConfigModel = new userPushConfigModel (); $result = $userPushConfigModel->getConfigByUserId($userId, $sys); $data = isset($result['ext']) ? $result['ext'] : ''; $data = (array)json_decode($data, true); Return [' code '= > 0,' MSG '= >' success 'and' data '= > $data]; } /** * public function blogPush($authorId, $title, $text) $followService = new followService (); $followListId = $followService->getAuthorFollowList($authorId, 'sports'); $pushModel = new UserPushConfigModel(); $pushConfig = $pushModel->getConfigByUserIdArr($followListId, 'sports'); $identifyArr = []; foreach ($pushConfig as $value) { $ext = (array)json_decode($value['ext'], true); if (in_array('information', $ext)) { $identifyArr[] = $value['identify']; } } if (! empty($identifyArr)) { try { JPush::getInstance()->push($identifyArr, $title, $text, 'InfoDetails', $blogId); } catch (\Exception $exception) { \Log::error($exception->getMessage()); Public function loginLinkPush($userId, $identify, $sys = '343') { $userPushConfigModel = new UserPushConfigModel(); $config = $userPushConfigModel->getConfigByUserId($userId, 'sports'); if (empty($config)) { $data = [ 'user_id' => $userId, 'identify' => $identify, 'update_time' => time(), 'sys' => $sys, 'ext' => json_encode(['start' => true, 'end' => true, 'score' => true, 'news' => true, 'information' => true]) ]; $result = $userPushConfigModel->addConfig($data); If (empty ($result)) {return [' code '= > 1,' MSG '= >' add aurora push failure]; } return ['code' => 0, 'MSG' => ' ']; } $data = [ 'identify' => $identify, 'update_time' => time(), ]; $result = $userPushConfigModel->updateConfig($userId, $sys, $data); If (empty ($result)) {return [' code '= > 1,' MSG '= >' update the aurora push failure]; } return ['code' => 0, 'MSG' => 'update ']; Public function logoutLinkPush($userId, $userId, $userId) $sys = '343') { $userPushConfigModel = new UserPushConfigModel(); $data = [ 'identify' => '', 'update_time' => time(), ]; $result = $userPushConfigModel->updateConfig($userId, $sys, $data); If (empty ($result)) {return [' code '= > 1,' MSG '= >' log out, update the aurora push failure]; } return [' code '= > 0,' MSG '= >' log out, update the aurora push success]. }}Copy the code

Push the instance

application/lucky/admin/controller/Blog.php

$data['author_id']=123; $data['title']= $data['title']; $title = '$title '; $pushService = new PushService(); $pushService->blogPush($data['author_id'], $title, mb_substr($data['title'], 0, 10) . '... ', $result);Copy the code

Initialize the

use JPush\Client as JPush; . . $client = new JPush($app_key, $master_secret); .Copy the code

OR

$client = new \JPush\Client($app_key, $master_secret);
Copy the code

Simple push

$client->push()
    ->setPlatform('all')
    ->addAllAudience()
    ->setNotificationAlert('Hello, JPush')
    ->send();
Copy the code

Exception handling

$pusher = $client->push();
$pusher->setPlatform('all');
$pusher->addAllAudience();
$pusher->setNotificationAlert('Hello, JPush');
try {
    $pusher->send();
} catch (\JPush\Exceptions\JPushException $e) {
    // try something else here
    print $e;
}
Copy the code

Push production examples

Note: This is an example and should not be used directly in the real world!!

The examples folder in the download contains simple sample code for developers to quickly learn how to use the library.

Easy to use

Fill in the corresponding appKey and masterSecret first, and optionally set Registration_id.

To run the example code in push_example.php:

$PHP examples/push_example.php = $PHP examples/push_example.phpCopy the code

You can also edit the related sample file to change the parameters to see the effect of the execution

test

# edit the tests/bootstrap. PHP file $composer tests/JPush/ xxtest.php $composer tests/JPush/ xxtest.phpCopy the code

Buy me a cup of coffee 🙂

Feel helpful to you, give me a tip, thank you!

Wechat appreciation code link, click to jump:

www.owenzhang.com/wechat_rewa…