package com.dsh.utils.push; import com.alibaba.druid.support.json.JSONUtils; import com.dsh.utils.push.android.AndroidBroadcast; import com.dsh.utils.push.android.AndroidUnicast; import com.dsh.utils.push.ios.IOSBroadcast; import com.dsh.utils.push.ios.IOSUnicast; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.Map; /** * 友盟推送工具类 */ @Component public class PushUtil { /** * 友盟appkey */ private static String appkey; @Value("${umeng.appKey}") public void setAppkey(String appkey) { PushUtil.appkey = appkey; } /** * 友盟appMasterSecret */ private static String appMasterSecret; @Value("${umeng.appMasterSecret}") public void setAppMasterSecret(String appMasterSecret) { PushUtil.appMasterSecret = appMasterSecret; } /** * 友盟ioskey */ private static String ioskey; @Value("${umeng.iosKey}") public void setIoskey(String ioskey) { PushUtil.ioskey = ioskey; } /** * 友盟iosMasterSecret */ private static String iosMasterSecret; @Value("${umeng.iosMasterSecret}") public void setIosMasterSecret(String iosMasterSecret) { PushUtil.iosMasterSecret = iosMasterSecret; } /** * 友盟安卓离线推送 */ private static String channelActivity; @Value("${umeng.ChannelActivity}") public void setChannelActivity(String channelActivity) { PushUtil.channelActivity = channelActivity; } private String timestamp = null; private static PushClient client = new PushClient(); /** * 推送消息(单个设备) * * @param device 设备号 * @param deviceType 设备类型 * @param ticker 通知标题 * @param title 消息标题 * @param content 消息内容 * @param extras 扩展消息 * @param extras * @throws Exception */ public static void pushOne(String device, Integer deviceType, String ticker, String title, String content, Map extras, String type) throws Exception { if (deviceType == 0) {// 安卓 sendAndroidUnicast(device, ticker, title, content, extras, type); } else if (deviceType == 1) {// 苹果 sendIOSUnicast(device, ticker, title, content, extras, type); } } /** * 推送消息(广播) * * @param ticker 通知标题 * @param title 消息标题 * @param content 消息内容 * @param extras 扩展消息 * @param extras * @throws Exception */ public static void pushAll(String ticker, String title, String content, Map extras, String type) throws Exception { sendAndroidBroadcast(ticker, title, content, extras, type); sendIOSBroadcast(ticker, title, content, extras, type); } // /** // * 推送消息(多个设备) // * @param devices 设备集合 // * @param deviceType 设备类型 // * @param ticker // * @param title // * @param content // * @param extras // * @throws Exception // */ // public static void pushList(List devices, Integer deviceType, String ticker, String title, String content, Map extras) throws Exception { // if (deviceType == 0) {// 安卓 // if (!CollectionUtils.isEmpty(devices)) { // for (String device : devices) { // sendAndroidUnicast(device, ticker, title, content, extras); // } // } // }else if (deviceType == 1) {// 苹果 // if (!CollectionUtils.isEmpty(devices)) { // for (String device : devices) { // sendIOSUnicast(device, ticker, title, content, extras); // } // } // } // } /** * 推送安卓 * * @param deviceToken 设备号 * @param ticker 通知标题 * @param title 消息标题 * @param content 消息内容 * @param extras 扩展参数 * @throws Exception */ protected static void sendAndroidUnicast(String deviceToken, String ticker, String title, String content, Map extras, String type) throws Exception { AndroidUnicast unicast = new AndroidUnicast(appkey, appMasterSecret); // TODO Set your device token unicast.setDeviceToken(deviceToken); unicast.setTicker(ticker); unicast.setTitle(title); unicast.setText(content); unicast.setExtraField("type", type); unicast.goAppAfterOpen(); unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION); // TODO Set 'production_mode' to 'false' if it's a test device. // For how to register a test device, please see the developer doc. unicast.setProductionMode(); unicast.setChannelActivity(channelActivity); // Set customized fields if (extras != null && !extras.isEmpty()) { unicast.setExtraField("extras", JSONUtils.toJSONString(extras)); } // unicast.setChannelActivity("your channel activity"); // unicast.setChannelProperties("abc"); client.send(unicast); } /** * 推送安卓(广播) * * @param ticker 通知标题 * @param title 消息标题 * @param content 消息内容 * @param extras 扩展参数 * @throws Exception */ protected static void sendAndroidBroadcast(String ticker, String title, String content, Map extras, String type) throws Exception { AndroidBroadcast broadcast = new AndroidBroadcast(appkey, appMasterSecret); broadcast.setTicker(ticker); broadcast.setTitle(title); broadcast.setText(content); broadcast.setExtraField("type", type); broadcast.goAppAfterOpen(); broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION); // TODO Set 'production_mode' to 'false' if it's a test device. // For how to register a test device, please see the developer doc. broadcast.setProductionMode(); broadcast.setChannelActivity(channelActivity); // Set customized fields if (extras != null && !extras.isEmpty()) { broadcast.setExtraField("extras", JSONUtils.toJSONString(extras)); } // unicast.setChannelActivity("your channel activity"); // unicast.setChannelProperties("abc"); client.send(broadcast); } /** * 推送苹果(广播) * * @param ticker 通知标题 * @param title 消息标题 * @param content 消息内容 * @param extras 扩展参数 * @throws Exception */ protected static void sendIOSBroadcast(String ticker, String title, String content, Map extras, String type) throws Exception { IOSBroadcast broadcast = new IOSBroadcast(ioskey, iosMasterSecret); //alert值设置为字符串 //unicast.setAlert("IOS 单播测试"); //alert的值设置为字典 broadcast.setAlert(ticker, title, content); broadcast.setBadge(1); broadcast.setCustomizedField("type", type); broadcast.setSound("default"); // TODO set 'production_mode' to 'true' if your app is under production mode broadcast.setTestMode(); // Set customized fields broadcast.setCustomizedField("extras", JSONUtils.toJSONString(extras)); client.send(broadcast); } /** * 推送苹果 * * @param deviceToken 设备号 * @param ticker 通知标题 * @param title 消息标题 * @param content 消息内容 * @param extras 扩展参数 * @throws Exception */ protected static void sendIOSUnicast(String deviceToken, String ticker, String title, String content, Map extras, String type) throws Exception { IOSUnicast unicast = new IOSUnicast(ioskey, iosMasterSecret); // TODO Set your device token unicast.setDeviceToken(deviceToken); //alert值设置为字符串 //unicast.setAlert("IOS 单播测试"); //alert的值设置为字典 unicast.setAlert(ticker, title, content); unicast.setBadge(1); unicast.setCustomizedField("type", type); unicast.setSound("default"); // TODO set 'production_mode' to 'true' if your app is under production mode unicast.setTestMode(); // Set customized fields unicast.setCustomizedField("extras", JSONUtils.toJSONString(extras)); client.send(unicast); } }