极光推送服务端实现

因为暂时开发自有对单一用户不同设备进行信息推送。
因此就简单实现了两个接口,可能后期项目完善会增加一些其他接口也不会太难。
极光推送已经把接口编写的简单至极,一个简单的例子就可以实现。但是会有很多参数有他不同的含义,可以参照:极光推送接口文档
接口代码:

public interface PushPortal {
    /**
     * 推送所有平台所有用户
     * @return
     */
    public boolean pushAllAllAlert(String alert , Map<String, String> extras);
    /**
     * 推送所有平台所有单一用户
     * @return
     */
    public boolean pushAllAliasAlert(String alert,List<String> aliasList,Map<String, String> extras);

}

实现代码:

@Service("pushPortal")
public class PushPortalImpl implements PushPortal{

    private final String MASTER_SECRET="e6a135cb55a7292******";
    //JPush服务器端下发的master_key
    private final String APP_KEY="284403bc5d3b2*******";
    //JPush服务器端下发的app_key
    private final boolean iosflat =false;
    //ios转发平台环境  True 表示推送生产环境,False 表示要推送开发环境   默认是开发
    long timeToLive = 86400L;  //信息保留时间
    JPushClient jpushClient = new JPushClient(MASTER_SECRET,APP_KEY, null, ClientConfig.getInstance());
    @Override
    public boolean pushAllAllAlert(String alert , Map<String, String> extras) {
        //组件传输参数
        PushPayload payload = PushPayload  
                .newBuilder()  
                .setPlatform(Platform.all())  //指定平台
                .setAudience(Audience.all())  //用户指定
                .setNotification(
                        Notification.newBuilder()
                                .setAlert(alert)
                                .addPlatformNotification(
                                        AndroidNotification.newBuilder().addExtras(extras).build())
                                .addPlatformNotification(
                                        IosNotification.newBuilder().addExtras(extras).build())
                                .build())
                .setOptions(Options.newBuilder().setApnsProduction(iosflat).setTimeToLive(timeToLive).build()).build();  
                //上面设置ios平台环境  True 表示推送生产环境,False 表示要推送开发环境   默认是开发
        try {
            PushResult result = jpushClient.sendPush(payload);
            System.out.println("Got result - " + result);
            return true;
        } catch (APIConnectionException e) {
            // Connection error, should retry later
            System.out.print("Connection error, should retry later "+e);
            return false;
        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            System.out.println("根据返回的错误信息核查请求是否正确"+e);
            System.out.println("HTTP 状态信息码: " + e.getStatus());
            System.out.println("JPush返回的错误码: " + e.getErrorCode());
            System.out.println("JPush返回的错误信息: " + e.getErrorMessage());
            return false;
        }
    }

    @Override
    public boolean pushAllAliasAlert(String alert,List<String> aliasList,Map<String, String> extras) {
        //
        PushPayload payload = PushPayload  
                .newBuilder()  
                .setPlatform(Platform.all())  //指定平台
                .setAudience(Audience.alias(aliasList))    //用户指定
                .setNotification(
                        Notification.newBuilder()
                                .setAlert(alert)
                                .addPlatformNotification(
                                        AndroidNotification.newBuilder().addExtras(extras).build())
                                .addPlatformNotification(
                                        IosNotification.newBuilder().addExtras(extras).build())
                                .build())
                .setOptions(Options.newBuilder().setApnsProduction(iosflat).setTimeToLive(timeToLive).build()).build(); 
        try {
            PushResult result = jpushClient.sendPush(payload);
            System.out.println("Got result - " + result);
            return true;
        } catch (APIConnectionException e) {
            // Connection error, should retry later
            System.out.print("Connection error, should retry later "+e);
            return false;
        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            System.out.println("根据返回的错误信息核查请求是否正确"+e);
            System.out.println("HTTP 状态信息码: " + e.getStatus());
            System.out.println("JPush返回的错误码: " + e.getErrorCode());
            System.out.println("JPush返回的错误信息: " + e.getErrorMessage());
            return false;
        }
    }
}

添加注入方便调用。

博客主要是提供参考,具体参数还是要看光放文档。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值