Skip to content

Commit

Permalink
feat: support review
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Aug 21, 2024
1 parent 63d96ae commit 6bff547
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 52 deletions.
82 changes: 43 additions & 39 deletions src/main/java/com/huawei/push/examples/SendTestMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,15 @@
*/
package com.huawei.push.examples;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;
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;

Expand All @@ -42,7 +36,7 @@ public void sendTestMessage() throws HuaweiMesssagingException {
.setBody("sample message body")
.build();

JSONObject multiLangKey = new JSONObject();
/* JSONObject multiLangKey = new JSONObject();
JSONObject titleKey = new JSONObject();
titleKey.put("en","好友请求");
JSONObject bodyKey = new JSONObject();
Expand All @@ -53,46 +47,56 @@ public void sendTestMessage() throws HuaweiMesssagingException {
LightSettings lightSettings = LightSettings.builder().setColor(Color.builder().setAlpha(0f).setRed(0f).setBlue(1f).setGreen(1f).build())
.setLightOnDuration("3.5")
.setLightOffDuration("5S")
.build();
.build();*/

AndroidNotification androidNotification = AndroidNotification.builder().setIcon("/raw/ic_launcher2")
.setColor("#AACCDD")
.setSound("/raw/shake")
.setDefaultSound(true)
.setTag("tagBoom")
AndroidNotification androidNotification = AndroidNotification.builder()
.setTitle("你有新的消息")
.setBody("你在干啥?")
//.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("Big Boom Title")
.setBigBody("Big Boom 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)
//.setBodyLocKey("key2")
//.addBodyLocArgs("boy").addBodyLocArgs("dog")
//.setTitleLocKey("key1")
//.addTitleLocArgs("Girl").addTitleLocArgs("Cat")
//.setChannelId("Your Channel ID")
//.setNotifySummary("some summary")
////.setMultiLangkey(multiLangKey)
//.setStyle(1)
//.setBigTitle("Big Boom Title")
//.setBigBody("Big Boom 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")
AndroidConfig androidConfig = AndroidConfig.builder()
//.setCollapseKey(-1)
//.setUrgency(Urgency.NORMAL.getValue())
//.setTtl("10000s")
//.setBiTag("the_sample_bi_tag_for_receipt_service")
.setNotification(androidNotification)
.build();

Message message = Message.builder().setNotification(notification)
Message message = Message.builder()
//.setNotification(notification)
.setAndroidConfig(androidConfig)
.addToken("AND8rUp4etqJvbakK7qQoCVgFHnROXzH8o7B8fTl9rMP5VRFN83zU3Nvmabm3xw7e3gZjyBbp_wfO1jP-UyDQcZN_CtjBpoa7nx1WaVFe_3mqXMJ6nXJNUZcDyO_-k3sSw")
.addToken("IQAAAACy0LgAAADBbzjXeF4Y-BTmHuEkn6fwOdK0a-FP1wUHByFpI-gHHg7fcxNc1eEDG0jNWNtS_vQZ91IqBMJssJoYpCuwVXtyeG8J2xYqLHwu5Q")
.build();

SendResponse response = huaweiMessaging.sendMessage(message, true);
SendResponse response = huaweiMessaging.sendMessage(message, false);
System.out.println(JSON.toJSONString(response));
}

public static void main(String[] args) throws HuaweiMesssagingException {
new SendTestMessage().sendTestMessage();
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/huawei/push/message/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import com.huawei.push.util.CollectionUtils;
import com.huawei.push.util.ValidatorUtils;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -157,6 +155,7 @@ public static class Builder {
private List<String> token = new ArrayList<>();
private String topic;
private String condition;
private List<Review> review;

private Builder() {
}
Expand Down
105 changes: 105 additions & 0 deletions src/main/java/com/huawei/push/message/Review.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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.message;

import com.alibaba.fastjson.annotation.JSONField;
import com.huawei.push.util.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;

public class Review {
@JSONField(name = "reviewer")
private String reviewer;

@JSONField(name = "type")
private Integer type;

@JSONField(name = "result")
private Object result;

public void check() {
ValidatorUtils.checkArgument(StringUtils.isNotEmpty(this.reviewer), "reviewer should not be empty");
ValidatorUtils.checkArgument(type != null, "type should not be empty");
ValidatorUtils.checkArgument(result != null, "result should not be empty");
}

public Review() {
}

public Review(String reviewer, Integer type, Object result) {
this.reviewer = reviewer;
this.type = type;
this.result = result;
}

private Review(Builder builder) {
this.reviewer = builder.reviewer;
this.type = builder.type;
this.result = builder.result;
}

public String getReviewer() {
return reviewer;
}

public void setReviewer(String reviewer) {
this.reviewer = reviewer;
}

public Integer getType() {
return type;
}

public void setType(Integer type) {
this.type = type;
}

public Object getResult() {
return result;
}

public void setResult(Object result) {
this.result = result;
}

// Builder class
public static class Builder {
private String reviewer;
private Integer type;
private Object result;

public Builder() {
}

public Review.Builder setReviewer(String reviewer) {
this.reviewer = reviewer;
return this;
}

public Review.Builder setType(Integer type) {
this.type = type;
return this;
}

public Review.Builder setResult(Object result) {
this.result = result;
return this;
}

public Review build() {
return new Review(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

import com.huawei.push.exception.HuaweiMesssagingException;
import com.huawei.push.message.Message;
import com.huawei.push.message.Review;
import com.huawei.push.message.TopicMessage;
import com.huawei.push.reponse.SendResponse;

import java.util.List;

/**
* sending messages interface
*/
Expand All @@ -30,10 +33,11 @@ public interface HuaweiMessageClient {
*
* @param message message {@link Message}
* @param validateOnly A boolean indicating whether to send message for test. or not.
* @param review A list of {@link Review} objects.
* @return {@link SendResponse}.
* @throws HuaweiMesssagingException
*/
SendResponse send(Message message, boolean validateOnly, String accessToken) throws HuaweiMesssagingException;
SendResponse send(Message message, boolean validateOnly, List<Review> review, String accessToken) throws HuaweiMesssagingException;

SendResponse send(TopicMessage message, String operation, String accessToken) throws HuaweiMesssagingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alibaba.fastjson.JSONObject;
import com.huawei.push.exception.HuaweiMesssagingException;
import com.huawei.push.message.Message;
import com.huawei.push.message.Review;
import com.huawei.push.message.TopicMessage;
import com.huawei.push.model.TopicOperation;
import com.huawei.push.reponse.SendResponse;
Expand All @@ -37,6 +38,7 @@
import java.io.IOException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

Expand Down Expand Up @@ -69,9 +71,9 @@ public CloseableHttpClient getHttpClient() {
}

@Override
public SendResponse send(Message message, boolean validateOnly, String accessToken) throws HuaweiMesssagingException {
public SendResponse send(Message message, boolean validateOnly, List<Review> review, String accessToken) throws HuaweiMesssagingException {
try {
return sendRequest(message, validateOnly, accessToken);
return sendRequest(message, validateOnly, review, accessToken);
} catch (IOException e) {
throw new HuaweiMesssagingException(HuaweiMessaging.INTERNAL_ERROR, "Error while calling HCM backend service", e);
}
Expand Down Expand Up @@ -125,14 +127,14 @@ private SendResponse sendRequest(TopicMessage message, String operation, String
/**
* send request
*
* @param message message {@link Message}
* @param message message {@link Message}
* @param validateOnly A boolean indicating whether to send message for test or not.
* @param accessToken A String for oauth
* @return {@link SendResponse}
* @throws IOException If a error occurs when sending request
*/
private SendResponse sendRequest(Message message, boolean validateOnly, String accessToken) throws IOException, HuaweiMesssagingException {
Map<String, Object> map = createRequestMap(message, validateOnly);
private SendResponse sendRequest(Message message, boolean validateOnly, List<Review> review, String accessToken) throws IOException, HuaweiMesssagingException {
Map<String, Object> map = createRequestMap(message, validateOnly, review);
HttpPost httpPost = new HttpPost(this.HcmPushUrl);
StringEntity entity = new StringEntity(JSON.toJSONString(map), "UTF-8");
// String aqa = JSON.toJSONString(map);
Expand Down Expand Up @@ -163,13 +165,17 @@ private SendResponse sendRequest(Message message, boolean validateOnly, String a
*
* @param message A non-null {@link Message} to be sent.
* @param validateOnly A boolean indicating whether to send message for test or not.
* @param review A list of {@link Review} objects.
* @return a map of request
*/
private Map<String, Object> createRequestMap(Message message, boolean validateOnly) {
private Map<String, Object> createRequestMap(Message message, boolean validateOnly, List<Review> review) {
return new HashMap<String, Object>() {
{
put("validate_only", validateOnly);
put("message", message);
if (review != null && review.size() > 0) {
put("review", review);
}
}
};
}
Expand Down
27 changes: 23 additions & 4 deletions src/main/java/com/huawei/push/messaging/HuaweiMessaging.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
import com.google.common.base.Suppliers;
import com.huawei.push.exception.HuaweiMesssagingException;
import com.huawei.push.message.Message;
import com.huawei.push.message.Review;
import com.huawei.push.message.TopicMessage;
import com.huawei.push.model.TopicOperation;
import com.huawei.push.reponse.SendResponse;
import com.huawei.push.util.ValidatorUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
* This class is the entrance for all server-side HCM actions.
*
Expand Down Expand Up @@ -83,7 +85,7 @@ HuaweiMessageClient getMessagingClient() {
* delivery.
*/
public SendResponse sendMessage(Message message) throws HuaweiMesssagingException {
return sendMessage(message, false);
return sendMessage(message, false, null);
}

/**
Expand Down Expand Up @@ -116,7 +118,6 @@ public SendResponse listTopic(TopicMessage topicMessage) throws HuaweiMesssaging
return messagingClient.send(topicMessage, TopicOperation.LIST.getValue(), ImplHuaweiTrampolines.getAccessToken(app));
}


/**
* Sends message {@link Message}
*
Expand All @@ -131,7 +132,25 @@ public SendResponse listTopic(TopicMessage topicMessage) throws HuaweiMesssaging
public SendResponse sendMessage(Message message, boolean validateOnly) throws HuaweiMesssagingException {
ValidatorUtils.checkArgument(message != null, "message must not be null");
final HuaweiMessageClient messagingClient = getMessagingClient();
return messagingClient.send(message, validateOnly, ImplHuaweiTrampolines.getAccessToken(app));
return messagingClient.send(message, validateOnly, null, ImplHuaweiTrampolines.getAccessToken(app));
}

/**
* Sends message {@link Message}
*
* <p>If the {@code validateOnly} option is set to true, the message will not be actually sent. Instead
* HCM performs all the necessary validations, and emulates the send operation.
*
* @param message message {@link Message} to be sent.
* @param validateOnly a boolean indicating whether to send message for test or not.
* @param review A list of {@link Review} objects.
* @return {@link SendResponse}.
* @throws HuaweiMesssagingException exception.
*/
public SendResponse sendMessage(Message message, boolean validateOnly, List<Review> review) throws HuaweiMesssagingException {
ValidatorUtils.checkArgument(message != null, "message must not be null");
final HuaweiMessageClient messagingClient = getMessagingClient();
return messagingClient.send(message, validateOnly, review, ImplHuaweiTrampolines.getAccessToken(app));
}

/**
Expand Down

0 comments on commit 6bff547

Please sign in to comment.