极光推送工具类JpushUtil,无需修改,直接可用
来源:互联网 发布:qq2012透明皮肤软件 编辑:程序博客网 时间:2024/06/06 01:52
package com.hxkj.Jpush;
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 JpushUtil {
protected static final Logger log = LoggerFactory.getLogger(JpushUtil.class);
// demo App defined in resources/jpush-api.conf
private static final String APPKEY = "**********************";
private static final String MASTERSECRET = "***************************";
public static JPushClient jpushClient = null;
/**
* 推送通知接口
* @param registrationId 设备标识
* @param tags tag数组
* @param title 推送标题
* @param btype 推送类型
* @param content 推送内容
*/
public static void main(String[] args) {
sendPushNotice("13165ffa4e0c8b42569", 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 later
log.error("Connection error, should retry later", e);
} catch (APIRequestException e) {
// Should review the error, and fix the request
log.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 later
log.error("Connection error, should retry later", e);
} catch (APIRequestException e) {
// Should review the error, and fix the request
log.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());
}
}
/**
* 根据别名通知推送
* @param alias 别名
* @param 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通知推送
* @param alias 别名
* @param 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通知推送
* @param alias 别名
* @param 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();
}
/**
* 根据通知推送
* @param alias 别名
* @param 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();
}
}
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 JpushUtil {
protected static final Logger log = LoggerFactory.getLogger(JpushUtil.class);
// demo App defined in resources/jpush-api.conf
private static final String APPKEY = "**********************";
private static final String MASTERSECRET = "***************************";
public static JPushClient jpushClient = null;
/**
* 推送通知接口
* @param registrationId 设备标识
* @param tags tag数组
* @param title 推送标题
* @param btype 推送类型
* @param content 推送内容
*/
public static void main(String[] args) {
sendPushNotice("13165ffa4e0c8b42569", 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 later
log.error("Connection error, should retry later", e);
} catch (APIRequestException e) {
// Should review the error, and fix the request
log.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 later
log.error("Connection error, should retry later", e);
} catch (APIRequestException e) {
// Should review the error, and fix the request
log.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());
}
}
/**
* 根据别名通知推送
* @param alias 别名
* @param 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通知推送
* @param alias 别名
* @param 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通知推送
* @param alias 别名
* @param 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();
}
/**
* 根据通知推送
* @param alias 别名
* @param 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();
}
}
阅读全文
1 0
- 极光推送工具类JpushUtil,无需修改,直接可用
- 极光消息推送工具类
- 极光消息推送工具类
- 极光推送-java工具类
- 极光推送 自己整理的关于极光的工具类
- 极光推送工具类 java httpClient
- 在用java进行极光推送的时候,需要的一个工具类: 使用的时候,直接调用 sendToRegistrationId 方法
- JPush极光推送API工具类(JAVA)
- 极光推送demo,可直接运行
- 线程池工具类,直接可用!
- android 调用极光推送SDK设置Tag和Alias工具类——java Web调用极光推送web SDK 发送推送消息工具类
- 修改极光推送通知的逻辑。
- 极光推送指南,直接加到项目里
- 极光推送
- 极光推送
- 极光推送
- 极光推送
- 极光推送
- iOS倒计时-swift2.3
- listview多条目加载
- marquee滚动标签
- struts2文件上传和下载
- 【转】解决 Atom 的包管理器——apm 访问速度慢的问题
- 极光推送工具类JpushUtil,无需修改,直接可用
- 运维之shell篇-----3.循环结构 、 case语句 、 函数及中断控制
- 中介者模式
- 前端里移动端到底比pc端多哪些知识,为啥面试时好多公司都问h5
- android中,在activity弹出一个对话框,并不会执行onpause生命周期方法
- LBS隐私保护的一些看法
- rabbitmq配置
- java学习系列2(多线程二)
- java常用类库---日期类的操作实例