admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.yeshi.fanli.util.factory;
 
import java.io.UnsupportedEncodingException;
 
import com.yeshi.fanli.dto.push.PushTypeEnum;
import com.yeshi.fanli.util.StringUtil;
 
import net.sf.json.JSONObject;
 
public class IOSPushFactory {
 
    private static JSONObject filterPushContent(JSONObject json) {
        if (json != null)
            try {
                boolean ischange = false;
                while (json.toString().getBytes("UTF-8").length > 253) {
                    // 首先削减内容,然后削减标题
                    String title = json.optJSONObject("aps").optJSONObject("alert").optString("title");
                    String body = json.optJSONObject("aps").optJSONObject("alert").optString("body");
                    if (StringUtil.isNullOrEmpty(body) || body.length() < 4) {
                        // 削减标题
                        if (!StringUtil.isNullOrEmpty(title) && title.length() > 6) {
                            json.optJSONObject("aps").optJSONObject("alert").put("title",
                                    title.substring(0, title.length() - 1));
                            continue;
                        }
                    } else {
                        ischange = true;
                        json.optJSONObject("aps").optJSONObject("alert").put("body",
                                body.substring(0, body.length() - 1));
                        continue;
                    }
                }
 
                if (ischange) {
                    String body = json.optJSONObject("aps").optJSONObject("alert").optString("body");
                    json.optJSONObject("aps").optJSONObject("alert").put("body", body + "...");
                }
 
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        return json;
    }
 
    /**
     * IOS商品推送 返回JSON的字符串长度小于等于256
     * 
     * @param auctionId
     *            淘宝商品ID
     * @param title
     * @param body
     * @return
     */
    public static JSONObject createGoodsPush(String auctionId, String title, String body) {
        JSONObject alert = new JSONObject();
        alert.put("title", title);
        alert.put("body", body);
        alert.put("badge", "1");
        alert.put("sound", "default");
        alert.put("url", "http://id=" + auctionId);
        alert.put("type",PushTypeEnum.goodsdetail.getCode());
 
        JSONObject aps = new JSONObject();
        aps.put("alert", alert);
        //aps.put("badge", 1);
        JSONObject json = new JSONObject();
        json.put("aps", aps);
        return filterPushContent(json);
    }
 
    /**
     * IOS站内信推送 返回JSON的字符串长度小于等于256
     * 
     * @param title
     * @param body
     * @return
     */
    public static JSONObject createZNXPush(String title, String body) {
        JSONObject alert = new JSONObject();
        alert.put("title", title);
        alert.put("body", body);
        alert.put("badge", "1");
        alert.put("sound", "default");
        alert.put("type", PushTypeEnum.ZNX.getCode());
        JSONObject aps = new JSONObject();
        aps.put("alert", alert);
        //aps.put("badge", 1);
        JSONObject json = new JSONObject();
        json.put("aps", aps);
        return filterPushContent(json);
    }
 
    /**
     * IOS链接推送 返回JSON的字符串长度小于等于256
     * 
     * @param shortUrl
     *            短链
     * @param title
     * @param body
     * @return
     */
    public static JSONObject createURLPush(String shortUrl, String title, String body) {
        JSONObject alert = new JSONObject();
        alert.put("title", title);
        alert.put("body", body);
        alert.put("badge", "1");
        alert.put("sound", "default");
        alert.put("type",PushTypeEnum.url.getCode());
        alert.put("url", shortUrl);
 
        JSONObject aps = new JSONObject();
        aps.put("alert", alert);
        //aps.put("badge", 1);
        JSONObject json = new JSONObject();
        json.put("aps", aps);
        return filterPushContent(json);
    }
 
    /**
     * WEEX页面推送
     * 
     * @param shortUrl
     * @param title
     * @param body
     * @return
     */
    public static JSONObject createWEEXPush(String shortUrl, String title, String body) {
        JSONObject alert = new JSONObject();
        alert.put("title", title);
        alert.put("body", body);
        alert.put("badge", "1");
        alert.put("sound", "default");
        alert.put("type", PushTypeEnum.weex.getCode());
        alert.put("url", shortUrl);
 
        JSONObject aps = new JSONObject();
        aps.put("alert", alert);
        //aps.put("badge", 1);
        JSONObject json = new JSONObject();
        json.put("aps", aps);
        return filterPushContent(json);
    }
 
    /**
     * 百川网页推送
     * 
     * @param shortUrl
     * @param title
     * @param body
     * @return
     */
    public static JSONObject createBaiChuanPush(String shortUrl, String title, String body) {
        JSONObject alert = new JSONObject();
        alert.put("title", title);
        alert.put("body", body);
        alert.put("badge", "1");
        alert.put("sound", "default");
        alert.put("type", PushTypeEnum.baichuan.getCode());
        alert.put("url", shortUrl);
 
        JSONObject aps = new JSONObject();
        aps.put("alert", alert);
        //aps.put("badge", 1);
        JSONObject json = new JSONObject();
        json.put("aps", aps);
        return filterPushContent(json);
    }
    
    /**
     * 福利中心推送
     * @param title
     * @param body
     * @return
     */
    public static JSONObject createWelfarePush( String title, String body) {
        JSONObject alert = new JSONObject();
        alert.put("title", title);
        alert.put("body", body);
        alert.put("badge", "1");
        alert.put("sound", "default");
        alert.put("type", PushTypeEnum.welfare.getCode());
 
        JSONObject aps = new JSONObject();
        aps.put("alert", alert);
        //aps.put("badge", 1);
        JSONObject json = new JSONObject();
        json.put("aps", aps);
        return filterPushContent(json);
    }
 
    
    /**
     * 用户签到
     * @param title
     * @param body
     * @return
     */
    public static JSONObject createUserSignInPush( String title, String body) {
        JSONObject alert = new JSONObject();
        alert.put("title", title);
        alert.put("body", body);
        alert.put("badge", "1");
        alert.put("sound", "default");
        alert.put("type", PushTypeEnum.signin.getCode());
 
        JSONObject aps = new JSONObject();
        aps.put("alert", alert);
        //aps.put("badge", 1);
        JSONObject json = new JSONObject();
        json.put("aps", aps);
        return filterPushContent(json);
    }
    
}