[ 極光文檔 ]html
進去以後,建立一個應用,已有應用可跳過
前端
建立完成
java
進入應用,看到AppKey和MasterSecret,這兩個參數在sdk集成的時候會用到
android
<dependency> <groupId>cn.jpush.api</groupId> <artifactId>jpush-client</artifactId> <version>3.2.17</version> </dependency> <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jiguang-common</artifactId> <version>1.0.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>
com.XXX.jdpush //包名
jdpush //類名ios
接下來是在jdpush類的操做
其實,極光已經給咱們集成好了,只需調用推送方法便可web
1 //極光推送>>Android 2 //Map<String, String> parm是我本身傳過來的參數,同窗們能夠自定義參數 3 public static void jpushAndroid(Map<String, String> parm) { 4 // 設置好帳號的app_key和masterSecret 5 String appKey = "**************"; 6 String masterSecret = "************"; 7 //建立JPushClient(極光推送的實例) 8 JPushClient jpushClient = new JPushClient(masterSecret, appKey); 9 //推送的關鍵,構造一個payload 10 PushPayload payload = PushPayload.newBuilder() 11 .setPlatform(Platform.android())//指定android平臺的用戶 12 .setAudience(Audience.all())//你項目中的全部用戶 13 .setNotification(Notification.android(parm.get("msg"), "這是title", parm)) 14 //發送內容,這裏不要盲目複製粘貼,這裏是我從controller層中拿過來的參數) 15 .setOptions(Options.newBuilder().setApnsProduction(false).build()) 16 //這裏是指定開發環境,不用設置也不要緊 17 .setMessage(Message.content(parm.get("msg")))//自定義信息 18 .build(); 19 20 try { 21 PushResult pu = jpushClient.sendPush(payload); 22 } catch (APIConnectionException e) { 23 e.printStackTrace(); 24 } catch (APIRequestException e) { 25 e.printStackTrace(); 26 } 27 }
1 //極光推送>>ios 2 //Map<String, String> parm是我本身傳過來的參數,同窗們能夠自定義參數 3 public static void jpushIOS(Map<String, String> parm) { 4 // 設置好帳號的app_key和masterSecret是必須的 5 String appKey = "*********************"; 6 String masterSecret = "**********************"; 7 8 //建立JPushClient 9 JPushClient jpushClient = new JPushClient(masterSecret, appKey); 10 PushPayload payload = PushPayload.newBuilder() 11 .setPlatform(Platform.ios())//ios平臺的用戶 12 .setAudience(Audience.all())//全部用戶 13 .setNotification(Notification.newBuilder() 14 .addPlatformNotification(IosNotification.newBuilder() 15 .setAlert(parm.get("msg")) 16 .setBadge(+1) 17 .setSound("happy")//這裏是設置提示音(更多能夠去官網看看) 18 .addExtras(parm) 19 .build()) 20 .build()) 21 .setOptions(Options.newBuilder().setApnsProduction(false).build()) 22 .setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定義信息 23 .build(); 24 25 try { 26 PushResult pu = jpushClient.sendPush(payload); 27 28 } catch (APIConnectionException e) { 29 e.printStackTrace(); 30 } catch (APIRequestException e) { 31 e.printStackTrace(); 32 } 33 }
.setOptions(Options.newBuilder().setApnsProduction(false).build())api
[ 極光文檔Push API v3 ] markdown
1 //這是個人添加文章的接口方法 2 //如今我要添加一篇文章後,進行推送 3 public Map<String, Object> addArticle() throws Exception { 4 Map<String, Object> result = new HashMap<String, Object>(); 5 .... 6 //省略文章添加的步驟 7 8 //從前端頁面傳個參數過來判斷是否推送 9 if((infoMap.get("is_push").toString()).trim().equals("1")){ 10 //設置推送參數 11 //這裏同窗們就能夠自定義推送參數了 12 Map<String, String> parm =new HashMap<String, String>(); 13 //這是個人文章id 14 parm.put("id",(""+id).trim()); 15 //文章標題 16 parm.put("Atitle",(String) infoMap.get("Atitle") ); 17 //設置提示信息,內容是文章標題 18 parm.put("msg",(String) infoMap.get("Atitle") ); 19 //調用ios的 20 Jdpush.jpushIOS(parm); 21 //而後調用安卓的 22 Jdpush.jpushAndroid(parm); 23 } 24 return result; 25 }
推送後報錯了app
Your request params is invalid. Please check them according to error message.
Error response from JPush server. Should review and fix it.
//而後狀態碼是
INFO 2017-07-14 11:08:04,208 com.weiwend.jdpush.Jdpush.http-bio-8080-exec-7 HTTP Status: 400
INFO 2017-07-14 11:08:04,208 com.weiwend.jdpush.Jdpush.http-bio-8080-exec-7 Error Code: 1011
INFO 2017-07-14 11:08:04,208 com.weiwend.jdpush.Jdpush.http-bio-8080-exec-7 Error Message: cannot find user by this audience
INFO 2017-07-14 11:08:04,208 com.weiwend.jdpush.Jdpush.http-bio-8080-exec-7 Msg ID: 4241206476框架
這就很尷尬了
若是你的項目中有用戶,
可是,是你的參數(setAudience)設置錯了,也會也會報錯.
PS:推送全部平臺
注意事項:安卓直接忽略開發環境和生產環境,可是ios對環境敏感哦~~~~
(就是安卓不論生產開發都會推送,可是ios必須指定才能推,默認生產模式)
1 //極光推送>>All全部平臺 2 public static void jpushAll(Map<String, String> parm) { 3 // 設置好帳號的ACCESS_KEY和SECRET_KEY 4 5 //建立JPushClient 6 JPushClient jpushClient = new JPushClient(masterSecret, appKey); 7 //建立option 8 9 PushPayload payload = PushPayload.newBuilder() 10 .setPlatform(Platform.all()) //全部平臺的用戶 11 .setAudience(Audience.registrationId(parm.get("RegId")))//registrationId指定用戶 12 .setNotification(Notification.newBuilder() 13 .addPlatformNotification(IosNotification.newBuilder() 14 .setAlert(parm.get("msg")) 15 .setBadge(+1) 16 .setSound("happy") 17 .addExtras(parm) 18 .build()) 19 .addPlatformNotification(AndroidNotification.newBuilder() 20 .addExtras(parm) 21 .setAlert(parm.get("msg")) 22 .build()) 23 .build()) 24 .setOptions(Options.newBuilder().setApnsProduction(true).build())//指定開發環境 25 .setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定義信息 26 .build(); 27 try { 28 PushResult pu = jpushClient.sendPush(payload); 29 } catch (APIConnectionException e) { 30 e.printStackTrace(); 31 } catch (APIRequestException e) { 32 e.printStackTrace(); 33 } 34 }