Aurora recently required to push the push message sent to the client, so here recorded during use.
Aurora push server document:
https://docs.jiguang.cn/jpush/server/push/server_overview/
Aurora push server PHP Code:
https://github.com/jpush/jpush-api-php-client
Aurora push installation project in laravel
composer require jpush/jpush
We create a jpush.php file in the config directory to get key and secret
<?php return [ 'app_key' => env('JPUSH_APP_KEY', ''), 'master_secret' => env('JPUSH_MASTER_SECRET', ''), 'apns_production' => env('JPUSH_APNS_PRODUCTION', true), ];
Then the parameters in the configuration file .env
JPUSH_APP_KEY= JPUSH_MASTER_SECRET= JPUSH_APNS_PRODUCTION=true
Then we in the app directory, create a Services Directory, and create JPushService.php
<?php namespace App\Services; use JPush\Client as JPush; use Log; class JPushService { protected static $client = null; // push type const PUSH_TYPE_ALL = 1; const PUSH_TYPE_TAG = 2; const PUSH_TYPE_ALIAS = 3; const PUSH_TYPE_REG_ID = 4; private function __construct() { } private function __clone() { } /** * Get instances */ public static function getInstance() { if (!self::$client) { self::$client = new JPush(config('jpush.app_key'), config('jpush.master_secret'), null); } return self::$client; } /** * To android or ios push message */ public static function pushNotify($params) { // push platform $platform = $params['platform'] ?? 'all'; // title push $title = $params['title'] ?? ''; // push content $content = $params['content'] ?? ''; // notification bar style ID $builder_id = $params['builder_id'] ?? 0; // additional fields $extras = $params['extras'] ?? ''; // push type $type = $params['type'] ?? ''; // push target (registration ID) $reg_id = $params['reg_id'] ?? ''; // push target (label) $tag = $params['tag'] ?? ''; // push target (alias) $alias = $params['alias'] ?? ''; try { $push = self::getInstance()->push(); // set the platform $push->setPlatform($platform); switch ($type) { case self::PUSH_TYPE_ALL: $push->addAllAudience(); break; case self::PUSH_TYPE_TAG: $push->addTag($tag); break; case self::PUSH_TYPE_ALIAS: $push->addAlias($alias); break; case self::PUSH_TYPE_REG_ID: $push->addRegistrationId($reg_id); break; } $push->androidNotification($content, [ 'title' => $title, 'builder_id' => $builder_id, 'extras' => $extras, ])->iosNotification($content, [ 'sound' => 'sound', 'badge' => '+1', 'extras' => $extras ])->options([ 'apns_production' => config('jpush.apns_production', true), // represents a long (in seconds) messages remain offline 'time_to_live' => 86400, ]); $response = $push->send(); if ($response['http_code'] != 200) { Log::channel('jpush')->error(json_encode($response, JSON_UNESCAPED_UNICODE)); } return $response; } catch (\Throwable $e) { Log::channel('jpush')->error(json_encode([ 'file' => $e->getFile(), 'line' => $e->getLine(), 'message' => $e->getMessage(), 'params' => $params, ], JSON_UNESCAPED_UNICODE)); } } /** * Get the specified device aliases and labels */ public static function getDevices($reg_id) { $response = self::getInstance()->device()->getDevices($reg_id); if ($response['http_code'] == 200) { return $response['body']; } return []; } /** * Add tags to the specified device */ public static function addTags($reg_id, $tags = []) { $response = self::getInstance()->device()->addTags($reg_id, $tags); if ($response['http_code'] == 200) { return true; } return false; } /** * Clear the specified device label */ public static function clearTags($reg_id) { $response = self::getInstance()->device()->clearTags($reg_id); if ($response['http_code'] == 200) { return true; } return false; } /** * Clear the specified device label */ public static function removeTags($reg_id, $tags = []) { $response = self::getInstance()->device()->removeTags($reg_id, $tags); if ($response['http_code'] == 200) { return true; } return false; } /** * Updates the specified device alias */ public static function updateAlias($reg_id, $alias) { $response = self::getInstance()->device()->updateAlias($reg_id, $alias); if ($response['http_code'] == 200) { return true; } return false; } }
After creation, we can call JPushService :: pushNotify () in the project to push news.
JPushService::pushNotify([ //title 'Title' => 'test', //content 'Content' => 'test', // device identification, with related equipment 'reg_id' => 'xxxxxxxxxxx', //no 'extras' => [ 'key' => 'value', ], // push type 'type' => JPushService::PUSH_TYPE_REG_ID, ]);
Andrews reg_id is distal or after obtaining the IOS, PHP rear pass, then the user with the associated savings.
Note, reg_id is associated with the device, with APP on a device, when different user login, reg_id is the same, this will cause a problem.
A user registration after the APP, and switched to the user B, user B receives a message that is sent to user A, which can cause confusion message.
Solution:
When the message is transmitted via an alias, a device can be bound as an alias, when a user logs A, bound to the reg_id user_a alias, or switch the user quit, put alias blanking.
User B then landing, put reg_id bound to user_b. When the push message, the message will be pushed through the alias.
Binding alias (recommended user ID to distinguish different aliases):
JPushService::updateAlias($user->jpush_reg_id, 'user_id_' . $user->id);
Blank alias:
JPushService::updateAlias($user->jpush_reg_id, '');
Send by Alias:
JPushService::pushNotify([ 'Title' => 'test', 'Content' => 'test', 'alias' => 'user_id_' . $message->receive_id, 'extras' => $extras, 'type' => JPushService::PUSH_TYPE_ALIAS, ]);