SpringBoot集成极光推送

SpringBoot集成极光推送
1.添加pom依赖,注意依赖版本问题

		<dependency>
            <groupId>cn.jpush.api</groupId>
            <artifactId>jpush-client</artifactId>
            <version>3.4.8</version>
        </dependency>
        <dependency>
            <groupId>cn.jpush.api</groupId>
            <artifactId>jiguang-common</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.6.Final</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.7</version>
        </dependency>

2.在控制器方法中设置好在jPush官网申请的appKey和masterSecret密码

 	@Resource
    PushService pushService;	
    
	@GetMapping("send/{msg}")
    @ResponseBody
    public String sendMessage(@PathVariable String msg){
        JPushClient jPushClient = new JPushClient(MASTER_SECRET,APP_KEY,null, ClientConfig.getInstance());
        PushPayload payload = pushService.sendPush(msg);
        return "发送完成!";
    }
@Service
public class PushService {
    protected static final Logger LOG = LoggerFactory.getLogger(PushService.class);

    protected static final String APP_KEY = "****************";
    protected static final String MASTER_SECRET = "**************************";
    protected static final String GROUP_PUSH_KEY = "************************";
    protected static final String GROUP_MASTER_SECRET = "*********************";

    public static final String TITLE = "Test from API example";
    public static final String ALERT = "Test from API Example - alert";
    public static final String MSG_CONTENT = "Test from API Example - msgContent";
    public static final String REGISTRATION_ID = "0900e8d85ef";
    public static final String TAG = "tag_api";
    public static long sendCount = 0;
    private static long sendTotalTime = 0;

    public static PushPayload sendPush(String msg){
        ClientConfig clientConfig = ClientConfig.getInstance();
        final JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
        final PushPayload payload = buildPushObject_all_all_alertWithTitle(msg);
        try {
            PushResult result = jpushClient.sendPush(payload);
            LOG.info("Got result -"+result);
        } catch (APIConnectionException e) {
            LOG.error("Connection error,should retry later",e);
        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            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 payload;
    }

    public static PushPayload buildPushObject_all_all_alert() {
        return PushPayload.alertAll(ALERT);
    }
    public static PushPayload buildPushObject_all_all_alertWithTitle(String msg){
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())
                .setNotification(Notification.alert(msg))
                .setMessage(Message.content(msg))
                .build();
    }

    public static PushPayload buildPushObject_android_tag_alertWithTitle() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.tag("tag1"))
                .setNotification(Notification.android(ALERT, TITLE, null))
                .build();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值