package org.yeshi.utils.push; import com.alibaba.fastjson.JSONObject; import com.meizu.push.sdk.constant.PushType; import com.meizu.push.sdk.server.IFlymePush; import com.meizu.push.sdk.server.constant.ResultPack; import com.meizu.push.sdk.server.model.push.PushResult; import com.meizu.push.sdk.server.model.push.VarnishedMessage; import com.xiaomi.xmpush.server.Constants; import com.xiaomi.xmpush.server.Message; import com.xiaomi.xmpush.server.Result; import com.xiaomi.xmpush.server.Sender; import org.json.simple.parser.ParseException; import org.yeshi.utils.push.entity.PushAppInfo; import org.yeshi.utils.push.entity.PushMessage; import org.yeshi.utils.push.exception.MeiZuPushException; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.*; /** * 小米推送 * 推送文档:https://dev.mi.com/console/doc/detail?pId=1278 */ public class XiaoMiPushUtil { private static Message createMessage(PushAppInfo appInfo, PushMessage message) { String title = message.getTitle(); String content = message.getContent(); if (title.length() > 50) title = title.substring(0, 50 - 3) + "..."; if (content.length() > 128) content = content.substring(0, 128 - 3) + "..."; //示例:intent:#Intent;component=com.doudou.ysvideo/com.weikou.beibeivideo.ui.push.OpenClickActivity;S.activity=test;S.params=%7B%22id%22%3A%22123123%22%7D;end String uri = String.format("intent:#Intent;component=%s/%s;", appInfo.getPackageName(), message.getActivity().replace(appInfo.getPackageName(), "")); //增加Activity的跳转参数 for (Iterator its = message.getActivityParams().keySet().iterator(); its.hasNext(); ) { String k = its.next(); try { uri += String.format("S.%s=%s;", k, URLEncoder.encode(message.getActivityParams().get(k), "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } uri += "end"; Message msg = new Message.Builder() //设置在通知栏展示的通知的标题, 不允许全是空白字符, 长度小于50, 一个中英文字符均计算为1(通知栏消息必填) .title(title) //设置在通知栏展示的通知描述, 不允许全是空白字符, 长度小于128, 一个中英文字符均计算为1(通知栏消息必填) .description(content) .restrictedPackageName(appInfo.getPackageName()) //1:使用默认提示音提示 //2:使用默认震动提示 //4:使用默认led灯光提示 //-1(系统默认值):以上三种效果都有 //0:以上三种效果都无,即静默推送。 //并且支持1,2,4的任意OR运算来实现声音、震动和闪光灯的任意组合。 .notifyType(1) // 使用默认提示音提示 //可选项, 对app提供一些扩展功能(除了这些扩展功能, // 开发者还可以定义一些key和value来控制客户端的行为, // 注:key和value的字符数不能超过1024, 至多可以设置10个key-value键值对) //.extra() .extra(Constants.EXTRA_PARAM_NOTIFY_EFFECT, Constants.NOTIFY_ACTIVITY) // 启动Activity的intent uri .extra(Constants.EXTRA_PARAM_INTENT_URI, uri) .build(); return msg; } /** * 推送通知 * * @param appInfo * @param message * @param regIds 不能超过1000个 * @return * @throws IOException * @throws MeiZuPushException */ public static String pushNotificationByRegIds(PushAppInfo appInfo, PushMessage message, List regIds) throws IOException { //推送标题限制字数1-32,推送内容限制字数1-100 Sender sender = new Sender(appInfo.getAppSecret()); Message msg = createMessage(appInfo, message); Result result = sender.unionSend(msg, regIds, 3); return result.getMessageId(); } /** * 推送所有 * * @param appInfo * @param message * @return * @throws IOException * @throws MeiZuPushException */ public static String pushNotificationAll(PushAppInfo appInfo, PushMessage message) throws IOException, ParseException { Sender sender = new Sender(appInfo.getAppSecret()); Message msg = createMessage(appInfo, message); Result result = sender.broadcastAll(msg, 3); return result.getMessageId(); } public static void main(String[] args) { PushAppInfo appInfo = new PushAppInfo("2882303761519871448", "5161987181448", "OcdMXvErcGtf1XyXnwrhOg=="); appInfo.setPackageName("com.tejia.lijin"); Map activityParams = new HashMap<>(); activityParams.put("activity", "com.weikou.beibeivideo.ui.media.VideoDetailActivity2"); JSONObject jumpParams = new JSONObject(); jumpParams.put("Id", "8219668"); jumpParams.put("Share", 0); jumpParams.put("ThirdType", 0); activityParams.put("params", jumpParams.toJSONString()); PushMessage message = new PushMessage("她们创业的那些事儿", "讲述了三个不同年龄、不同性格的女生因缘际会在一起创业的故事,在这条不被看好的创业之路上,她们终将学会,对鸟事一笑置之,不是认输,而是成长!", "com.tejia.lijin.app.ui.PushOpenClickActivity", null, null, activityParams); String[] ids = new String[]{ "t76FQBHolG/YQ9V7qmhtsTbLXNDcCNHfSfNK2k3iKI6B8RYL9FPSWJvXDaBNKy8N" }; try { XiaoMiPushUtil.pushNotificationByRegIds(appInfo, message,Arrays.asList(ids)); } catch (Exception e) { e.printStackTrace(); } // try { // XiaoMiPushUtil.pushNotificationByRegIds(appInfo, message, Arrays.asList(ids)); // } catch (Exception e) { // e.printStackTrace(); // } } }