1. Add Poom dependency:
<dependency> <groupId>cn.jpush.api</groupId> <artifactid>jiguang-common</artifact id> <version>1.1.1</version> </dependency> <dependency> <GROUPID>IO.N Etty</groupid> <artifactId>netty-all</artifactId> <VERSION>4.1.6.FINAL</VERSION&G T <scope>compile</scope> </dependency> <dependency> <groupid>com.google.code.gson </groupId> <artifactId>gson</artifactId> <version>2.3</version> </depend ency> <dependency> <groupId>org.slf4j</groupId> <artifactid>slf4j-api</arti Factid> <version>1.7.7</version> </dependency> <!--for log4j and <dependenc y> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> < ; version>1.7.7</version> </dependency> <dependency> <groupId>log4j</groupId> <ar Tifactid>log4j</artifactid> <version>1.2.17</version> </dependency>
2. Create the tool class as follows (red is where you need to be aware):
PackageCom.demo.jiguangpush;Importcn.jiguang.common.resp.APIConnectionException;Importcn.jiguang.common.resp.APIRequestException;Importcn.jpush.api.JPushClient;ImportCn.jpush.api.push.PushResult;ImportCn.jpush.api.push.model.Message;Importcn.jpush.api.push.model.Options;ImportCn.jpush.api.push.model.Platform;ImportCn.jpush.api.push.model.PushPayload;Importcn.jpush.api.push.model.audience.Audience;Importcn.jpush.api.push.model.notification.IosNotification;Importcn.jpush.api.push.model.notification.Notification;ImportJava.util.Map; Public classJgpush {//set up App_key and Mastersecret for your account /*private static String AppKey = "3C8473F025736EA1BFA3A1C1"; private static String Mastersecret = "ee97e1c77b2e3710da671886";*/ //mine is the database configuration//Aurora Push >>android//map<string, string> parm is my own. Parameters that can be customized Public StaticString jpushandroid (map<string, string>parm) { //Create JpushclientJpushclient jpushclient =NewJpushclient (Parm.get ("Mastersecret"), Parm.get ("AppKey"));//Note The order of the parameters, or you will get an error (Mastersecret, AppKey)//push the key, construct a payloadPushpayload payload =Pushpayload.newbuilder (). Setplatform (Platform.android ())//Specify the users of the Android platform. Setaudience (Audience.all ())//all users in your project. Setnotification (Notification.android (Parm.get ("Pushcontent"), Parm.get ("Pushtitle"), parm))//send the content, here do not blindly copy and paste, here is I took from the controller layer parameters .. SetOptions (Options.newbuilder (). Setapnsproduction (false). Build ())//here is the specified development environment, without setting it's okay, Android has only one. Setmessage (Message.content (Parm.get ("Pushcontent")))//Custom Information. Build (); Try{pushresult Pu=Jpushclient.sendpush (payload); } Catch(apiconnectionexception e) {e.printstacktrace (); } Catch(apirequestexception e) {e.printstacktrace (); returnE.geterrormessage ();//Throw failure error message } return"Success"; } //Aurora Push >>ios//map<string, string> parm is my own. Parameters that can be customized Public StaticString Jpushios (map<string, string>parm) { //Create JpushclientJpushclient jpushclient =NewJpushclient (Parm.get ("Mastersecret"), Parm.get ("AppKey")); Pushpayload Payload=Pushpayload.newbuilder (). Setplatform (Platform.ios ())//users of the iOS platform. Setaudience (Audience.all ())//All Users. Setnotification (Notification.newbuilder (). Addplatformnotification (Iosnotifica Tion.newbuilder (). Setalert (Parm.get ("Pushcontent"))//. Setbadge (+1)//If this parameter is set, the app-side corner Bidding clubs is this parameter, without this parameter, the Aurora will automatically implement the +1 function. Setsound ("Default"). Addextras (Parm). Build ()). Bui LD ()). SetOptions (Options.newbuilder (). Setapnsproduction (true). Build ())//False for the development environment; True for production (iOS by two environments, need to differentiate). Setmessage (Message.newbuilder (). Setmsgcontent (Parm.get ("Pushcontent")). Addextras (Parm). Build ())//Custom Information. Build (); Try{pushresult Pu=Jpushclient.sendpush (payload); } Catch(apiconnectionexception e) {e.printstacktrace (); } Catch(apirequestexception e) {e.printstacktrace (); returnE.geterrormessage (); } return"Success"; }}
Encountered pit: need to implement +1 functions on the app side (push a message, no view, and push one, at this point, the corner should be 2) The final result is the angle mark has been 1, and then found the reason is the parameter setting problem:
Aurora Push Official documents: https://docs.jiguang.cn/jpush/server/sdk/java_sdk/
Aurora Push Reference article: 75117323
Java Aurora Push Record