极光推送maven与代码,无需修改,粘贴即用
生活随笔
收集整理的這篇文章主要介紹了
极光推送maven与代码,无需修改,粘贴即用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
maven:
<!-- 極光推送 --> <dependency><groupId>cn.jpush.api</groupId><artifactId>jpush-client</artifactId><version>3.3.10</version> </dependency> <dependency><groupId>cn.jpush.api</groupId><artifactId>jiguang-common</artifactId><version>1.1.4</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> </dependency> <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.7</version> </dependency> <!--極光推送結束 -->?
正式代碼:
import java.util.HashMap; import java.util.Map;import org.slf4j.Logger; import org.slf4j.LoggerFactory;import cn.jiguang.common.ClientConfig; import cn.jiguang.common.TimeUnit; 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.Platform; import cn.jpush.api.push.model.PushPayload; import cn.jpush.api.push.model.audience.Audience; import cn.jpush.api.push.model.notification.AndroidNotification; import cn.jpush.api.push.model.notification.IosNotification; import cn.jpush.api.push.model.notification.Notification; import cn.jpush.api.push.model.notification.WinphoneNotification; import cn.jpush.api.report.ReceivedsResult; import cn.jpush.api.report.ReceivedsResult.Received; import cn.jpush.api.report.UsersResult;public class tuisong {protected static final Logger log = LoggerFactory.getLogger(tuisong.class);// demo App defined in resources/jpush-api.confprivate static final String APPKEY = "58854e6e9763f66fd3d3d245";private static final String MASTERSECRET = "f7935b875f2eb06fb904f9d8";public static JPushClient jpushClient = null;/*** 推送通知接口* @ 設備標識* @ tag數組* @ 推送標題* @ 推送類型* @ 推送內容*/public static void main(String[] args) {sendPushNotice("160a3797c870b06d4e0", null, "標題", "類型", "明凱推送消息測試","content"); // sendPushNotice(null, null, "標題", "類型", "測試alert","content"); }public static void sendPushNotice(String registrationId, String[] tags, String title, String btype,String alert, String content) {jpushClient = new JPushClient(MASTERSECRET, APPKEY);PushPayload payload = null;// 生成推送的內容,這里我們先測試全部推送// 通知提示信息if (content != null) {Map<String, String> map = new HashMap<String, String>();map.put("btype", btype);// 根據別名推送if (registrationId != null && tags == null) {payload = buldPushObject_all_all_alias(registrationId, title,alert, content, map);} else if (registrationId == null && tags != null) { // 根據tag[]推送payload = buldPushObject_all_all_tag(tags, title,alert, content, map);} else if (registrationId != null && tags != null) { // 別名和tags[] 推送通知payload = buldPushObject_all_all_aliasAndTag(registrationId, tags, title,alert, content, map);} else if (registrationId == null && tags == null) {payload = buldPushObject_all_all(title,alert, content, map);}} else {log.info("No notification - " + content);}try {System.out.println(payload.toString());PushResult result = jpushClient.sendPush(payload);System.out.println("推送結果:"+result.getResponseCode());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());}}/*** 推送自定義消息接口.根據別名修改標簽(tag)* @param alias 別名* @param content 推送內容*/public static void sendPushMessage(String alias, String content) {jpushClient = new JPushClient(MASTERSECRET, APPKEY);PushPayload payload = null;// For push, all you need do is to build PushPayload object.// PushPayload payload = buildPushObject_all_all_alert();// 判斷用戶別名和tag不為空的情況下才推送修改標簽(tag)if (content != null && alias != null) {payload = PushPayload.newBuilder().setAudience(Audience.alias(alias)).setPlatform(Platform.all()).setMessage(Message.content(content)).build();} else {log.info("No notification - " + content);}try {System.out.println(payload.toString());PushResult result = jpushClient.sendPush(payload);System.out.println(result + "................................");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());}}/*** 查詢記錄推送成功條數* @param mid*/public static void countPush(String mid) {jpushClient = new JPushClient(MASTERSECRET, APPKEY);PushPayload payload = null;try {ReceivedsResult result = jpushClient.getReportReceiveds(mid);Received received = result.received_list.get(0);System.out.println("android_received:" + received.android_received+ "\nios:" + received.ios_apns_sent);log.debug("Got result - " + result);} catch (APIConnectionException e) {// Connection error, should retry laterlog.error("Connection error, should retry later", e);} catch (APIRequestException e) {// Should review the error, and fix the requestlog.error("Should review the error, and fix the request", e);log.info("HTTP Status: " + e.getStatus());log.info("Error Code: " + e.getErrorCode());log.info("Error Message: " + e.getErrorMessage());}}/*** 統計用戶數據。需要vip用戶才能訪問*/public static void getReportUser() {jpushClient = new JPushClient(MASTERSECRET, APPKEY);PushPayload payload = null;try {UsersResult result = jpushClient.getReportUsers(TimeUnit.DAY, "2015-04-28", 8);// Received received =result// System.out.println("android_received:"+received.android_received+"\nios:"+received.ios_apns_sent);log.debug("Got result - " + result);} catch (APIConnectionException e) {// Connection error, should retry laterlog.error("Connection error, should retry later", e);} catch (APIRequestException e) {// Should review the error, and fix the requestlog.error("Should review the error, and fix the request", e);log.info("HTTP Status: " + e.getStatus());log.info("Error Code: " + e.getErrorCode());log.info("Error Message: " + e.getErrorMessage());}}/*** 根據別名通知推送* @ 別名* @ alert 推送內容* @return*/public static PushPayload buldPushObject_all_all_alias(String registrationId, String title,String alert, String content, Map<String, String> map) {return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.registrationId(registrationId)).setNotification(Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder().setAlert(alert).addExtra("content", content).addExtras(map).build()).addPlatformNotification(AndroidNotification.newBuilder().setAlert(alert).addExtra("content", content).setTitle(title).addExtras(map).build()).addPlatformNotification(WinphoneNotification.newBuilder().setAlert(alert).addExtra("content", content).addExtras(map).build()).build()).build();}/*** 根據tag通知推送* @ 別名* @ alert 推送內容* @return*/public static PushPayload buldPushObject_all_all_tag(String[] tags, String title,String alert, String content, Map<String, String> map) {return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.tag(tags)).setNotification(Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder().setAlert(alert).addExtra("content", content).addExtras(map).build()).addPlatformNotification(AndroidNotification.newBuilder().setAlert(alert).addExtra("content", content).setTitle(title).addExtras(map).build()).addPlatformNotification(WinphoneNotification.newBuilder().setAlert(alert).addExtra("content", content).addExtras(map).build()).build()).build();}/*** 根據tag通知推送* @ alias 別名* @ alert 推送內容* @return*/public static PushPayload buldPushObject_all_all_aliasAndTag(String registrationId, String[] tags,String alert, String title, String content, Map<String, String> map) {return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.registrationId(registrationId)).setAudience(Audience.tag(tags)).setNotification(Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder().setAlert(alert).addExtra("content", content).addExtras(map).build()).addPlatformNotification(AndroidNotification.newBuilder().setAlert(alert).addExtra("content", content).setTitle(title).addExtras(map).build()).addPlatformNotification(WinphoneNotification.newBuilder().setAlert(alert).addExtra("content", content).addExtras(map).build()).build()).build();}/*** 根據通知推送* @ alias 別名* @ alert 推送內容* @return*/public static PushPayload buldPushObject_all_all(String title,String alert, String content, Map<String, String> map) {return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.all()).setNotification(Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder().setAlert(alert).addExtra("content", content).addExtras(map).build()).addPlatformNotification(AndroidNotification.newBuilder().setAlert(alert).addExtra("content", content).setTitle(title).addExtras(map).build()).addPlatformNotification(WinphoneNotification.newBuilder().setAlert(alert).addExtra("content", content).addExtras(map).build()).build()).build();}}總結
以上是生活随笔為你收集整理的极光推送maven与代码,无需修改,粘贴即用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Git利用命令行提交代码完整步骤
- 下一篇: C#红包波动系数·高可用性,适合企业级开