|
@@ -0,0 +1,166 @@
|
|
1
|
+package com.oqpo.api.service;
|
|
2
|
+
|
|
3
|
+import com.oqpo.api.constant.ApiConstants;
|
|
4
|
+import com.oqpo.api.entity.PushData;
|
|
5
|
+import com.oqpo.api.entity.oper.MemberEntity;
|
|
6
|
+import com.oqpo.api.enums.PushMessage;
|
|
7
|
+import com.oqpo.api.mapper.oper.UserMngMapper;
|
|
8
|
+import com.oqpo.api.web.interceptor.HeaderRequestInterceptor;
|
|
9
|
+import lombok.extern.slf4j.Slf4j;
|
|
10
|
+import org.codehaus.jackson.map.ObjectMapper;
|
|
11
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
12
|
+import org.springframework.beans.factory.annotation.Value;
|
|
13
|
+import org.json.JSONArray;
|
|
14
|
+import org.json.JSONException;
|
|
15
|
+import org.json.JSONObject;
|
|
16
|
+import org.springframework.http.HttpEntity;
|
|
17
|
+import org.springframework.http.client.ClientHttpRequestInterceptor;
|
|
18
|
+import org.springframework.stereotype.Service;
|
|
19
|
+
|
|
20
|
+import java.io.BufferedReader;
|
|
21
|
+import java.io.InputStreamReader;
|
|
22
|
+import java.io.OutputStream;
|
|
23
|
+import java.net.HttpURLConnection;
|
|
24
|
+import java.net.URL;
|
|
25
|
+import java.util.ArrayList;
|
|
26
|
+import java.util.HashMap;
|
|
27
|
+import java.util.List;
|
|
28
|
+import java.util.Map;
|
|
29
|
+
|
|
30
|
+@Service
|
|
31
|
+@Slf4j
|
|
32
|
+public class PushService extends CommonService {
|
|
33
|
+
|
|
34
|
+ @Autowired
|
|
35
|
+ private UserMngMapper userMngMapper;
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+ @Value("${push.firebase_server_key}")
|
|
39
|
+ private String firebase_server_key;
|
|
40
|
+
|
|
41
|
+ @Value("${push.firebase_api_url}")
|
|
42
|
+ private String firebase_api_url;
|
|
43
|
+
|
|
44
|
+ // jobType po 주문, rtn 반품 , reuUnqNo 관련번호
|
|
45
|
+ public PushData makePushData(String brandId, String jobType, String relUnqNo) throws JSONException, InterruptedException {
|
|
46
|
+
|
|
47
|
+ PushData pushData = new PushData();
|
|
48
|
+
|
|
49
|
+ List<MemberEntity> targetList = userMngMapper.selectPushTargetList(brandId);
|
|
50
|
+
|
|
51
|
+ if (targetList.size() > 0) {
|
|
52
|
+
|
|
53
|
+ JSONObject bodySA = new JSONObject();
|
|
54
|
+ //List<JSONArray> arrayListSA = new ArrayList<JSONArray>();
|
|
55
|
+ JSONArray arraySA = new JSONArray();
|
|
56
|
+
|
|
57
|
+ for (int i = 0 ; i < targetList.size() ;i++) {
|
|
58
|
+ MemberEntity tempEntity = targetList.get(i);
|
|
59
|
+ arraySA.put(tempEntity.getAppReg());
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ bodySA.put("registration_ids", arraySA);
|
|
63
|
+ bodySA.put("content_available", ApiConstants.PUSH_CONTENT_AVAILABLE);
|
|
64
|
+ bodySA.put("priority", ApiConstants.PUSH_PRIORITY);
|
|
65
|
+ bodySA.put("apns-push-type", ApiConstants.PUSH_APNS_PUSH_TYPE);
|
|
66
|
+ bodySA.put("apns-priority", ApiConstants.PUSH_APNS_PRIORITY);
|
|
67
|
+
|
|
68
|
+ JSONObject dataSA = new JSONObject();
|
|
69
|
+
|
|
70
|
+ try {
|
|
71
|
+ if ("PO".equals(jobType)) {
|
|
72
|
+ dataSA.put("title", "매장주문");
|
|
73
|
+ } else {
|
|
74
|
+ dataSA.put("title", "매장반품");
|
|
75
|
+ }
|
|
76
|
+ if ("PO".equals(jobType)) {
|
|
77
|
+ dataSA.put("body", PushMessage.PUSH_REQ.getName());
|
|
78
|
+ } else {
|
|
79
|
+ dataSA.put("body", PushMessage.PUSH_RTN.getName());
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ dataSA.put("click_action", ApiConstants.PUSH_ACTION);
|
|
83
|
+ //dataSA.put("data", entity.getPushLandingCd());
|
|
84
|
+ //dataSA.put("relNo", entity.getLinkUrl());
|
|
85
|
+ bodySA.put("data", dataSA);
|
|
86
|
+ } catch (Exception e) {
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ pushData.setBodyData(bodySA.toString());
|
|
90
|
+ //pushData.setPushNo(entity.getPushNo());
|
|
91
|
+ //pushData.setPushSize(targetList.size());
|
|
92
|
+
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ return pushData;
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+ public ArrayList<ClientHttpRequestInterceptor> pushSet(HttpEntity<String> entity) {
|
|
100
|
+
|
|
101
|
+ ArrayList<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
|
|
102
|
+
|
|
103
|
+ log.info("firebase_server_key ============>" + firebase_server_key);
|
|
104
|
+ interceptors.add(new HeaderRequestInterceptor("Authorization", "key=" + firebase_server_key));
|
|
105
|
+ interceptors.add(new HeaderRequestInterceptor("Content-Type", "application/json;charset=UTF-8"));
|
|
106
|
+
|
|
107
|
+ return interceptors;
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+ public void pushSend(String brandId, String jobType, String relUnqNo) {
|
|
112
|
+ log.info("=========================push run");
|
|
113
|
+ try {
|
|
114
|
+ PushData pushData = makePushData(brandId, jobType, relUnqNo);
|
|
115
|
+ String bodyData = pushData.getBodyData();
|
|
116
|
+ for (int i = 0; i < ApiConstants.PUSH_RETRY; i++) {
|
|
117
|
+ URL url = new URL(firebase_api_url);
|
|
118
|
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
119
|
+ conn.setDoOutput(true);
|
|
120
|
+ conn.setRequestMethod("POST");
|
|
121
|
+ conn.setRequestProperty("Content-Type", "application/json");
|
|
122
|
+ conn.setRequestProperty("Authorization", "key=" + firebase_server_key);
|
|
123
|
+ conn.setConnectTimeout(ApiConstants.URLCONN_TIME_OUT);
|
|
124
|
+ conn.setReadTimeout(ApiConstants.URLCONN_TIME_OUT);
|
|
125
|
+ conn.setDoOutput(true);
|
|
126
|
+
|
|
127
|
+ OutputStream os = conn.getOutputStream();
|
|
128
|
+ os.write(bodyData.getBytes("UTF-8"));
|
|
129
|
+ os.flush();
|
|
130
|
+ os.close();
|
|
131
|
+ int responseCode = conn.getResponseCode();
|
|
132
|
+ if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
133
|
+ BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
134
|
+ String inputLine;
|
|
135
|
+ StringBuffer response = new StringBuffer();
|
|
136
|
+
|
|
137
|
+ while ((inputLine = in.readLine()) != null) {
|
|
138
|
+ response.append(inputLine);
|
|
139
|
+ }
|
|
140
|
+ in.close();
|
|
141
|
+ // 결과 처리
|
|
142
|
+ Map<String,Object> responsMap = new ObjectMapper().readValue(response.toString(), HashMap.class);
|
|
143
|
+
|
|
144
|
+ int retSuccess = (int) responsMap.get("success");
|
|
145
|
+ log.info("========================connect retSuccess" + Integer.toString(retSuccess));
|
|
146
|
+ int retError = (int) responsMap.get("failure");
|
|
147
|
+ log.info("========================connect erroor" + Integer.toString(retError));
|
|
148
|
+ break;
|
|
149
|
+ } else {
|
|
150
|
+ Thread.sleep(1000);
|
|
151
|
+ log.info("========================connect erroor" + Integer.toString(i));
|
|
152
|
+ }
|
|
153
|
+
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+ } catch(Exception e) {
|
|
157
|
+ e.printStackTrace();
|
|
158
|
+ }
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+ }
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+}
|