package com.yeshi.buwan.util;
|
|
import com.tencent.xinge.Message;
|
import com.tencent.xinge.MessageIOS;
|
import com.tencent.xinge.Style;
|
import com.tencent.xinge.XingeApp;
|
import com.yeshi.buwan.domain.VideoInfo;
|
import org.apache.log4j.Logger;
|
import org.json.JSONArray;
|
import org.json.JSONObject;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
public class XingePushUtil {
|
static Logger log = Logger.getLogger(XingePushUtil.class);
|
|
public static long ACCESS_ID_ANDROID = 2100218527L;
|
public static String ACCESS_KEY_ANDROID = "A5H4W4C3RG8T";
|
public static String SECRET_KEY_ANDROID = "bf6073e3af796391ab8642a3ed69b627";
|
|
public static long ACCESS_ID_IOS = 2200218652L;
|
public static String ACCESS_KEY_IOS = "I17UVY2C2S9Y";
|
public static String SECRET_KEY_IOS = "eac2eacbdebafaabafeca3a6535bd3f8";
|
|
public static int IOS_ENV = XingeApp.IOSENV_PROD;
|
|
static XingeApp androidPush = new XingeApp(ACCESS_ID_ANDROID, SECRET_KEY_ANDROID);
|
static XingeApp iosPush = new XingeApp(ACCESS_ID_IOS, SECRET_KEY_IOS);
|
|
// 按账号推送视频更新
|
public static boolean pushVideoUpdateByAccount(String resourceId, String detailid, VideoInfo info,
|
List<String> iosUserList, List<String> androidUserList) {
|
if (iosUserList != null && iosUserList.size() > 0)
|
pushVideoUpdateByAccountIos(resourceId, detailid, info, iosUserList);
|
|
if (androidUserList != null && androidUserList.size() > 0)
|
pushVideoUpdateByAccountAndroid(resourceId, detailid, info, androidUserList);
|
return true;
|
}
|
|
private static boolean pushVideoUpdateByAccountAndroid(String resourceId, String detailid, VideoInfo info,
|
List<String> androidUserList) {
|
Message message = new Message();
|
message.setExpireTime(60 * 60 * 24);// 存放一天
|
message.setTitle("布丸君提醒:您关注的视频更新啦!");
|
message.setContent("《" + info.getName() + "》" + info.getTag());
|
message.setType(Message.TYPE_NOTIFICATION);
|
message.setMultiPkg(1);
|
// #含义:样式编号0,响铃,震动,不可从通知栏清除,不影响先前通知
|
message.setStyle(new Style(0, 1, 1, 0, 0));
|
// ClickAction action = new ClickAction();
|
// action.setActionType(ClickAction.TYPE_INTENT);
|
// //action.setActivity("com.weikou.beibeivideo.ui.media.VideoDetailActivity");
|
// // action.set
|
// message.setAction(action);
|
// 跳转Activity的参数
|
Map<String, Object> custom = new HashMap<String, Object>();
|
custom.put("Activity", "com.weikou.beibeivideo.ui.media.VideoDetailActivity");
|
custom.put("Id", info.getId());
|
custom.put("ResourceId", resourceId);
|
custom.put("DetailId", detailid);
|
custom.put("Share", "0");
|
custom.put("ThirdType", "0");
|
custom.put("PushType", "VideoUpdate");
|
message.setCustom(custom);
|
message.setSendTime(TimeUtil.getGernalTime(System.currentTimeMillis() + 1000, "yyyy-MM-dd HH:mm:ss"));
|
JSONObject result = androidPush.createMultipush(message);
|
log.info(result.toString());
|
long pushId = 0;
|
if (result.optInt("ret_code") == 0) {
|
pushId = Long.parseLong(result.optJSONObject("result").optString("push_id"));
|
}
|
|
if (pushId > 0) {
|
if (androidUserList != null && androidUserList.size() > 0) {
|
result = androidPush.pushAccountListMultiple(pushId, androidUserList);
|
log.info(result.toString());
|
System.out.println(result.toString());
|
return result.optInt("ret_code") == 0;
|
}
|
}
|
return false;
|
}
|
|
private static boolean pushVideoUpdateByAccountIos(String resourceId, String detailid, VideoInfo info,
|
List<String> iosUserList) {
|
MessageIOS messageIOS = new MessageIOS();
|
|
messageIOS.setAlert("《" + info.getName() + "》" + info.getTag());
|
// messageIOS.setBadge(1);
|
// messageIOS.setSound("beep.wav");
|
Map<String, Object> custom = new HashMap<String, Object>();
|
custom.put("PushType", "pushVideoUpdateByAccount");
|
JSONObject params = new JSONObject();
|
params.put("Id", info.getId());
|
params.put("ResourceId", resourceId);
|
params.put("DetailId", detailid);
|
params.put("ThirdType", "0");
|
custom.put("Params", params.toString());
|
messageIOS.setCustom(custom);
|
messageIOS.setExpireTime(60 * 60 * 24);
|
messageIOS.setCategory("Category");
|
JSONObject result = iosPush.createMultipush(messageIOS, IOS_ENV);// 暂时开发环境推送
|
log.info(result.toString());
|
long pushId = 0;
|
if (result.optInt("ret_code") == 0) {
|
pushId = Long.parseLong(result.optJSONObject("result").optString("push_id"));
|
}
|
if (pushId > 0) {
|
if (iosUserList != null && iosUserList.size() > 0) {
|
result = iosPush.pushAccountListMultiple(pushId, iosUserList);
|
log.info(result.toString());
|
System.out.println(result.toString());
|
return result.optInt("ret_code") == 0;
|
}
|
}
|
return false;
|
}
|
|
// 推送系统消息
|
public static boolean pushSystemMessage(String title, String content, List<String> iosTags,
|
List<String> androidTag) {
|
Message message = new Message();
|
message.setMultiPkg(1);
|
message.setContent(content);
|
message.setTitle(title);
|
message.setType(Message.TYPE_MESSAGE);
|
|
MessageIOS messageIOS = new MessageIOS();
|
messageIOS.setAlert(title);
|
Map<String, Object> custom = new HashMap<String, Object>();
|
custom.put("PushType", "pushSystemMessage");
|
custom.put("Title", title);
|
custom.put("Content", content);
|
messageIOS.setCustom(custom);
|
|
JSONObject result = androidPush.pushTags(0, androidTag, "OR", message);
|
log.info(result);
|
result = iosPush.pushTags(0, iosTags, "OR", messageIOS, IOS_ENV);
|
log.info(result);
|
|
return true;
|
}
|
|
// 推送网页链接消息
|
public static boolean pushUrlMessage(String title, String content, String url) {
|
Message message = new Message();
|
message.setMultiPkg(1);
|
message.setContent(content);
|
message.setTitle(title);
|
message.setType(Message.TYPE_NOTIFICATION);
|
message.setStyle(new Style(0, 1, 1, 0, 0));
|
// 跳转Activity的参数
|
Map<String, Object> custom = new HashMap<String, Object>();
|
custom.put("url", url);
|
custom.put("Activity", "com.weikou.beibeivideo.ui.mine.BrowserActivity");
|
message.setCustom(custom);
|
log.info(androidPush.pushAllDevice(0, message));
|
|
MessageIOS messageIOS = new MessageIOS();
|
messageIOS.setAlert(title);
|
custom = new HashMap<String, Object>();
|
custom.put("PushType", "pushUrlMessage");
|
custom.put("Url", url);
|
messageIOS.setCustom(custom);
|
log.info(iosPush.pushAllDevice(0, messageIOS, IOS_ENV));
|
return true;
|
}
|
|
// 推送合辑
|
|
public static boolean pushSpecialMessage(String content, String specialId) {
|
Message message = new Message();
|
message.setMultiPkg(1);
|
message.setTitle("布丸君提醒您:");
|
message.setContent(content);
|
message.setType(Message.TYPE_NOTIFICATION);
|
message.setStyle(new Style(0, 1, 1, 0, 0));
|
Map<String, Object> custom = new HashMap<String, Object>();
|
custom.put("Activity", "com.weikou.beibeivideo.ui.category.SpecificDetailActivity");
|
custom.put("id", specialId);
|
message.setCustom(custom);
|
JSONObject result = androidPush.pushAllDevice(0, message);
|
log.info(result);
|
|
MessageIOS messageIOS = new MessageIOS();
|
messageIOS.setAlert(content);
|
messageIOS.setBadge(1);
|
messageIOS.setSound("beep.wav");
|
custom = new HashMap<String, Object>();
|
custom.put("PushType", "pushSpecialMessage");
|
custom.put("Id", specialId);
|
messageIOS.setCustom(custom);
|
result = iosPush.pushAllDevice(0, messageIOS, IOS_ENV);
|
log.info(result);
|
|
return true;
|
}
|
|
// 推送评论回复消息
|
public static boolean pushCommentMessage(String messageStr, String account) {
|
boolean androidSuccess = false;
|
boolean iosSuccess = false;
|
Message message = new Message();
|
message.setMultiPkg(1);
|
message.setTitle("布丸君提醒您:");
|
message.setContent(messageStr);
|
message.setType(Message.TYPE_NOTIFICATION);
|
message.setStyle(new Style(0, 1, 1, 0, 0));
|
Map<String, Object> custom = new HashMap<String, Object>();
|
custom.put("Activity", "com.weikou.beibeivideo.ui.mine.SystemMessageActivity");
|
message.setCustom(custom);
|
JSONObject result = androidPush.pushSingleAccount(0, account, message);
|
log.info(result);
|
androidSuccess = result.optInt("ret_code") == 0;
|
|
MessageIOS messageIOS = new MessageIOS();
|
messageIOS.setAlert(messageStr);
|
messageIOS.setBadge(1);
|
messageIOS.setSound("beep.wav");
|
custom = new HashMap<String, Object>();
|
custom.put("PushType", "pushCommentMessage");
|
messageIOS.setCustom(custom);
|
result = iosPush.pushSingleAccount(0, account, messageIOS, IOS_ENV);
|
log.info(result);
|
iosSuccess = result.optInt("ret_code") == 0;
|
return (androidSuccess || iosSuccess);
|
}
|
|
// 获取Android Tag
|
public static List<String> getAndroidTags() {
|
List<String> tagList = new ArrayList<String>();
|
JSONObject obj = androidPush.queryTags();
|
if (obj.optInt("ret_code") == 0) {
|
JSONArray array = obj.optJSONObject("result").optJSONArray("tags");
|
for (int i = 0; i < array.length(); i++) {
|
tagList.add(array.optString(i));
|
}
|
}
|
return tagList;
|
}
|
|
// 获取IOS Tag
|
public static List<String> getIosTags() {
|
List<String> tagList = new ArrayList<String>();
|
JSONObject obj = iosPush.queryTags();
|
if (obj.optInt("ret_code") == 0) {
|
JSONArray array = obj.optJSONObject("result").optJSONArray("tags");
|
for (int i = 0; i < array.length(); i++) {
|
tagList.add(array.optString(i));
|
}
|
}
|
return tagList;
|
}
|
|
public static String pushVideoForAndriod(String title,String content,VideoInfo vi){
|
Message message = new Message();
|
message.setMultiPkg(1);
|
message.setTitle(title);
|
message.setContent(content);
|
message.setType(Message.TYPE_NOTIFICATION);
|
message.setStyle(new Style(0, 1, 1, 0, 0));
|
Map<String, Object> custom = new HashMap<String, Object>();
|
custom.put("Activity", "com.weikou.beibeivideo.ui.media.VideoDetailActivity");
|
custom.put("Id", vi.getId());
|
custom.put("ResourceId", vi.getResourceList().get(0).getId());
|
custom.put("DetailId", "1");
|
custom.put("Share", "0");
|
custom.put("ThirdType", "0");
|
custom.put("PushType", "VideoUpdate");
|
message.setCustom(custom);
|
JSONObject result = androidPush.pushAllDevice(0,message);
|
// JSONObject result = androidPush.pushSingleAccount(0, "376133", message);
|
Integer code = (Integer) result.get("ret_code");
|
if(0==code){
|
String pushId = String.valueOf(result.getJSONObject("result").get("push_id"));
|
return pushId;
|
}
|
return null;
|
}
|
|
public static String pushVideoForIOS(String content,VideoInfo vi){
|
MessageIOS messageIOS = new MessageIOS();
|
messageIOS.setAlert(content);
|
messageIOS.setBadge(1);
|
messageIOS.setSound("beep.wav");
|
Map<String, Object> custom = new HashMap<String, Object>();
|
custom.put("PushType", "pushVideoUpdateByAccount");
|
JSONObject params = new JSONObject();
|
params.put("Id", vi.getId());
|
params.put("ResourceId", vi.getResourceList().get(0).getId());
|
params.put("DetailId", "1");
|
params.put("ThirdType", "0");
|
custom.put("Params", params.toString());
|
messageIOS.setCustom(custom);
|
messageIOS.setExpireTime(60 * 60 * 24);
|
messageIOS.setCategory("Category");
|
org.json.JSONObject result = iosPush.pushAllDevice(0,messageIOS,XingeApp.IOSENV_DEV);
|
Integer code = (Integer) result.get("ret_code");
|
if(0==code){
|
String pushId = String.valueOf(result.getJSONObject("result").get("push_id"));
|
return pushId;
|
}
|
return null;
|
}
|
|
public static int pushVideo(String title,String content,VideoInfo vi){
|
String andriod = pushVideoForAndriod(title, content, vi);
|
String ios = pushVideoForIOS(content, vi);
|
if(andriod == null && ios==null){
|
return 0;
|
}else if(andriod == null && ios !=null){
|
return 1;
|
}else if(andriod != null && ios==null){
|
return 2;
|
}else{
|
return 3;
|
}
|
}
|
|
}
|