package com.ks.push.utils.push; import com.alibaba.fastjson.JSONObject; import com.google.gson.Gson; import com.huawei.push.android.AndroidNotification; import com.huawei.push.android.ClickAction; import com.huawei.push.message.AndroidConfig; import com.huawei.push.messaging.HuaweiApp; import com.huawei.push.messaging.HuaweiMessaging; import com.huawei.push.reponse.SendResponse; import com.huawei.push.util.InitAppUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.*; /** * 华为推送 * sdk链接:https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Examples-V5/server-sample-code-0000001050986079-V5 */ public class HuaWeiPushUtil { static Logger logger = LoggerFactory.getLogger(HuaWeiPushUtil.class); private static AndroidNotification createMessage(PushMessage message) { String title = message.getTitle(); String content = message.getContent(); while (title.length() > 40 - 3) { title = title.substring(0, title.length() - 1); } if (title.length() == 40 - 3) { title += "..."; } while (content.length() > 1024 - 3) { content = content.substring(0, content.length() - 1); } if (content.length() == 1024 - 3) { title += "..."; } String uri = String.format("intent://%s?#Intent;scheme=%s;launchFlags=0x4000000;", message.getActivityHostPath(), message.getActivityScheme()); //增加Activity的跳转参数 Map activityParams = message.getActivityParams(); for (Iterator its = activityParams.keySet().iterator(); its.hasNext(); ) { String k = its.next(); logger.info("activityParams:{}-{}", k, activityParams.get(k)); try { if (activityParams.get(k) != null) { uri += String.format("S.%s=%s;", k, URLEncoder.encode(activityParams.get(k), "UTF-8")); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } uri += "end"; //参数说明链接:https://developer.huawei.com/consumer/cn/doc/development/HMSCore-References-V5/https-send-api-0000001050986197-V5 AndroidNotification androidNotification = AndroidNotification.builder() .setTitle(title) .setBody(content) .setClickAction(ClickAction.builder() //消息点击行为类型,取值如下: //1:打开应用自定义页面 //2:点击后打开特定URL //3:点击后打开应用 .setType(1) //设置打开特定URL,本字段填写需要打开的URL, 当type为2时必选。 // .setUrl("https://www.huawei.com") //自定义页面中intent的实现,请参见指定intent参数​。 //当type为1时,字段intent和action至少二选一。 .setIntent(uri) .build()) //0:默认样式 //1:大文本样式 //3:Inbox样式 // .setStyle(1) .build(); return androidNotification; } /** * 推送通知 * * @param appInfo * @param message * @param bigTag * @param tokens 不能超过1000个 * @return * @throws IOException * @throws MeiZuPushException */ public static String pushNotificationByTokens(PushAppInfo appInfo, PushMessage message,String bigTag, List tokens) throws Exception { HuaweiApp app = InitAppUtils.initializeApp(appInfo.getAppId(), appInfo.getAppSecret()); HuaweiMessaging huaweiMessaging = HuaweiMessaging.getInstance(app); AndroidNotification androidNotification = createMessage(message); //参数说明:https://developer.huawei.com/consumer/cn/doc/development/HMSCore-References-V5/https-send-api-0000001050986197-V5#ZH-CN_TOPIC_0000001124288117__p837521812163 AndroidConfig androidConfig = AndroidConfig.builder().setCollapseKey(-1) //透传消息投递优先级,取值如下: // //HIGH //NORMAL //设置为HIGH时需要申请权限,请参见申请特殊权限。 // //HIGH级别消息到达用户手机时可强制拉起应用进程。 // .setUrgency(Urgency.HIGH.getValue()) //消息缓存时间,单位是秒 // .setTtl("10000s") //批量任务消息标识,消息回执时会返回给应用服务器,应用服务器可以识别bi_tag对消息的下发情况进行统计分析。 .setBiTag(bigTag) .setNotification(androidNotification) .build(); com.huawei.push.message.Message msg = com.huawei.push.message.Message.builder() .setAndroidConfig(androidConfig) .addAllToken(tokens) .build(); SendResponse response = huaweiMessaging.sendMessage(msg); logger.info("华为推送响应结果:{}", new Gson().toJson(response)); return response.getRequestId(); } public static void main(String[] args) { PushAppInfo appInfo = new PushAppInfo("104190661", "", "5acdb54adb1b99c80a7a68b3bacdfea45c342df0535f040304ebb343c929b3bc"); appInfo.setPackageName("com.tejia.lijin"); Map activityParams = new HashMap<>(); activityParams.put("activity", "com.tejia.lijin.app.ui.PushOpenClickActivity"); JSONObject jumpParams = new JSONObject(); jumpParams.put("url", "https://s.click.taobao.com/KnCv9iu"); jumpParams.put("from", "push"); activityParams.put("params", jumpParams.toJSONString()); activityParams.put("type", "baichuan"); PushMessage message = new PushMessage("大家好", "大家下午号", null, "tejiapush", "com.huawei.codelabpush/deeplink", activityParams); String[] ids = new String[]{ "IQAAAACy0jK5AACFYBY3hZ7tobGQokkDBMNYPPqx_XZuIyMhiqyG8wuJoy099BmeJlIW2vIlqPXArpMw8a-av7ZZPRfBAgRQPjq317-Fvf4PQ3dyDg", "IQAAAACy0jK5AACR2ZSxytr2xPCaNh9ejS56WZphjyQlp3RaCeePbrnpd0GanNYYpQztD32lgSmOlEL6GjMBBsLyK8g4Qo59jsPMoMwZbMBk_gppEA", "IQAAAACy0jK5AAC3fru7bLf-Bsk8QuNJOSPCUi82ZHtN93YPPbbwCdd3IARs_RON3NfZ2a6uWf3yp5hzW-eKy4vIE_5qWqCUeDsaKRsCtzG1-8smIQ" }; try { HuaWeiPushUtil.pushNotificationByTokens(appInfo, message,"null", Arrays.asList(ids)); } catch (Exception e) { e.printStackTrace(); } } private static class AuthTokenInfo { public String token; public long expireTime; } }