admin
2024-10-16 7fa83e5dd03f7896bd1d1e8c47f5e926ff3d4ba0
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
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.AndroidNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ks.push.utils.JPushUtil;
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();
        }
 
        AndroidNotification.Builder androidBuilder = AndroidNotification.newBuilder().setAlert(message.getContent()).setTitle(message.getTitle());
 
        //通过intent推送
        if (message.getActivity().startsWith("intent:#")) {
            JsonObject intent = new JsonObject();
            intent.addProperty("url", message.getActivity());
            androidBuilder = androidBuilder.setIntent(intent).addExtras(message.getActivityParams());
        } else {
            androidBuilder = androidBuilder.addExtras(params);
        }
 
        PushPayload.Builder payloadBuilder = PushPayload.newBuilder().
                setPlatform(Platform.all())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(androidBuilder.build())
                        .build());
        loadTarget(payloadBuilder, regIds, aliasList);
        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;
    }
 
    private static void loadTarget(PushPayload.Builder builder, List<String> regIds, List<String> aliasList) {
        if (regIds != null) {
            builder.setAudience(Audience.registrationId(regIds));
        } else if (aliasList != null) {
            builder.setAudience(Audience.alias(aliasList));
        } else {
            builder.setAudience(Audience.all());
        }
    }
 
 
    /**
     * @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());
        loadTarget(payloadBuilder, regIds, aliasList);
 
        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("type", "web");
        JSONObject jumpParams = new JSONObject();
        jumpParams.put("url", "http://www.baidu.com");
        activityParams.put("params", jumpParams.toJSONString());
 
        PushAppInfo appInfo = new PushAppInfo();
        appInfo.setAppKey("6f219d3bfa78428537090fef");
        appInfo.setAppSecret("c48f35cd1cef4b717f721e4b");
 
 
        PushMessage pushMessage = new PushMessage();
        pushMessage.setActivity(JPushUtil.createIntent("com.yeshi.makemoney.video.app.push.PushOpenClickActivity", "com.yeshi.makemoney.video"));
        pushMessage.setActivityParams(activityParams);
        pushMessage.setTitle("测试标题");
        pushMessage.setContent("测试内容");
        pushNotification(appInfo, pushMessage, Arrays.asList(new String[]{"1507bfd3f737761f2f2"}), null);
    }
 
 
}