java-极光推送教程
技术交流QQ群:422167709 想学习的朋友可以进入,各路IT大牛帮忙解决问题
一、准备工作:
1.访问极光推送官网:https://www.jiguang.cn/accounts/login/form
2.注册登陆,拿到appKey和masterSecret
3.创建一个应用,安卓需要自己配置自己的设备ios一样的,服务端加上访问前缀和前端相对应即可。
二、工具类直接测试推送是否成功。
1 package com.hykj.common; 2 3 import cn.jiguang.common.resp.APIConnectionException; 4 import cn.jiguang.common.resp.APIRequestException; 5 import cn.jpush.api.JPushClient; 6 //import cn.jpush.api.common.resp.APIConnectionException; 7 //import cn.jpush.api.common.resp.APIRequestException; 8 import cn.jpush.api.push.PushResult; 9 import cn.jpush.api.push.model.Options; 10 import cn.jpush.api.push.model.Platform; 11 import cn.jpush.api.push.model.PushPayload; 12 import cn.jpush.api.push.model.audience.Audience; 13 import cn.jpush.api.push.model.notification.AndroidNotification; 14 import cn.jpush.api.push.model.notification.IosNotification; 15 import cn.jpush.api.push.model.notification.Notification; 16 import org.springframework.stereotype.Controller; 17 import org.springframework.web.bind.annotation.RequestMapping; 18 19 import com.fasterxml.jackson.databind.ser.std.StdKeySerializers.Default; 20 21 import java.util.HashMap; 22 import java.util.Map; 23 24 /** 25 * 26 * @Description:极光推送工具类 27 * @author: fuzongle 28 * @date: 2019年5月7日 下午9:55:02 29 */ 30 @Controller 31 @RequestMapping(value="/jpush") 32 public class JPushClientUtil{ 33 34 35 private static final String appKey = ""; // 必填,例如466f7032ac604e02fb7bda89 36 private static final String masterSecret = "";//必填,每个应用都对应一个masterSecret 37 38 39 private static final String appKey2 = ""; // 必填,例如466f7032ac604e02fb7bda89 40 private static final String masterSecret2 = "";//必填,每个应用都对应一个masterSecret 41 42 / 43 private static JPushClient jpush = null; 44 public static final int MAX = Integer.MAX_VALUE; 45 public static final int MIN = MAX / 2; 46 /** 47 * 保存离线的时长。秒为单位。最多支持10天(864000秒)。 48 * 0 表示该消息不保存离线。即:用户在线马上发出,当前不在线用户将不会收到此消息。 49 * 此参数不设置则表示默认,默认为保存1天的离线消息(86400秒)。 50 */ 51 private static long timeToLive = 60 * 60 * 24; 52 public static void main(String[] args) { 53 String msgTitle = "推送测试"; 54 String msgContent = "看到信息了么,看到就推送成功了!"; 55 //String tel_phone="18516779543"; 56 String tel_phone="15937016361"; 57 Map<String,String> extra = new HashMap<String, String>(); 58 extra.put("number","CO2017082811195687100"); 59 extra.put("type","money"); 60 push("JLY"+tel_phone,msgTitle,extra,0); 61 /* String title = "推送测试"; 62 PushPayload payload = JPushClientUtil.buildPushObject_all_all_alert(title); 63 System.out.println("服务器返回数据:" + payload.toString());*/ 64 } 65 /** 66 * 所有平台,所有设备,内容为 content 的通知 67 * 68 * @param content 69 * @return 70 */ 71 public static PushPayload buildPushObject_all_all_alert(String content) { 72 return PushPayload.alertAll(content); 73 } 74 75 /** 76 * 生成极光推送对象PushPayload(采用java SDK) 77 * @param alias 78 * @param alert 79 * @return PushPayload 80 */ 81 public static PushPayload buildPushObject_android_ios_alias_alert(String alias,String alert, 82 Map<String,String> extra,int value,String sound){ 83 return PushPayload.newBuilder() 84 .setPlatform(Platform.android_ios()) 85 .setAudience(Audience.alias(alias)) 86 .setNotification(Notification.newBuilder() 87 .addPlatformNotification(AndroidNotification.newBuilder() 88 .addExtras(extra) 89 .setAlert(alert) 90 .build()) 91 .addPlatformNotification(IosNotification.newBuilder() 92 .addExtras(extra) 93 .setAlert(alert) 94 .setBadge(value) 95 .setSound(sound) 96 .build()) 97 .build()) 98 .setOptions(Options.newBuilder() 99 .setApnsProduction(false)//true-推送生产环境 false-推送开发环境(测试使用参数) 100 .setTimeToLive(90)//消息在JPush服务器的失效时间(测试使用参数) 101 .build()) 102 .build(); 103 } 104 105 /** 106 * 极光推送方法(采用java SDK) 107 * @param alias 108 * @param alert 109 * @return PushResult 110 */ 111 public static PushResult push(String alias, String alert, Map<String,String> extra,int type){ 112 PushResult pushResult = null; 113 if (type==0){ 114 JPushClient jpushClient = new JPushClient(masterSecret, appKey,3); 115 PushPayload payload = buildPushObject_android_ios_alias_alert(alias,alert,extra,1,"default"); 116 try { 117 pushResult = jpushClient.sendPush(payload); 118 if(pushResult.isResultOK()){ 119 System.out.println("ok"); 120 } 121 } catch (APIConnectionException e) { 122 e.printStackTrace(); 123 return null; 124 } catch (APIRequestException e) { 125 e.printStackTrace(); 126 return null; 127 } 128 }else if (type==1){ 129 JPushClient jpushClient = new JPushClient(masterSecret2, appKey2,3); 130 PushPayload payload = buildPushObject_android_ios_alias_alert(alias,alert,extra,1,"default"); 131 try { 132 pushResult = jpushClient.sendPush(payload); 133 } catch (APIConnectionException e) { 134 e.printStackTrace(); 135 return null; 136 } catch (APIRequestException e) { 137 e.printStackTrace(); 138 return null; 139 } 140 // }else if (type==2){ 141 // JPushClient jpushClient = new JPushClient(masterSecret3, appKey3,3); 142 // PushPayload payload = buildPushObject_android_ios_alias_alert(alias,alert,extra); 143 // try { 144 // pushResult = jpushClient.sendPush(payload); 145 // } catch (APIConnectionException e) { 146 // e.printStackTrace(); 147 // return null; 148 // } catch (APIRequestException e) { 149 // e.printStackTrace(); 150 // return null; 151 // } 152 // } 153 } 154 return pushResult; 155 156 } 157 158 /** 159 * 给多个人推送消息 160 * @param list 161 */ 162 /*public static void pushManyMessage(List<PageData> list){ 163 164 for(int i=0;i<list.size();i++){ 165 pushMessage( 166 list.get(i).get("msgTitle").toString(), 167 list.get(i).get("msgContent").toString(), 168 list.get(i).get("user_id").toString() 169 ); 170 } 171 172 }*/ 173 174 /** 175 * 保持 sendNo 的唯一性是有必要的 176 */ 177 public static int getRandomSendNo() { 178 return (int) (MIN + Math.random() * (MAX - MIN)); 179 } 180 181 }