|
| 1 | +package cn.jpush.api.push.model.notification; |
| 2 | + |
| 3 | +import com.google.gson.JsonElement; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import com.google.gson.JsonPrimitive; |
| 6 | + |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +/** |
| 10 | + * <p><b>HMOS Phone 通知类</b></p> |
| 11 | + * <br> |
| 12 | + * 具体使用方法请参考官方文档 https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#platform |
| 13 | + * 支持 HMOS Notification 的参数: |
| 14 | + * <ul> |
| 15 | + * <li>alert: 继承自父类 PlatformNotification 的 alert 属性;本类设置则覆盖。</li> |
| 16 | + * <li>title: 支持 setTitle(string) 方法来设置;可替换通知标题。 </li> |
| 17 | + * <li>_open_page: 支持 setOpenPage(String) 方法来设置;可设置点击打开的页面名称</li> |
| 18 | + * <li>extras: 继承自父类 PlatformNotification 的 extras 属性;支持通过 addExtra(key, value) 来添加自定义字段,具体看代码。 </li> |
| 19 | + * </ul> |
| 20 | + * <br> |
| 21 | + */ |
| 22 | +public class HmosNotification extends PlatformNotification { |
| 23 | + private static final String NOTIFICATION_HMOS = "hmos"; |
| 24 | + |
| 25 | + private static final String TITLE = "title"; |
| 26 | + private static final String CATEGORY = "category"; |
| 27 | + private static final String LARGE_ICON = "large_icon"; |
| 28 | + private static final String INTENT = "intent"; |
| 29 | + private static final String BADGE_ADD_NUM = "badge_add_num"; |
| 30 | + private static final String TEST_MESSAGE = "test_message"; |
| 31 | + private static final String RECEIPT_ID = "receipt_id"; |
| 32 | + |
| 33 | + private final String title; |
| 34 | + private final String category; |
| 35 | + private final String large_icon; |
| 36 | + private final JsonObject intent; |
| 37 | + private final Integer badge_add_num; |
| 38 | + private final Boolean test_message; |
| 39 | + private final String receipt_id; |
| 40 | + |
| 41 | + private HmosNotification(Object alert, |
| 42 | + String title, |
| 43 | + String category, |
| 44 | + String large_icon, |
| 45 | + JsonObject intent, |
| 46 | + Integer badge_add_num, |
| 47 | + Boolean test_message, |
| 48 | + String receipt_id, |
| 49 | + Map<String, String> extras, |
| 50 | + Map<String, Number> numberExtras, |
| 51 | + Map<String, Boolean> booleanExtras, |
| 52 | + Map<String, JsonObject> jsonExtras, |
| 53 | + Map<String, JsonPrimitive> customData) { |
| 54 | + super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData); |
| 55 | + |
| 56 | + this.title = title; |
| 57 | + this.category = category; |
| 58 | + this.large_icon = large_icon; |
| 59 | + this.intent = intent; |
| 60 | + this.badge_add_num = badge_add_num; |
| 61 | + this.test_message = test_message; |
| 62 | + this.receipt_id = receipt_id; |
| 63 | + } |
| 64 | + |
| 65 | + public static Builder newBuilder() { |
| 66 | + return new Builder(); |
| 67 | + } |
| 68 | + |
| 69 | + public static HmosNotification alert(String alert) { |
| 70 | + return newBuilder().setAlert(alert).build(); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + @Override |
| 75 | + public String getPlatform() { |
| 76 | + return NOTIFICATION_HMOS; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public JsonElement toJSON() { |
| 81 | + JsonObject json = super.toJSON().getAsJsonObject(); |
| 82 | + |
| 83 | + if (null != title) { |
| 84 | + json.add(TITLE, new JsonPrimitive(title)); |
| 85 | + } |
| 86 | + if (null != category) { |
| 87 | + json.add(CATEGORY, new JsonPrimitive(category)); |
| 88 | + } |
| 89 | + if (null != large_icon) { |
| 90 | + json.add(LARGE_ICON, new JsonPrimitive(large_icon)); |
| 91 | + } |
| 92 | + if (null != intent) { |
| 93 | + json.add(INTENT, intent); |
| 94 | + } |
| 95 | + if (null != badge_add_num) { |
| 96 | + json.add(BADGE_ADD_NUM, new JsonPrimitive(badge_add_num)); |
| 97 | + } |
| 98 | + if (null != test_message) { |
| 99 | + json.add(TEST_MESSAGE, new JsonPrimitive(test_message)); |
| 100 | + } |
| 101 | + if (null != receipt_id) { |
| 102 | + json.add(RECEIPT_ID, new JsonPrimitive(receipt_id)); |
| 103 | + } |
| 104 | + return json; |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + public static class Builder extends PlatformNotification.Builder<HmosNotification, Builder> { |
| 109 | + private String title; |
| 110 | + private String category; |
| 111 | + private String large_icon; |
| 112 | + private JsonObject intent; |
| 113 | + private Integer badge_add_num; |
| 114 | + private Boolean test_message; |
| 115 | + private String receipt_id; |
| 116 | + |
| 117 | + @Override |
| 118 | + protected Builder getThis() { |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public Builder setAlert(Object alert) { |
| 124 | + this.alert = alert; |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + public Builder setTitle(String title) { |
| 129 | + this.title = title; |
| 130 | + return this; |
| 131 | + } |
| 132 | + |
| 133 | + public Builder setCategory(String category) { |
| 134 | + this.category = category; |
| 135 | + return this; |
| 136 | + } |
| 137 | + |
| 138 | + public Builder setLarge_icon(String large_icon) { |
| 139 | + this.large_icon = large_icon; |
| 140 | + return this; |
| 141 | + } |
| 142 | + |
| 143 | + public Builder setIntent(JsonObject intent) { |
| 144 | + this.intent = intent; |
| 145 | + return this; |
| 146 | + } |
| 147 | + |
| 148 | + public Builder setBadge_add_num(Integer badge_add_num) { |
| 149 | + this.badge_add_num = badge_add_num; |
| 150 | + return this; |
| 151 | + } |
| 152 | + |
| 153 | + public Builder setTest_message(Boolean test_message) { |
| 154 | + this.test_message = test_message; |
| 155 | + return this; |
| 156 | + } |
| 157 | + |
| 158 | + public Builder setReceipt_id(String receipt_id) { |
| 159 | + this.receipt_id = receipt_id; |
| 160 | + return this; |
| 161 | + } |
| 162 | + |
| 163 | + public HmosNotification build() { |
| 164 | + return new HmosNotification(alert, title, category,large_icon,intent,badge_add_num,test_message,receipt_id, |
| 165 | + extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder, jsonExtrasBuilder, super.customData); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments