一些关于springcloud项目 中 jpush(极光推送)的使用心的

首先,使用jpush需要先完成一下操作:
  • 到极光推送官方网站注册开发者帐号
  • 登录进入管理控制台,创建应用程序,得到 Appkey(SDK与服务器端通过Appkey互相识别);
  • 下载SDK 集成到 App 里。

我们公司已经买过了极光推送的服务,拿过来直接用就可以了。

pom中添加依赖:

<!--极光推送start-->
<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jpush-client</artifactId>
    <version>3.3.3</version>
</dependency>
<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jiguang-common</artifactId>
    <version>1.0.8</version>
</dependency>
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.6.Final</version>
    <scope>compile</scope>
</dependency>
<!--极光推送end-->

yml文件属性配置

jPush:
 #极光推送
  appKey: 
  masterSecret: 

获取属性实体:

public class JPush {
    @Value("${jPush.appKey:''}")
    private String appKey;

    @Value("${jPush.masterSecret:''}")
    private String masterSecret;

    public String getAppKey() {
        return appKey;
    }

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

    public String getMasterSecret() {
        return masterSecret;
    }

    public void setMasterSecret(String masterSecret) {
        this.masterSecret = masterSecret;
    }
}

下面贴出jpush推送方式通用类,里面包含了大多数推送方法:

/**
 * 推送给设备标识参数的用户
 * @param registrationId 设备标识
 * @param notification_title 通知内容标题
 * @param msg_title 消息内容标题
 * @param msg_content 消息内容
 * @param id 扩展字段
 * @param type 扩展字段
 * @return 0推送失败,1推送成功
 */
public static int sendToRegistrationId( String registrationId,String notification_title, String msg_title, String msg_content, String id, String type, int days) {
    int result = 0;
    try {
        PushPayload pushPayload= JpushClientUtil.buildPushObject_all_registrationId_alertWithTitle(registrationId,notification_title,msg_title,msg_content,id,type,days);
        //System.out.println(pushPayload);
        PushResult pushResult=jPushClient.sendPush(pushPayload);
        //System.out.println(pushResult);
        if(pushResult.getResponseCode()==200){
            result=1;
        }
    } catch (APIConnectionException e) {
        e.printStackTrace();

    } catch (APIRequestException e) {
        e.printStackTrace();
    }

    return result;
}

/**
 * 发送给所有安卓用户
 * @param notification_title 通知内容标题
 * @param msg_title 消息内容标题
 * @param msg_content 消息内容
 * @param id 扩展字段
 * @param type 扩展字段
 * @return 0推送失败,1推送成功
 */
public static int sendToAllAndroid( String notification_title, String msg_title, String msg_content, String id, String type, int days) {
    int result = 0;
    try {
        PushPayload pushPayload= JpushClientUtil.buildPushObject_android_all_alertWithTitle(notification_title,msg_title,msg_content,id,type,days);
        //System.out.println(pushPayload);
        PushResult pushResult=jPushClient.sendPush(pushPayload);
        //System.out.println(pushResult);
        if(pushResult.getResponseCode()==200){
            result=1;
        }
    } catch (Exception e) {

        e.printStackTrace();
    }

    return result;
}

/**
 * 发送给所有IOS用户
 * @param notification_title 通知内容标题
 * @param msg_title 消息内容标题
 * @param msg_content 消息内容
 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值