Author: Huang Zhicheng (Xiao Huang)

Author’s Blog: Blog address

The push requirements are more complex with the new version. Need to access a professional push. Previously has been based on APP IM to achieve.

At present, the mainstream push platforms on the market include Aurora, Umeng, carrier pigeon, etc..

Finally, the aurora push platform is selected through research

What is a push platform? Here’s a picture for example

Our business server requests the interface provided by aurora Platform. Ask them, and the Aurora platform pushes the information to our users.

Push has been mentioned, some novice friends may still be very confused. What is push?

In most HTTP requests, the client sends the request to the server and the server responds with the data. However, in some cases, we need to actively respond to the data to the client.

The process of actively responding to data is push.

So how does push work?

It’s simply a long connection. Normal HTTP is a short connection that is closed after the data is received. The long connection stays connected. Since it’s always connected, we can find that connection and push it to him.

All right, back to business. Let’s continue talking about how to access the aurora push.

Just a couple of concepts

  • Registration ID

After the client successfully initializes JPush, the JPush server will assign a Registration ID as the identity of the device (Registration ID is different for different apps on the same phone). Developers can push to a single device by specifying a specific Registration ID.

  • The alias

Only one alias can be specified for each user. You are advised to use different aliases for different users in the same application. In this way, users are identified as uniquely as possible by aliases.

  • The label

The purpose of labeling users who have installed applications is to facilitate developers to batch deliver Push messages according to the labels. Multiple labels can be assigned to each user.

Let’s try to understand the relationship through a picture

First of all, Registration ID is unique. We can give this Id an alias, that is, a note. This alias can be set to be the same as the user account for our platform. This is convenient for us to push.

Of course, pushing them one by one is cumbersome. We can Tag these users. For example, users in Beijing are placed under the Beijing Tag. Single users are placed under the single Tag. We push with a Tag, we can achieve a group effect.

Once you understand the concept, it should be clear that you need to bind JPush registered users to developer App users at this point.

This binding has two basic ideas:

  • Save the binding to the JPush server side

  • Save the binding to the developer application server

The first is the aliases and tags mentioned earlier. It is up to the client to set the alias.

The client developer will call the setAlias or setTags API to set up the relationship

The SDK saves this relationship setting to JPush Server

When a message is pushed on the server side, specify that it is pushed to a previously set alias or label.

The second is more troublesome. The client passes the Registration ID to the server, which handles the correspondence.

We use the first one here, where the alias or title is set by the client.

JPush provides four message forms: notifications, custom messages, rich media, and local notifications.

Here we focus on notification messages. Once you understand the first one, the rest are easy to follow when you look at the documentation.

  • notice

Push Notification, or Push Notification, is a Notification that appears on the status bar of your phone. Notifications are used to remind users of news content, promotional activities, product information, version update, and order status

Here is the address of the official website document, the following content is based on this document.

Document address: Aurora push

So let’s get a couple of ideas

  • Platform: push platform

JPush currently supports push on Android, iOS and Windows Phone. Its keywords are: “Android “, “ios”,” Winphone “.

If the target platform is iOS, you need to set the push environment through the apNS_production field in options. True indicates that the production environment is pushed. False indicates that the development environment is pushed. If this parameter is not specified, the production environment is pushed

Push to all platforms:

{ "platform" : "all" }
Copy the code

Specify a specific push platform:

{ "platform" : ["android"."ios"]}Copy the code

Typically, android and ios will do the trick. If the product has a Winphone version can also choose to push to all platforms, there is not too much problem.

  • Audience: Push the target

The push device object represents the list of devices to which a push can be pushed. JPush provides a variety of ways to identify push device objects, such as alias, tag, registration ID, clustering, broadcast, and so on.

See the documentation for specific parameters.

We are using the encapsulation push method, and we need to set the configuration of Android and ios respectively. I’ll explain this in my encapsulated methods.

The first step is to integrate the SDK with Composer

Add jpush dependencies to the composer. Json file in your project:

"require": {
    "jpush/jpush": "^ 3.5"
Copy the code

perform composer installInstall.

You can also download it directly. Download the address to find in the documentation

Once installed, Composer can be referenced directly through the namespace.

Here is a push method that I have simply encapsulated.

<? PHP /** * Created by PhpStorm. * User: huangzhicheng * Date: 2018/8/28 * Time: 3:12 PM */ Namespace data\tools; use data\tools\config\Output; use JPush\Client as Client; Class JPush {/** * send aurora push message via alias * @param$title// title * @param$content// Content * @param$alias// alias * @param array$params// Extended field * @param string$ios_badge// * @param array$platform// Push device * @returnArray | bool * @ author huangzhicheng August 29, 2018 * / public staticfunction pushMessageByAlias ($title.$content.$alias.$params= [].$ios_badge = '0'.$platform = ['ios'.'android'])

        if(! is_array ($alias)) return false;
        $jpush_conf= Output::getJPushKey (); // Obtain the configuration information app_key and master_secret$app_key = $jpush_conf[ 'app_key' ];
        $master_secret = $jpush_conf[ 'master_secret']; Try {// initializes$client = new Client($app_key.$master_secret);

            $result = $client->push ()
                ->setPlatform ($platform)
                ->addAlias ($alias)
                ->iosNotification (
                    $content['sound'= >'1'.'badge' => (int)$ios_badge.'content-available'= >true.'category'= >'jiguang'.'extras'= >$params,
                ->androidNotification ($content['title'= >$title, / /'build_id'= > 2,'extras'= >$params,
                ->options ([
                    'sendno'= > 100,'time_to_live'= > 86400,'apns_production'= >true, // Select the ios certificate to push. True indicates that the production environment is pushed. False indicates that the development environment is pushed'big_push_duration' => 10,
                ->send ();
            return $result;
        } catch (\Exception $e) {// Write error log // here depends on its own business}}}Copy the code

You can see that in the code. I used iosNotification and androidNotification methods to set push Settings for the two devices.

// Ios notification configuration item -> Ios Notification ($content, // Push content ['sound'= >'1'// Is there a sound'badge' => (int)$ios_badge// Display the number of angular markers'content-available'= >true, // Go to the documentation to see the specific use, usually set totrueOr 1'category'= >'jiguang'// Go to the documentation here as well'extras'= >$params, // The extension field depends on your business scenario.])Copy the code

Android’s push is similar. For more specific configuration items, see the documentation.

Finally, the wrapped code is called

const PUSH_TYPE = [
        'push_new_info'= >'1'.'push_visitor_alert'= >'2'


const APP_NAME = "* * * *";
public static function pushNewInfoNotice ($uids.$title.$url.$txt.$type = '1')

    $ext = [
        'push_type' => strval (self::PUSH_TYPE[ 'push_new_info' ]),
        'info_type' => strval ($type),//1- Information,2- project'title' => empty($title)? self::APP_NAME :$title.'content'= >$txt.'redirect_url'= >$url

    $res = JPush::pushMessageByAlias ($title.$txt.$uids.$ext);
    return $res;
Copy the code

All we have to do is call this method and push it.

$ext is the message format we defined with the client. They perform different operations based on push_type.

One last thing. Is that we developed and produced an aurora application. So you can’t just send broadcast messages in the development environment.

So how do you distinguish between a production environment and a development environment?

Here is the official advice

1. Use the same Appkey and package name, and use registrationID, Tag, and Alias to push the test machine

2. If you must test your broadcast feed, create a test app on the official website with a different Appkey and package name

3. If you still need the same package name and test your broadcast push, create an aurora account on the official website and create a test application with the same package name.

This article is recorded here, there will be something to supplement in the future update.

Completed: 00:22:32, 01 Sep 2018