Java知识书屋

【Java工具】之极光推送(九)

2019-06-13  本文已影响15人  3d0829501918

为了以后方便,特把极光推送方法记录下来。

<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jpush-client</artifactId>
    <version>3.2.17</version>
</dependency>
protected static final Logger LOG = LoggerFactory.getLogger(JpushUtils.class);
private static final String appKey ="你的appKey";
private static final String masterSecret = "你的masterSecret";
private static final ClientConfig clientConfig = ClientConfig.getInstance();
private static final JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
    /**
     * 推送全部用户
     * @param notification
     * @param message
     */
    public static boolean pushAll(String notification, String message) {
        boolean b = false;
        PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder().setAlert(notification).setSound("default")
                                .setBadge(5)
                                .setContentAvailable(true).addExtra("from", "JPush").addExtra("content", message)
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder().setAlert(notification).build())
                        .build())
                .setMessage(Message.content(message)).setOptions(Options.newBuilder().setApnsProduction(false).build())
                .build();
        try {
            PushResult result = jpushClient.sendPush(payload);
            b = true;
        } catch (APIConnectionException e) {
        } catch (APIRequestException e) {
        }
        return b;
    }
/**
     * 根据别名进行推送
     * @param notification
     * @param message  .setContentAvailable(true)
     */
    public static boolean pushSelected(String alias[], String notification, String message) {
        boolean b = false;
        PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(alias))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder().setAlert(notification).setSound("default")
                                .setBadge(5)
                                .addExtra("1", "http://tms.tikinmedia.com/upload/deviceVersion")
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder().setAlert(notification).build())
                        .build())
                //.setMessage(Message.content(message))
                .setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
        try {
            PushResult result = jpushClient.sendPush(payload);
            System.out.println(result);
            b = true;
        } catch (APIConnectionException e) {
        } catch (APIRequestException e) {
        }
        return b;
    }
    /**
     * 安卓与ios平台,通过别名推送
     * @param alias     别名
     * @param content   推送内容
     * @param key       拓展字段key值
     * @param value     拓展字段value
     * @return
     */
    public static boolean buildPushObject_all_alias_alert(String[] alias, String content,String key,String value) {
        boolean b = false;
        PushPayload payload =  PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.newBuilder()
                        .setAlert(content)
                        .addPlatformNotification(AndroidNotification.newBuilder().addExtra(key,value)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setBadge(5)
                                .setSound("default").addExtra(key,value)
                                .build())
                        .build())
                .build();
    try {
            PushResult pushResult = jpushClient.sendPush(payload);
            //解析数据,0表示成功 
            //{"msg_id":3828060948763255,"sendno":180326298,"statusCode":0}
            System.out.println(pushResult);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
          return b;
    }

迈开脚步,再长的路也不在话下;停滞不前,再短的路也难以到达。

上一篇 下一篇

猜你喜欢

热点阅读