package org.yeshi.utils.push; import com.alibaba.fastjson.JSONObject; import com.huawei.push.android.*; import com.huawei.push.message.AndroidConfig; import com.huawei.push.message.Notification; import com.huawei.push.messaging.HuaweiApp; import com.huawei.push.messaging.HuaweiMessaging; import com.huawei.push.model.Importance; import com.huawei.push.model.Urgency; import com.huawei.push.model.Visibility; import com.huawei.push.reponse.SendResponse; import com.huawei.push.util.InitAppUtils; import com.vivo.push.sdk.notofication.Message; import com.vivo.push.sdk.notofication.Result; import com.vivo.push.sdk.notofication.TargetMessage; import com.vivo.push.sdk.server.Sender; import org.yeshi.utils.push.entity.PushAppInfo; import org.yeshi.utils.push.entity.PushMessage; import org.yeshi.utils.push.exception.MeiZuPushException; import org.yeshi.utils.push.exception.VIVOPushException; 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 { 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的跳转参数 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"; //参数说明链接: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 tokens 不能超过1000个 * @return * @throws IOException * @throws MeiZuPushException */ public static String pushNotificationByTokens(PushAppInfo appInfo, PushMessage message, List tokens) throws Exception, VIVOPushException { 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("the_sample_bi_tag_for_receipt_service") .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); System.out.println(response.getCode()); 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.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("祝大家春节快乐", "下午好,hello world",null, "tejiapush", "com.huawei.codelabpush/deeplink", activityParams); String[] ids = new String[]{ "IQAAAACy0jK5AAC29MbhaYv1GIvxiqez_mtCeUB3crtUvOZMVLAUGmxrc5EgFuLpL-NgipO5L7xlNrxJw-UtmsH09c_cXu8Lxyusb-OONu8YwqYiOA" }; try { HuaWeiPushUtil.pushNotificationByTokens(appInfo, message, Arrays.asList(ids)); } catch (Exception e) { e.printStackTrace(); } } private static class AuthTokenInfo { public String token; public long expireTime; } }