java SDK server push - push Aurora (JPush)

URL: https: //blog.csdn.net/duyusean/article/details/86581475

 

      APP message push more and more common in applications to record it for pushing way used in the project, it does not own for Andriod native push mechanism, a simple way is to use a third-party push push services a way that by embedding SDK use the push service provided by third parties, the mainstream has Baidu cloud push, push Aurora, Friends of the Union, a push, Amazon and so on. This post only describes the use of Aurora push mode.
        If you are a novice, I suggest you first read this blog, then go to the official website, so maybe get started faster.
Key steps: 1. Aurora's official website to register an account, create an application, obtain AppKey, Master Secret (official website: HTTPS: //www.jiguang.cn/)
2. import-dependent projects in java jar package, if you It is a maven project, you can add a dependency in the pom file.
3. Create a JPushUtil class, the code at the bottom.
 
Detailed steps 1. Log on Aurora's official website, register an account 2. Select the "developer services" - "Aurora push" - "Immediate Experience"

 
3. Create an application

4. Click on the newly created application, and get AppKey Secret Master 


5. In java project import dependencies, (refer to the official website: https: //docs.jiguang.cn/jpush/server/sdk/java_sdk/#_2) maven way will depend on the conditions below the maven pom.xml file into your project in.
<dependency> <groupId> cn.jpush.api < / groupId> <artifactId> jpush-client </ artifactId> <version> 3.3.9 </ version> </ dependency>
Dependencies slf4j / log4j (Logger) gson ( Google JSON Utils) wherein slf4j may work with logback, log4j, commons-logging and other logging framework may be configured to use according to your needs.
If you use Maven to build the project, you need to increase your pom.xml in the project:
    <dependency>        <groupId>cn.jpush.api</groupId>        <artifactId>jiguang-common</artifactId>        <version>1.1.3</version>    </dependency>    <dependency>        <groupId>io.netty</groupId>        <artifactId>netty-all</artifactId>        <version>4.1.6.Final</version>        <scope>compile</scope>    </dependency>    <dependency>        <groupId>com.google.code.gson</groupId>        <artifactId>gson</artifactId>        <version>2.3</version>    </dependency>    <dependency>        <groupId>org.slf4j</groupId>        <artifactId>slf4j-api</artifactId>        <version>1.7.7</version>    </dependency>     <!-- For log4j -->    <dependency> <groupId> org.slf4j </ groupId> <artifactId> slf4j-log4j12 </ artifactId> <version> 1.7.7 </ version> </ dependency> <dependency> <groupId> log4j </ groupId> <artifactId> log4j </ artifactId> <version> 1.2.17 </ version> </ dependency> If you do not use Maven to build the project, the next project libs / directory has dependent jar can be copied to your project go.
6. Create JPushUtil class, using the method given in ios and Android, and methods are applicable to both, as follows: / * ** Aurora push tools * Created by DUYU on 2019/1/17. * / public class JPushUtil {// we set up the account app_key and masterSecret are necessary private static String APP_KEY = "fcc67 ************* ba80d76"; private static String mASTER_SECRET = "a4c45 ** *********** cc0 "; // Aurora push >> Android // Map <String, String> parm is my own pass over the parameters, you can customize the parameters public static void jpushAndroid (Map <String , String> parm) {// create jPushClient (Aurora pushed example) jPushClient jpushClient = new jPushClient (mASTER_SECRET, APP_KEY); // key pushed, a configuration payload PushPayload payload = PushPayload.newBuilder () .setPlatform (Platform.android ()) // designated android platform users .setAudience (Audience.all ()) // all users of your project // .setAudience (Audience.alias (parm.get ( "alias"))) // set the alias send,Single, point to point transmission mode is provided by tag //.setAudience(Audience.tag("tag1"))//, equivalent mass // .setAudience (Audience.registrationId (parm.get ( "id"))) // registrationId specified user .setNotification (Notification.android (parm.get ( "msg"), parm.get ( "title"), parm)) // send content .setOptions (Options.newBuilder (). setApnsProduction (true) .setTimeToLive (7200) .build ()) // apnProduction specify true development environment for the production of false mode to test mode (android does not distinguish mode, ios distinguish mode) without setting it does not matter // TimeToLive two-hour buffer time .setMessage (Message. content (parm.get ( "msg"))) // custom information .build (); try {PushResult pu = jpushClient.sendPush (payload); System.out.println (pu.toString ());} catch ( APIConnectionException e) {e.printStackTrace ();} Catch (APIRequestException e) {e.printStackTrace ();}} // push Aurora >> ios // Map <String, String> parm pass over my own parameters, custom parameters public static void jpushIOS (Map < String, String> parm) {// create JPushClient JPushClient jpushClient = new JPushClient (mASTER_SECRET, APP_KEY); PushPayload payload = PushPayload.newBuilder () .setPlatform (Platform.ios ()) // ios platform for user .setAudience (Audience. all ()) // all users //.setAudience(Audience.registrationId(parm.get("id")))//registrationId specified user .setNotification (Notification.newBuilder () .addPlatformNotification (IosNotification.newBuilder () .setAlert (parm.get ( "msg")).setBadge (+1) .setSound ( "happy") // here is to set the tone (more can look Quguan network) .addExtras (parm) .build ()) .build ()) .setOptions (Options.newBuilder ( ) .setApnsProduction (false) .build ()) .setMessage (Message.newBuilder (). setMsgContent (parm.get ( "msg")). addExtras (parm) .build ()) // custom information .build () ; try {PushResult pu = jpushClient.sendPush (payload); System.out.println (pu.toString ());} catch (APIConnectionException e) {e.printStackTrace ();} catch (APIRequestException e) {e.printStackTrace ( );}} // Aurora push all platforms >> All public static void jpushAll (Map <String, String>parm) {// create JPushClient JPushClient jpushClient = new JPushClient (MASTER_SECRET, APP_KEY); // create option PushPayload payload = PushPayload.newBuilder () .setPlatform (Platform.all ()) // all platform users .setAudience (Audience. registrationId (parm.get ( "id"))) // registrationId specified user .setNotification (Notification.newBuilder () .addPlatformNotification (IosNotification.newBuilder () // transmit ios .setAlert (parm.get ( "msg")) / / message body .setBadge (+1) .setSound ( "happy") // ios tone .addExtras (parm) // additional parameters .build ()) .addPlatformNotification (AndroidNotification.newBuilder () // transmit android .addExtras (parm) // additional parameter .setAlert (parm.get ( "msg")) // message body .build ()) .build ()) .setOptions (Options.newBuilder () .setApnsProduction (true) .build ()) // specify true development environment for the production of false mode to test mode (android does not distinguish mode, ios distinguish mode) .setMessage (Message.newBuilder (). setMsgContent (parm.get ( "msg . ")) addExtras (parm) .build ()) // custom information .build (); try {PushResult pu = jpushClient.sendPush (payload); System.out.println (pu.toString ());} catch (APIConnectionException e) {e.printStackTrace ();} catch (APIRequestException e) {e.printStackTrace ();}}} 7.Test DEMO class public class demo {public static void main (String [] args) {// // parameter setting push push herein can customize the parameters Map <String, String> parm = new HashMap <String, String> (); // set the message, the content is the title of the article parm.put ( "msg", "receive, please contact the sender"); parm.put ( "title", "here is the title"); parm.put ( "alias" , "abc"); JPushUtil.jpushAndroid (parm);}} test results:


8. Aurora backstage view You can view the history of the push in Aurora background Aurora log in the background, click on the below application "push"


Select Api


click on the "Operations" View Details




9. :( parameter information from official documents https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#platform)
Platform: Push JPush platform currently supports push Android, iOS, Windows Phone three platforms. Its keywords are: "android", "ios" , "winphone".
If the target platform for the iOS platform, we need to set the environment to push through apns_production field options when you push Notification. True to push production environments, False pledged to push development environment; if not specified, compared with push production environment; can only be pushed to one environment.
 
Push all platforms:
{ "Platform": "All" target specific push internet}:
{ "Platform": [ "Android", "iOS"]} 
Audience: pushing devices push the target object represents a push which can be pushed to the Device List. Push to confirm device object, JPush offers a variety of ways, such as: aliases, labels, registration ID, clustering, broadcasting.
If you want to send all broadcast (all devices), directly fill out the "all".
Target device selection mode to push outside broadcasting, are summarized as follows:
Keyword Type Meaning Remarks tagJSON Array label OR array. OR relationship between a plurality of tags, i.e. taking the union. Label to large-scale device attributes, user attribute grouping. A push up to 20. Effective tag: the letters (case sensitive), numbers, underscores, Chinese characters, special characters, @ # $ & * + = | ¥!.. Restrictions: a tag of each length is limited to 40 bytes. (Length determination requires the use of UTF-8 encoding) tag_andJSON Array tags AND array. Is an AND relationship between a plurality of tags, i.e., on the intersection. Note the tag distinction. A push up to 20. tag_notJSON Array label NOT array. Among the plurality of tags, labels and to take multiple sets, and then take the results of complement. A push up to 20. aliasJSON Array alias array. It is OR relationship between a plurality of aliases, i.e. taking the union. Alias to identify a user. A device can only bind an alias, but multiple devices can be bound to the same alias. A push up to 1000. Effective alias components:! The letters (case sensitive), numbers, underscores, Chinese characters, special characters, @ # $ & * + = | ¥. Restrictions: Each alias limits the length of 40 bytes. (Length determination requires the use of UTF-8 encoding) registration_idJSON Array Register ID array. It is OR relationship between the plurality of registration ID, a get and set. Equipment Identity. A push up to 1000. The client SDK is available to the integrated value. segmentJSON Array user grouping user grouping ID ID in page creation. It is defined as an array, but you can only push a limit. Currently you can only push a limit is. A abtestJSON ArrayA / B Test ID in page creation / ID B testing. Is defined as an array, but there is a limit can only push a. You can only push a current restrictions. 
Several types of the above at least need to have one. If the value of the array length is zero, indicating that the type is not present.
This types can co-exist, a number of implicit relationship is AND, that is to take the results of several types of intersection.
E.g:
"audience": { "tag" : [ "tag1", "tag2"], "tag_and": [ "tag3", "tag4"], "tag_not": [ "tag5", "tag6"]}
first calculate " tag "results tag1 field or tag2 = a;
recalculated" tag_and "field results tag3 and tag4 = B;
recalculated" tag_not "field is the result of non-(Tag5 or tag6) = C
" Audience "final result is a and B and C.
notification: notification
 
message: custom message
 
options: push optional parameter options.
Currently it contains several options as follows:
Option key type ID Meaning Description sendnoint purely used as an optional push API call identifier, as returned by the API returns, to facilitate matching caller API request returns. The value 0 indicates no messageid sendno, so the field is in the range of non-int 0. When time_to_liveint optional length (second) push this message remains offline user is not online, offline for how long the message for the user, in order to push it on the line again. Default 86400 (1 day), up to 10 days. Set to 0 to not retain the offline messages, push only current online users can receive. This field iOS Notification message is invalid. override_msg_idlong optional message ID prior to be covered if the current push a push to cover, before completing a push msg_id herein will produce coverage effect, namely: 1) the message is received offline msg_id contents covering; 2 ) even if the end user has received msg_id Android, if the notification bar has not been cleared, the new message will overwrite the contents of this notice before; time overlay function works is: 1 day. If it does not exist within the coverage msg_id a specified time, an error is returned 1003, indicating a valid message is not overwriting the current message is not pushed; This field is only valid Android. apns_productionboolean optional APNs whether a production environment True to push production environments, False pledged to push development environment; if not specified, compared with push production environment. But note, JPush server SDK default setting is push "development environment." This field is only valid for iOS Notification. Alternatively update notification apns_collapse_idstring iOS notification if the identifier of the new APNs matched current notification has the same notification apns-collapse-id field, the content will be updated with new notification it, and putting it in the center of the first notification. collapse id not be longer than 64 bytes. Rapidly may be selected big_push_durationint (minutes), also known as a slow push push, push as quickly as possible to the original speed, lower down, a given n minutes, to uniformly push the push certain user. The maximum is 1400. The constant speed is not set is not pushed.
---------------- Disclaimer: This article is the original article CSDN bloggers "Du _ demon", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. Original link: https: //blog.csdn.net/duyusean/article/details/86581475

Guess you like

Origin www.cnblogs.com/zxtceq/p/11966818.html