更新鸿蒙推送,支持badge
# Conflicts: # src/main/java/cn/wildfirechat/push/hm/HMPushServiceImpl.java
This commit is contained in:
28
src/main/java/cn/wildfirechat/push/hm/PushOptions.java
Normal file
28
src/main/java/cn/wildfirechat/push/hm/PushOptions.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package cn.wildfirechat.push.hm;
|
||||
|
||||
import cn.wildfirechat.push.PushMessage;
|
||||
import cn.wildfirechat.push.PushMessageType;
|
||||
import com.google.gson.Gson;
|
||||
import com.turo.pushy.apns.PushType;
|
||||
|
||||
public class PushOptions {
|
||||
public boolean testMessage;
|
||||
public Integer ttl;
|
||||
public String biTag;
|
||||
public String receiptId;
|
||||
public Integer collapseKey;
|
||||
public Integer backgroundMode;
|
||||
|
||||
|
||||
public static PushOptions buildPushOptions(PushMessage pushMessage) {
|
||||
PushOptions pushOptions = new PushOptions();
|
||||
pushOptions.testMessage = false;
|
||||
if (pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_VOIP_INVITE || pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_VOIP_BYE) {
|
||||
pushOptions.ttl = 60;
|
||||
} else {
|
||||
// 保持默认值,1 天
|
||||
}
|
||||
|
||||
return pushOptions;
|
||||
}
|
||||
}
|
||||
43
src/main/java/cn/wildfirechat/push/hm/RequestBody.java
Normal file
43
src/main/java/cn/wildfirechat/push/hm/RequestBody.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package cn.wildfirechat.push.hm;
|
||||
|
||||
import cn.wildfirechat.push.PushMessage;
|
||||
import cn.wildfirechat.push.hm.payload.AlertPayload;
|
||||
import cn.wildfirechat.push.hm.payload.VoipPayload;
|
||||
import cn.wildfirechat.push.hm.payload.internal.Payload;
|
||||
import cn.wildfirechat.push.hm.payload.internal.Target;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class RequestBody {
|
||||
public Payload payload;
|
||||
public PushOptions pushOptions;
|
||||
public Target target;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new Gson().toJson(this);
|
||||
}
|
||||
|
||||
public static RequestBody buildAlertRequestBody(PushMessage pushMessage) {
|
||||
RequestBody requestBody = new RequestBody();
|
||||
requestBody.payload = AlertPayload.buildAlertPayload(pushMessage);
|
||||
Target target = new Target();
|
||||
target.token = new ArrayList<>();
|
||||
target.token.add(pushMessage.deviceToken);
|
||||
requestBody.target = target;
|
||||
requestBody.pushOptions = PushOptions.buildPushOptions(pushMessage);
|
||||
return requestBody;
|
||||
}
|
||||
|
||||
public static RequestBody buildVoipRequestBody(PushMessage pushMessage) {
|
||||
RequestBody requestBody = new RequestBody();
|
||||
requestBody.payload = VoipPayload.buildVoipPayload(pushMessage);
|
||||
Target target = new Target();
|
||||
target.token = new ArrayList<>();
|
||||
target.token.add(pushMessage.deviceToken);
|
||||
requestBody.target = target;
|
||||
requestBody.pushOptions = PushOptions.buildPushOptions(pushMessage);
|
||||
return requestBody;
|
||||
}
|
||||
}
|
||||
@@ -3,20 +3,12 @@ package cn.wildfirechat.push.hm.payload;
|
||||
import cn.wildfirechat.push.PushMessage;
|
||||
import cn.wildfirechat.push.PushMessageType;
|
||||
import cn.wildfirechat.push.Utility;
|
||||
import cn.wildfirechat.push.hm.payload.internal.ClickAction;
|
||||
import cn.wildfirechat.push.hm.payload.internal.Notification;
|
||||
import cn.wildfirechat.push.hm.payload.internal.Payload;
|
||||
import cn.wildfirechat.push.hm.payload.internal.Target;
|
||||
import cn.wildfirechat.push.hm.payload.internal.*;
|
||||
import com.google.gson.Gson;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
public class AlertPayload {
|
||||
Payload payload;
|
||||
Target target;
|
||||
public class AlertPayload extends Payload{
|
||||
Notification notification;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
@@ -24,10 +16,12 @@ public class AlertPayload {
|
||||
}
|
||||
|
||||
public static AlertPayload buildAlertPayload(PushMessage pushMessage) {
|
||||
AlertPayload alertPayload = new AlertPayload();
|
||||
Notification notification = new Notification();
|
||||
String[] titleAndBody = Utility.getPushTitleAndContent(pushMessage);
|
||||
notification.title = titleAndBody[0];
|
||||
notification.body = titleAndBody[1];
|
||||
alertPayload.notification = notification;
|
||||
|
||||
ClickAction clickAction = new ClickAction();
|
||||
|
||||
@@ -45,14 +39,13 @@ public class AlertPayload {
|
||||
|
||||
notification.clickAction = clickAction;
|
||||
|
||||
Target target = new Target();
|
||||
target.token = new ArrayList<>();
|
||||
target.token.add(pushMessage.deviceToken);
|
||||
|
||||
AlertPayload alertPayload = new AlertPayload();
|
||||
alertPayload.payload = new Payload();
|
||||
alertPayload.payload.notification = notification;
|
||||
alertPayload.target = target;
|
||||
Badge badge = new Badge();
|
||||
int badgeNum = pushMessage.getUnReceivedMsg() + pushMessage.getExistBadgeNumber();
|
||||
if (badgeNum <= 0) {
|
||||
badgeNum = 1;
|
||||
}
|
||||
badge.setNum = badgeNum;
|
||||
notification.badge = badge;
|
||||
|
||||
return alertPayload;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package cn.wildfirechat.push.hm.payload;
|
||||
|
||||
import cn.wildfirechat.push.hm.payload.internal.Notification;
|
||||
import cn.wildfirechat.push.hm.payload.internal.Payload;
|
||||
|
||||
public class ExtensionPayload extends Payload {
|
||||
public Notification notification;
|
||||
public String extraData;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package cn.wildfirechat.push.hm.payload;
|
||||
|
||||
import cn.wildfirechat.push.hm.payload.internal.Payload;
|
||||
|
||||
public class VoIPCallPayload extends Payload {
|
||||
public String extraData;
|
||||
}
|
||||
@@ -8,25 +8,15 @@ import com.google.gson.Gson;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
public class VoipPayload {
|
||||
Payload payload;
|
||||
Target target;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new Gson().toJson(this);
|
||||
}
|
||||
|
||||
public static VoipPayload buildAlertPayload(PushMessage pushMessage) {
|
||||
Target target = new Target();
|
||||
target.token = new ArrayList<>();
|
||||
target.token.add(pushMessage.deviceToken);
|
||||
public class VoipPayload extends Payload {
|
||||
public String extraData;
|
||||
|
||||
public static VoipPayload buildVoipPayload(PushMessage pushMessage) {
|
||||
VoipPayload voipPayload = new VoipPayload();
|
||||
voipPayload.payload = new Payload();
|
||||
voipPayload.payload.extraData = "TODO";
|
||||
voipPayload.target = target;
|
||||
|
||||
// 由于一般应用无法申请 voip 权限,故暂未实现
|
||||
// https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/push-apply-right#section7291115452410
|
||||
voipPayload.extraData = "TODO";
|
||||
return voipPayload;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package cn.wildfirechat.push.hm.payload.internal;
|
||||
|
||||
public class Badge {
|
||||
public Integer addNum;
|
||||
public Integer setNum;
|
||||
}
|
||||
@@ -1,11 +1,24 @@
|
||||
package cn.wildfirechat.push.hm.payload.internal;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
// https://developer.huawei.com/consumer/cn/doc/harmonyos-references/push-scenariozed-api-request-param#section17371529101117
|
||||
public class Notification {
|
||||
public String category = "IM";
|
||||
public String title;
|
||||
public String body;
|
||||
public ClickAction clickAction;
|
||||
public int style;
|
||||
public String image;
|
||||
public int style;
|
||||
public String bigTitle;
|
||||
public String bigBody;
|
||||
public Integer notifyId;
|
||||
public String appMessageId;
|
||||
public String profileId;
|
||||
public List<String> inboxContent;
|
||||
public ClickAction clickAction;
|
||||
public Badge badge;
|
||||
public String sound;
|
||||
public Integer soundDuration;
|
||||
public boolean foregroundShow;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
package cn.wildfirechat.push.hm.payload.internal;
|
||||
|
||||
public class Payload {
|
||||
public Notification notification;
|
||||
public String extraData;
|
||||
public abstract class Payload {
|
||||
}
|
||||
|
||||
3
src/main/java/cn/wildfirechat/push/hm/readme.md
Normal file
3
src/main/java/cn/wildfirechat/push/hm/readme.md
Normal file
@@ -0,0 +1,3 @@
|
||||
文档在这儿:
|
||||
|
||||
https://developer.huawei.com/consumer/cn/doc/harmonyos-references/push-rest-api
|
||||
Reference in New Issue
Block a user