5 Star 25 Fork 16

华为移动服务/hms-push-serverdemo-java

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
SendTopicMessage.java 4.30 KB
一键复制 编辑 原始数据 按行查看 历史
Mike-mei 提交于 2021-07-15 14:45 . Update parameter name
/*
* Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.huawei.push.examples;
import com.alibaba.fastjson.JSONObject;
import com.huawei.push.android.AndroidNotification;
import com.huawei.push.android.BadgeNotification;
import com.huawei.push.android.ClickAction;
import com.huawei.push.android.Color;
import com.huawei.push.android.LightSettings;
import com.huawei.push.exception.HuaweiMesssagingException;
import com.huawei.push.message.AndroidConfig;
import com.huawei.push.message.Message;
import com.huawei.push.message.Notification;
import com.huawei.push.messaging.HuaweiApp;
import com.huawei.push.messaging.HuaweiMessaging;
import com.huawei.push.model.Urgency;
import com.huawei.push.model.Importance;
import com.huawei.push.model.Visibility;
import com.huawei.push.reponse.SendResponse;
import com.huawei.push.util.InitAppUtils;
public class SendTopicMessage {
/**
* send message to topic
*
* @throws HuaweiMesssagingException
*/
public void sendToTopic() throws HuaweiMesssagingException {
HuaweiApp app = InitAppUtils.initializeApp();
HuaweiMessaging huaweiMessaging = HuaweiMessaging.getInstance(app);
Notification notification = Notification.builder().setTitle("send topic message title")
.setBody("send topic message title")
.build();
JSONObject multiLangKey = new JSONObject();
JSONObject titleKey = new JSONObject();
titleKey.put("en","好友请求");
JSONObject bodyKey = new JSONObject();
bodyKey.put("en","My name is %s, I am from %s.");
multiLangKey.put("key1", titleKey);
multiLangKey.put("key2", bodyKey);
LightSettings lightSettings = LightSettings.builder().setColor(Color.builder().setAlpha(0f).setRed(0f).setBlue(1f).setGreen(1f).build())
.setLightOnDuration("3.5")
.setLightOffDuration("5S")
.build();
AndroidNotification androidNotification = AndroidNotification.builder().setIcon("/raw/ic_launcher2")
.setColor("#AACCDD")
.setSound("/raw/shake")
.setDefaultSound(true)
.setTag("tagBoom")
.setClickAction(ClickAction.builder().setType(2).setUrl("https://www.huawei.com").build())
.setBodyLocKey("key2")
.addBodyLocArgs("boy").addBodyLocArgs("dog")
.setTitleLocKey("key1")
.addTitleLocArgs("Girl").addTitleLocArgs("Cat")
.setChannelId("Your Channel ID")
.setNotifySummary("some summary")
.setMultiLangkey(multiLangKey)
.setStyle(1)
.setBigTitle("send topic message title")
.setBigBody("send topic message body")
.setAutoClear(86400000)
.setNotifyId(486)
.setGroup("Group1")
.setImportance(Importance.LOW.getValue())
.setLightSettings(lightSettings)
.setBadge(BadgeNotification.builder().setAddNum(1).setBadgeClass("Classic").build())
.setVisibility(Visibility.PUBLIC.getValue())
.setForegroundShow(true)
.build();
AndroidConfig androidConfig = AndroidConfig.builder().setCollapseKey(-1)
.setUrgency(Urgency.HIGH.getValue())
.setTtl("10000s")
.setBiTag("the_sample_bi_tag_for_receipt_service")
.setNotification(androidNotification)
.build();
Message message = Message.builder().setNotification(notification)
.setAndroidConfig(androidConfig)
.setTopic("targetTopic")
.build();
SendResponse response = huaweiMessaging.sendMessage(message);
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/hms-core/hms-push-serverdemo-java.git
git@gitee.com:hms-core/hms-push-serverdemo-java.git
hms-core
hms-push-serverdemo-java
hms-push-serverdemo-java
master

搜索帮助

A270a887 8829481 3d7a4017 8829481