/*
|
* 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();
|
titleKey.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("M.String.body")
|
.addBodyLocArgs("boy").addBodyLocArgs("dog")
|
.setTitleLocKey("M.String.title")
|
.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);
|
}
|
}
|