服务端极光推送

    xiaoxiao2021-03-25  3

    1.后台手动配置appkey和masterSecret

    /**

    * APP推送消息页面 * * @author  lys * * @date  2016年12月16日 下午3:05:06  * */ @RequestMapping("/toPush") public String toPush(){ return "WEB-INF/aoxin/aoxin/manage/webConfig/push";  } /** *   * 发送推送消息 * * @author  lys * * @date  2016年12月16日 下午3:39:13  * */ @ResponseBody @RequestMapping("/push") public String push(String content) { try { if(StringUtils.isNotBlank(content)){ WebConfig appKeyWebConfig = webConfigService.getWebConfigByConfigKey("appKey"); WebConfig masterSecretWebConfig = webConfigService.getWebConfigByConfigKey("masterSecret"); if(appKeyWebConfig == null){ return "请配置appKey,key的值一定要为:appKey"; } if(masterSecretWebConfig == null){ return "请配置masterSecret,key的值一定要为:masterSecret"; } new JPushUtil(content,appKeyWebConfig.getVal1(),masterSecretWebConfig.getVal1()).sendPush(); return "发送成功"; }else{ return "发送的内容不能为空"; } } catch (Exception e) { log.error("发送消息 push 方法异常 :",e); return "发送失败"; }

    }

    2.  maven  pom.xml配置

    <!-- JPush --> <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jpush-client</artifactId> <version>3.2.10</version> </dependency> <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jiguang-common</artifactId> <version>0.1.3</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3</version> </dependency>

    3.推送工具类

    package aoxin.aoxin.utils; import java.util.HashMap; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import cn.jiguang.commom.ClientConfig; import cn.jiguang.common.resp.APIConnectionException; import cn.jiguang.common.resp.APIRequestException; import cn.jpush.api.JPushClient; import cn.jpush.api.push.PushResult; import cn.jpush.api.push.model.Message; import cn.jpush.api.push.model.Options; import cn.jpush.api.push.model.Platform; import cn.jpush.api.push.model.PushPayload; import cn.jpush.api.push.model.SMS; import cn.jpush.api.push.model.audience.Audience; import cn.jpush.api.push.model.audience.AudienceTarget; import cn.jpush.api.push.model.notification.AndroidNotification; import cn.jpush.api.push.model.notification.IosAlert; import cn.jpush.api.push.model.notification.IosNotification; import cn.jpush.api.push.model.notification.Notification; import com.google.gson.JsonObject; public class JPushUtil {     protected static final Logger LOG = LoggerFactory.getLogger(JPushUtil.class);     // demo App defined in resources/jpush-api.conf  private static  String appKey ; private static  String masterSecret ; public static String alert ; public static final String TITLE = "Test from API example";     public static final String MSG_CONTENT = "Test from API Example - msgContent";     public static final String REGISTRATION_ID = "0900e8d85ef";     public static final String TAG = "tag_api";          public JPushUtil(String alert,String appKey,String masterSecret){     JPushUtil.alert = alert;     JPushUtil.appKey=appKey;     JPushUtil.masterSecret=masterSecret;     }      public static void main(String[] args) { //        testSendPushWithCustomConfig(); //        testSendIosAlert(); new JPushUtil("每天!","5abd0463bb00e9f20608e1e2","b98a9ff0567fc8bf44a1bfb3").sendPush(); } public static void testSendPush() {    // HttpProxy proxy = new HttpProxy("localhost", 3128);    // Can use this https proxy: https://github.com/Exa-Networks/exaproxy ClientConfig clientConfig = ClientConfig.getInstance();         JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);                  // For push, all you need do is to build PushPayload object.         PushPayload payload = buildPushObject_all_all_alert();                  try {             PushResult result = jpushClient.sendPush(payload);             LOG.info("Got result - " + result);                      } catch (APIConnectionException e) {             LOG.error("Connection error. Should retry later. ", e);                      } catch (APIRequestException e) {             LOG.error("Error response from JPush server. Should review and fix it. ", e);             LOG.info("HTTP Status: " + e.getStatus());             LOG.info("Error Code: " + e.getErrorCode());             LOG.info("Error Message: " + e.getErrorMessage());             LOG.info("Msg ID: " + e.getMsgId());         } } public  void sendPush() {    // HttpProxy proxy = new HttpProxy("localhost", 3128);    // Can use this https proxy: https://github.com/Exa-Networks/exaproxy ClientConfig clientConfig = ClientConfig.getInstance();         JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);                  // For push, all you need do is to build PushPayload object.         PushPayload payload = buildPushObject_all_all_alert();                  try {             PushResult result = jpushClient.sendPush(payload);             LOG.info("Got result - " + result);                      } catch (APIConnectionException e) {             LOG.error("Connection error. Should retry later. ", e);                      } catch (APIRequestException e) {             LOG.error("Error response from JPush server. Should review and fix it. ", e);             LOG.info("HTTP Status: " + e.getStatus());             LOG.info("Error Code: " + e.getErrorCode());             LOG.info("Error Message: " + e.getErrorMessage());             LOG.info("Msg ID: " + e.getMsgId());         } } public static PushPayload buildPushObject_all_all_alert() {    return PushPayload.alertAll(alert); }     public static PushPayload buildPushObject_all_alias_alert() {         return PushPayload.newBuilder()                 .setPlatform(Platform.all())                 .setAudience(Audience.alias("alias1"))                 .setNotification(Notification.alert(alert))                 .build();     }          public static PushPayload buildPushObject_android_tag_alertWithTitle() {         return PushPayload.newBuilder()                 .setPlatform(Platform.android())                 .setAudience(Audience.tag("tag1"))                 .setNotification(Notification.android(alert, TITLE, null))                 .build();     }          public static PushPayload buildPushObject_android_and_ios() {         return PushPayload.newBuilder()                 .setPlatform(Platform.android_ios())                 .setAudience(Audience.tag("tag1"))                 .setNotification(Notification.newBuilder()                 .setAlert("alert content")                 .addPlatformNotification(AndroidNotification.newBuilder()                 .setTitle("Android Title").build())                 .addPlatformNotification(IosNotification.newBuilder()                 .incrBadge(1)                 .addExtra("extra_key", "extra_value").build())                 .build())                 .build();     }     public static void buildPushObject_with_extra() {         JsonObject jsonExtra = new JsonObject();         jsonExtra.addProperty("extra1", 1);         jsonExtra.addProperty("extra2", false);         Map<String, String> extras = new HashMap<String, String>();         extras.put("extra_1", "val1");         extras.put("extra_2", "val2");         PushPayload payload = PushPayload.newBuilder()                 .setPlatform(Platform.android_ios())                 .setAudience(Audience.tag("tag1"))                 .setNotification(Notification.newBuilder()                         .setAlert("alert content")                         .addPlatformNotification(AndroidNotification.newBuilder()                                 .setTitle("Android Title")                                 .addExtras(extras)                                 .addExtra("booleanExtra", false)                                 .addExtra("numberExtra", 1)                                 .addExtra("jsonExtra", jsonExtra)                                 .build())                         .addPlatformNotification(IosNotification.newBuilder()                                 .incrBadge(1)                                 .addExtra("extra_key", "extra_value").build())                         .build())                 .build();         System.out.println(payload.toJSON());     }          public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {         return PushPayload.newBuilder()                 .setPlatform(Platform.ios())                 .setAudience(Audience.tag_and("tag1", "tag_all"))                 .setNotification(Notification.newBuilder()                         .addPlatformNotification(IosNotification.newBuilder()                                 .setAlert(alert)                                 .setBadge(5)                                 .setSound("happy")                                 .addExtra("from", "JPush")                                 .build())                         .build())                  .setMessage(Message.content(MSG_CONTENT))                  .setOptions(Options.newBuilder()                          .setApnsProduction(true)                          .build())                  .build();     }          public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {         return PushPayload.newBuilder()                 .setPlatform(Platform.android_ios())                 .setAudience(Audience.newBuilder()                         .addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))                         .addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))                         .build())                 .setMessage(Message.newBuilder()                         .setMsgContent(MSG_CONTENT)                         .addExtra("from", "JPush")                         .build())                 .build();     }     public static void testSendPushWithCustomConfig() {         ClientConfig config = ClientConfig.getInstance();         // Setup the custom hostname         config.setPushHostName("https://api.jpush.cn");         JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, config);         // For push, all you need do is to build PushPayload object.         PushPayload payload = buildPushObject_all_all_alert();         try {             PushResult result = jpushClient.sendPush(payload);             LOG.info("Got result - " + result);         } catch (APIConnectionException e) {             LOG.error("Connection error. Should retry later. ", e);         } catch (APIRequestException e) {             LOG.error("Error response from JPush server. Should review and fix it. ", e);             LOG.info("HTTP Status: " + e.getStatus());             LOG.info("Error Code: " + e.getErrorCode());             LOG.info("Error Message: " + e.getErrorMessage());             LOG.info("Msg ID: " + e.getMsgId());         }     }     public static void testSendIosAlert() {         JPushClient jpushClient = new JPushClient(masterSecret, appKey);         IosAlert alert = IosAlert.newBuilder()                 .setTitleAndBody("test alert", "test ios alert json")                 .setActionLocKey("PLAY")                 .build();         try {             PushResult result = jpushClient.sendIosNotificationWithAlias(alert, new HashMap<String, String>(), "alias1");             LOG.info("Got result - " + result);         } catch (APIConnectionException e) {             LOG.error("Connection error. Should retry later. ", e);         } catch (APIRequestException e) {             LOG.error("Error response from JPush server. Should review and fix it. ", e);             LOG.info("HTTP Status: " + e.getStatus());             LOG.info("Error Code: " + e.getErrorCode());             LOG.info("Error Message: " + e.getErrorMessage());         }     }     public static void testSendWithSMS() {         JPushClient jpushClient = new JPushClient(masterSecret, appKey);         try {             SMS sms = SMS.content("Test SMS", 10);             PushResult result = jpushClient.sendAndroidMessageWithAlias("Test SMS", "test sms", sms, "alias1");             LOG.info("Got result - " + result);         } catch (APIConnectionException e) {             LOG.error("Connection error. Should retry later. ", e);         } catch (APIRequestException e) {             LOG.error("Error response from JPush server. Should review and fix it. ", e);             LOG.info("HTTP Status: " + e.getStatus());             LOG.info("Error Code: " + e.getErrorCode());             LOG.info("Error Message: " + e.getErrorMessage());         }     } }

    转载请注明原文地址: https://ju.6miu.com/read-282408.html

    最新回复(0)