极光推送的java实现

第二导入

@PropertySource("classpath:config/properties/jpush.properties")
@Component
public class JpushConfig {
    @Value(value="${appKey}")
    public String appKey;
    
    @Value(value="${appSecret}")
    public String appSecret;

    public String getAppKey() {
        return appKey;
    }

    public void setAppKey(String appKey) {
        this.appKey = appKey;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }
    
    

}

第三就是实现

@Component
public class JpushService {

    private static Logger logger = LoggerFactory.getLogger(JpushService.class);
    
    @Autowired
    private JpushConfig jpushConfig;
    
    
    public void sendMsg(long userId,String message) {
        ClientConfig clientConfig = ClientConfig.getInstance();
        String host = (String) clientConfig.get(ClientConfig.PUSH_HOST_NAME);
        final NettyHttpClient client = new NettyHttpClient(ServiceHelper.getBasicAuthorization(jpushConfig.getAppKey(), jpushConfig.getAppSecret()), null, clientConfig);
        try {
            URI uri = new URI(host + clientConfig.get(ClientConfig.PUSH_PATH));
            PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(String.valueOf(userId))).setNotification(Notification.alert(message)).build();
            client.sendRequest(HttpMethod.POST, payload.toString(), uri, new NettyHttpClient.BaseCallback() {
                @Override
                public void onSucceed(ResponseWrapper responseWrapper) {
                    logger.debug("userid: {},sendmsg: {},Jpush result: {}" ,userId, message,responseWrapper.responseContent);
                }
            });
        } catch (URISyntaxException e) {
            logger.info("极光推送失败:", e);
        }finally {
            client.close();
        }
    }
    
    public void sendNewMsgNotice(long userId) {
        String message = "您有一条新信息!";
        this.sendMsg(userId, message );
    }
    

}

第四调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值