preface

Tell me the original intention of writing this article. I once made an APP that used message push, and recently this project used message push again. However, due to the gap of more than 4 months between the two projects, I forgot the integration of push and how to use it, so I went to look at the source code of the previous project and some blogs related to push at that time, looking for those clues. Suddenly feel this is a waste of time, why don’t you write an article to summarize it, later will certainly use push, the next time to see this article, the idea should be more intuitive. And then on second thought, well, yes, do it!

PS: This article is based on Cordova. If you don't have Cordova, please install it first.

The body of the

1. Cordova creates the APP project

Cordova Create myAPP com.muzi. Test Cordova Platforms Add Android With file name myAPP,APP package name com.muzi. Test Cordova Platforms Add Android Some of the Cordova details of adding a project you created to the Android platform can be seen in my previous blog post that flies over here

2. Request notificationsAPP_KEY(Aurora push, which I always use for my notifications)

2.1 the applicationAPP_KEYNeed to be in aThe aurora pushOfficial website registration, the specific process is not said.

2.2 Enter the APP Application Creation Center

https://www.jiguang.cn/accoun…

The package name entered here (which cannot be modified after being saved) is the package name of the Cordova project created. The package name must be the same in both places, otherwise it cannot be pushed. After saving, you can download the demo integrated with Aurora push, or you can download the packaged test app for pushing test. (As the company’s products are special and only for Android users, I am not so familiar with the use of push on other platforms. Here, I will only introduce the use of the Android end)

3. Install the Aurora Push pluginjpush-phonegap-plugin

Go to the root of the project file that Cordova created at the beginning, and install the plug-in. There are three ways to do this:

  • Installation via Cordova Plugins requires Cordova CLI 5.0+ :

    cordova plugin add jpush-phonegap-plugin --variable APP_KEY=your_jpush_appkey
  • Or install directly from the URL:

    cordova plugin add https://github.com/jpush/jpush-phonegap-plugin.git --variable APP_KEY=your_jpush_appkey
  • Or download to local installation:

    cordova plugin add Your_Plugin_Path --variable APP_KEY=your_jpush_appkey

When installing plug-ins, be aware of the versions of Jpush and jcore, some versions cannot initialize push

Your_jpush_appkey: The APP_KEY generated when creating the app with Aurora Push

4. The coding

4.1 the initializationJPush

When the App starts, indevicereadyCalled in the event callbackwindow.JPush.init()Method initialization can be calledwindow.JPush.stopPush()The end of the push

Document. The addEventListener (" deviceready ", function () {/ / plug-in initialization window. JPush. The init (); }, false );

4.2 Pushing mode of aurora push

  • Broadcast (All)
  • Device Tag — can be single or multiple
  • Device Alias — can be single or multiple
  • Registration ID(one of them)
  • Users push in groups

4.3 Obtain the device returning from the background of Aurora Pushregistration ID

thisregistration IDIs unique, the project after receiving this ID can be stored, and then sent to the server, server can be based on this IDregistration IDSend push messages to the specified client (mobile phone)

window.JPush.getRegistrationID(function(rId) {       
        console.log(rId);
      });

4.4 set upEquipment Tag (TAG)

window.JPush.setTags({ sequence: 1, tags: ['tag1', 'tag2']}, (result) = bb0 {var sequence = result.sequence var tags = result.tags // array type}, (error) => { var sequence = error.sequence var errorCode = error.code })

Parameter specification sequence: number. The user-defined sequence number of an operation, returned with the result of the operation, to identify the uniqueness of an operation. Tags: Array

When this method is called, the Settings are sent to the server of AuroraTag, will trigger the corresponding callback function

4.5 set upDevice Alias

window.JPush.setAlias({ sequence: 1, alias: 'your_alias' },
  (result) => {
    var sequence = result.sequence
    var alias = result.alias
  }, (error) => {
    var sequence = error.sequence
    var errorCode = error.code
  })

Parameter specification sequence: number. The user-defined sequence number of an operation, returned with the result of the operation, to identify the uniqueness of an operation. tags: string

  • Each call to set a valid alias overrides the previous Settings.
  • Valid aliases: letters (case-sensitive), numbers, underscores, Chinese characters, special @! # $& * + =. |.
  • Limitation: Alias name length is limited to 40 bytes (length is determined in UTF-8 encoding).

4.6 Get the content of the push message

This method is fired when the client receives a push message and returns onejsonStructured data packets from which the required data can be fetched

/ / receives a push message callback window. Plugins. JPushPlugin. ReceiveNotificationInAndroidCallback = function (data) {try {the console. The log ( "JPushPlugin:receiveNotificationInAndroidCallback:", data ); } catch (exception) { console.log("JPushPlugin:pushCallback ", exception); }};

4.7 Click the notification bar’s push message

When you click on a message in the notification bar, it triggers this method and returns a message containing the specific push contentjsonData, according to the contents, to do the corresponding event processing, such as the need to click notification bar message, jump to the corresponding page for processing

// Click the notification bar callback, Here to write specific logic window. Plugins. JPushPlugin. OpenNotificationInAndroidCallback = function (data) {try {/ / location. The href = 'index.html'; console.log(data); } catch (error) {} };

4.8 More push related APIs can be viewedPhoneGap plugin website

4.9 Push testing

After initializing Aurora push window.jpush.init (), registration ID will be generated when the APP runs for the first time, or tags and Alias will be set successfully. You can test the message push on the official website of Aurora push, as shown in the figure below:

When the initial message is pushed, the push message will be received on the phone a short time later (sometimes there may be a delay of a few seconds to more than 10 seconds, generally it is a second arrival)

nonsense

That’s what I usejpush-phonegap-pluginPlug-in push some experience, hope to be able to help the need of friends. If there are mistakes or deficiencies, you are welcome to point out, if you feel satisfied, welcome thumb up and favorites.

reference

  • PhoneGap plug-in documentation

  • Aurora push documents