86183
2022-09-09 0d999e33085c0a25c5525242748f6aa62a401159
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
package com.dsh.app.util.push;
 
import com.alibaba.druid.support.json.JSONUtils;
import com.dsh.app.util.push.android.AndroidUnicast;
import com.dsh.app.util.push.ios.IOSUnicast;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.util.Map;
 
/**
 * 友盟推送工具类
 */
@Component
public class PushUtil {
 
    /**
     * 友盟appkey
     */
    private static String appkey;
 
    @Value("${umeng.appKey}")
    public void setAppkey(String appkey) {
        PushUtil.appkey = appkey;
    }
 
    /**
     * 友盟appMasterSecret
     */
    private static String appMasterSecret;
 
    @Value("${umeng.appMasterSecret}")
    public void setAppMasterSecret(String appMasterSecret) {
        PushUtil.appMasterSecret = appMasterSecret;
    }
 
    /**
     * 友盟ioskey
     */
    private static String ioskey;
 
    @Value("${umeng.iosKey}")
    public void setIoskey(String ioskey) {
        PushUtil.ioskey = ioskey;
    }
 
    /**
     * 友盟iosMasterSecret
     */
    private static String iosMasterSecret;
 
    @Value("${umeng.iosMasterSecret}")
    public void setIosMasterSecret(String iosMasterSecret) {
        PushUtil.iosMasterSecret = iosMasterSecret;
    }
 
 
    /**
     * 友盟安卓离线推送
     */
    private static String channelActivity;
 
    @Value("${umeng.ChannelActivity}")
    public void setChannelActivity(String channelActivity) {
        PushUtil.channelActivity = channelActivity;
    }
 
    private String timestamp = null;
    private static PushClient client = new PushClient();
 
    /**
     * 推送消息(单个设备)
     *
     * @param device     设备号
     * @param deviceType 设备类型
     * @param ticker     通知标题
     * @param title      消息标题
     * @param content    消息内容
     * @param extras     扩展消息
     * @param extras
     * @throws Exception
     */
    public static void pushOne(String device, Integer deviceType, String ticker, String title, String content, Map<String, Object> extras, String type) throws Exception {
        if (deviceType == 0) {// 安卓
            sendAndroidUnicast(device, ticker, title, content, extras, type);
        } else if (deviceType == 1) {// 苹果
            sendIOSUnicast(device, ticker, title, content, extras, type);
        }
    }
 
//    /**
//     * 推送消息(多个设备)
//     * @param devices 设备集合
//     * @param deviceType 设备类型
//     * @param ticker
//     * @param title
//     * @param content
//     * @param extras
//     * @throws Exception
//     */
//    public static void pushList(List<String> devices, Integer deviceType, String ticker, String title, String content, Map<String, Object> extras) throws Exception {
//        if (deviceType == 0) {// 安卓
//            if (!CollectionUtils.isEmpty(devices)) {
//                for (String device : devices) {
//                    sendAndroidUnicast(device, ticker, title, content, extras);
//                }
//            }
//        }else if (deviceType == 1) {// 苹果
//            if (!CollectionUtils.isEmpty(devices)) {
//                for (String device : devices) {
//                    sendIOSUnicast(device, ticker, title, content, extras);
//                }
//            }
//        }
//    }
 
    /**
     * 推送安卓
     *
     * @param deviceToken 设备号
     * @param ticker      通知标题
     * @param title       消息标题
     * @param content     消息内容
     * @param extras      扩展参数
     * @throws Exception
     */
    protected static void sendAndroidUnicast(String deviceToken, String ticker, String title, String content, Map<String, Object> extras, String type) throws Exception {
        AndroidUnicast unicast = new AndroidUnicast(appkey, appMasterSecret);
        // TODO Set your device token
        unicast.setDeviceToken(deviceToken);
        unicast.setTicker(ticker);
        unicast.setTitle(title);
        unicast.setText(content);
        unicast.setExtraField("type", type);
        unicast.goAppAfterOpen();
        unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        unicast.setProductionMode();
        unicast.setChannelActivity(channelActivity);
        // Set customized fields
        if (extras != null && !extras.isEmpty()) {
            unicast.setExtraField("extras", JSONUtils.toJSONString(extras));
        }
//        unicast.setChannelActivity("your channel activity");
//        unicast.setChannelProperties("abc");
        client.send(unicast);
    }
 
    /**
     * 推送苹果
     *
     * @param deviceToken 设备号
     * @param ticker      通知标题
     * @param title       消息标题
     * @param content     消息内容
     * @param extras      扩展参数
     * @throws Exception
     */
    protected static void sendIOSUnicast(String deviceToken, String ticker, String title, String content, Map<String, Object> extras, String type) throws Exception {
        IOSUnicast unicast = new IOSUnicast(ioskey, iosMasterSecret);
        // TODO Set your device token
        unicast.setDeviceToken(deviceToken);
        //alert值设置为字符串
        //unicast.setAlert("IOS 单播测试");
        //alert的值设置为字典
        unicast.setAlert(ticker, title, content);
        unicast.setBadge(1);
        unicast.setCustomizedField("type", type);
        unicast.setSound("default");
        // TODO set 'production_mode' to 'true' if your app is under production mode
        unicast.setTestMode();
        // Set customized fields
        unicast.setCustomizedField("extras", JSONUtils.toJSONString(extras));
        client.send(unicast);
    }
}