admin
2022-01-12 4a7367a869ef12375ea6678ca44e102b8919c624
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
package com.ks.push.utils.push;
 
import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yeshi.utils.push.entity.PushAppInfo;
import org.yeshi.utils.push.entity.PushMessage;
 
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @author hxh
 * @title: JpushUtil
 * @description: 极光推送
 * @date 2021/12/2 15:34
 */
public class JpushUtil {
 
    private static Logger LOG = LoggerFactory.getLogger(JpushUtil.class);
 
 
    /**
     * @return java.lang.String
     * @author hxh
     * @description 推送通知
     * @date 15:59 2021/12/2
     * @param: appInfo
     * @param: message
     * @param: regIds
     * @param: aliasList
     **/
    public static String pushNotification(PushAppInfo appInfo, PushMessage message, List<String> regIds, List<String> aliasList) {
        JPushClient jpushClient = new JPushClient(appInfo.getAppSecret(), appInfo.getAppKey(), null, ClientConfig.getInstance());
 
        Map<String, String> params;
        if (message.getActivity() != null) {
            params = new HashMap<>();
            params.put("activity", message.getActivity());
            params.put("params", new Gson().toJson(message.getActivityParams()));
        } else {
            params = message.getActivityParams();
        }
 
        PushPayload.Builder payloadBuilder = PushPayload.newBuilder().
                setPlatform(Platform.all())
                .setNotification(Notification.android(message.getContent(), message.getTitle(), params));
 
 
        if (regIds != null) {
            payloadBuilder.setAudience(Audience.registrationId(regIds));
        } else if (aliasList != null) {
            payloadBuilder.setAudience(Audience.alias(aliasList));
        } else {
            payloadBuilder.setAudience(Audience.all());
        }
 
        PushPayload payload = payloadBuilder.build();
 
        try {
            PushResult result = jpushClient.sendPush(payload);
            LOG.info("Got result - " + result);
            return result.msg_id + "";
        } catch (APIConnectionException e) {
            LOG.error("Connection error, should retry later", e);
        } catch (APIRequestException e) {
            LOG.error("Should review the error, and fix the request", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());
        }
        return null;
    }
 
 
    /**
     * @return java.lang.String
     * @author hxh
     * @description 推送透传消息
     * @date 11:48 2022/1/12
     * @param: appInfo
     * @param: data
     * @param: regIds
     * @param: aliasList
     **/
    public static String pushMessage(PushAppInfo appInfo, JSONObject data, List<String> regIds, List<String> aliasList) {
        JPushClient jpushClient = new JPushClient(appInfo.getAppSecret(), appInfo.getAppKey(), null, ClientConfig.getInstance());
 
        PushPayload.Builder payloadBuilder = PushPayload.newBuilder().
                setPlatform(Platform.all())
                .setMessage(Message.newBuilder()
                        .setMsgContent(data.toJSONString())
                        .build());
        if (regIds != null) {
            payloadBuilder.setAudience(Audience.registrationId(regIds));
        } else if (aliasList != null) {
            payloadBuilder.setAudience(Audience.alias(aliasList));
        } else {
            payloadBuilder.setAudience(Audience.all());
        }
 
        PushPayload payload = payloadBuilder.build();
 
        try {
            PushResult result = jpushClient.sendPush(payload);
            LOG.info("Got result - " + result);
            return result.msg_id + "";
        } catch (APIConnectionException e) {
            LOG.error("Connection error, should retry later", e);
        } catch (APIRequestException e) {
            LOG.error("Should review the error, and fix the request", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());
        }
        return null;
    }
 
    public static void main(String[] args) {
        Map<String, String> activityParams = new HashMap<>();
        activityParams.put("activity", "test");
        activityParams.put("type", "requestLocation");
        JSONObject jumpParams = new JSONObject();
        jumpParams.put("id", 123123);
        activityParams.put("params", jumpParams.toJSONString());
 
        PushAppInfo appInfo = new PushAppInfo();
        appInfo.setAppKey("987d3d50f209994fa522d001");
        appInfo.setAppSecret("e4053447d0014576ea2c47ab");
//        PushMessage message = new PushMessage("你好啊", "下午好,hello world", null, null, null, activityParams);
 
        Map<String,Object> dataMap=new HashMap<>();
        dataMap.put("type","requestLocation");
 
        JSONObject data = new  JSONObject(dataMap);
        pushMessage(appInfo, data, Arrays.asList(new String[]{"100d8559098624d84e6"}), null);
    }
 
 
}