admin
2021-06-30 92cc47680855fd0ad62c90de013ea79530cf5c21
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
package org.yeshi.utils.push;
 
import com.alibaba.fastjson.JSONObject;
import com.meizu.push.sdk.constant.PushType;
import com.meizu.push.sdk.server.IFlymePush;
import com.meizu.push.sdk.server.constant.ResultPack;
import com.meizu.push.sdk.server.model.push.PushResult;
import com.meizu.push.sdk.server.model.push.VarnishedMessage;
import org.yeshi.utils.push.entity.PushAppInfo;
import org.yeshi.utils.push.entity.PushMessage;
import org.yeshi.utils.push.exception.MeiZuPushException;
 
import java.io.IOException;
import java.util.*;
 
/**
 * 魅族推送
 * 推送文档:http://open.res.flyme.cn/fileserver/upload/file/201803/e174a5709f134f64aae3fb168aec8ea3.pdf
 */
public class MeiZuPushUtil {
 
    private static VarnishedMessage createMessage(PushAppInfo appInfo, PushMessage message) {
        VarnishedMessage.Builder builder = new VarnishedMessage.Builder().appId(Long.parseLong(appInfo.getAppId()))
                .title(message.getTitle())
                .content(message.getContent())
                //点击动作 (0,"打开应用"),(1,"打开应用页面"),(2,"打 开URI页面"),(3, "应用客户端自定义")【非必填, 默认值为0】
                .clickType(1);
        if (message.getActivity() != null) {
            builder = builder.activity(message.getActivity());
        }
        if (message.getActivityParams() != null && message.getActivityParams().size() > 0) {
            JSONObject params = new JSONObject();
            for (Iterator<String> its = message.getActivityParams().keySet().iterator(); its.hasNext(); ) {
                String key = its.next();
                params.put(key, message.getActivityParams().get(key));
            }
            builder = builder.parameters(params);
        }
        return builder.build();
    }
 
    /**
     * 推送通知
     *
     * @param appInfo
     * @param message
     * @param pushIds 不能超过1000个
     * @return
     * @throws IOException
     * @throws MeiZuPushException
     */
    public static String pushNotificationByPushId(PushAppInfo appInfo, PushMessage message, List<String> pushIds) throws IOException, MeiZuPushException {
        //推送标题限制字数1-32,推送内容限制字数1-100
        IFlymePush push = new IFlymePush(appInfo.getAppSecret());
        VarnishedMessage msg = createMessage(appInfo, message);
        ResultPack<PushResult> result = push.pushMessage(msg, pushIds, 2);
        if (result.isSucceed()) {
            return result.value().getMsgId();
        } else {
            throw new MeiZuPushException(result.code(), result.comment());
        }
    }
 
    /**
     * 推送所有
     *
     * @param appInfo
     * @param message
     * @return
     * @throws IOException
     * @throws MeiZuPushException
     */
    public static String pushNotificationAll(PushAppInfo appInfo, PushMessage message) throws IOException, MeiZuPushException {
        IFlymePush push = new IFlymePush(appInfo.getAppSecret());
        VarnishedMessage msg = createMessage(appInfo, message);
        ResultPack<Long> result = push.pushToApp(PushType.STATUSBAR, msg);
        if (result.isSucceed()) {
            return result.value() + "";
        } else {
            throw new MeiZuPushException(result.code(), result.comment());
        }
    }
 
 
    public static void main(String[] args) {
        PushAppInfo appInfo = new PushAppInfo("139085", "dc40ea74e19948a683cb1876d5a8813e", "6497adb8bde044c6a32c0a2766332ca3");
        Map<String, String> activityParams = new HashMap<>();
        activityParams.put("activity", "test");
        JSONObject jumpParams = new JSONObject();
        jumpParams.put("id", 123123);
        activityParams.put("params", jumpParams.toJSONString());
 
        PushMessage message = new PushMessage("你好啊", "下午好,hello world", "com.weikou.beibeivideo.ui.push.OpenClickActivity", null, null, activityParams);
 
        String[] ids = new String[]{
                "UCI4e0f4070047c4949057e76446d6474484500447b05"
        };
 
        try {
            MeiZuPushUtil.pushNotificationByPushId(appInfo, message, Arrays.asList(ids));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MeiZuPushException e) {
            e.printStackTrace();
        }
    }
 
 
}