极光推送

首先在极光官网注册帐号,并创建一个应用。在应用信息中绑定Android应用包名以及IOS开发人员申请的APNS证书文件,应用就创建完成了,把AppKey给移动端开发人员,设置到APP中。

一个应用供一个APP使用,如果你有多个APP就多创建几个应用。

一.java服务端需要做

1.导入jar包

maven方式

<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jpush-client</artifactId>
    <version>3.2.15</version>
</dependency>

jar包方式。需要如下jar包

极光推送

2.java推送消息实现方法

private static final String appKey ="你的appkey";
    private static final String masterSecret = "你的secret";
    public static final String content = "恭喜你被1000元现金红包砸中!";//通知的内容
    public static final String title = "中奖消息";

    public static Map<String, Object> pushMessage() {

        Map<String,Object> map = new LinkedHashMap<String,Object>();
        JPushClient jpushClient = new JPushClient(masterSecret, appKey);
        PushPayload payload = buildPushObject_all_all_alert();

        try {
            PushResult result = jpushClient.sendPush(payload);
            logger.info("result:"+result);
        } catch (APIConnectionException e) {
            logger.error("Connection error:"+e);
        } catch (APIRequestException e) {
            logger.error("Should review the error, and fix the request", e);
            logger.info("HTTP Status: " + e.getStatus());
            logger.info("Error Code: " + e.getErrorCode());
            logger.info("Error Message: " + e.getErrorMessage());
        }
        return map;
    }

    /**
     * 构建推送对象:推送给所有平台
     * setBadge(i)===i表示几条信息未读
     * @return
     */
    public static PushPayload buildPushObject_all_all_alert() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())//所有平台
                //.setAudience(Audience.all()) //所有人
                .setAudience(Audience.alias("4c0341a8d77011e69bce00155d016e02"))//向选定的人推送
                .setNotification(Notification.newBuilder()
                        .setAlert(content)
                        .addPlatformNotification(AndroidNotification.newBuilder().setTitle(title).build())
                        .addPlatformNotification(IosNotification.newBuilder().setBadge(1).setSound("default").build())
                                .build())
                                .build();
    }

    public static PushPayload buildPushObject_all_alias_alert() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())//所有人
                .setNotification(Notification.alert(""))
                .build();
    }

    public static void main(String[] args) {
        pushMessage();
    }

二.移动端需要做

用户登录app后,给用户设置一个别名alias传到Jpush服务器

三.极光社区

https://community.jiguang.cn/