java极光推送demo_Java集成极光推送

首先是jar,这里因为我项目中用的是Maven,需要jar的可自行去官网下载jar

在Maven中的pom.xml文件中添加

cn.jpush.api

jpush-client

3.2.17

cn.jpush.api

jiguang-common

1.0.3

JPushExample .java/**

极光推送

@author Bobby

*/

public class JPushExample {

/**

* 全平台推送

* @param parm

*/

public static void jpushAll(Map parm) {

//创建JPushClient

JPushClient jpushClient = new JPushClient(JPushConfig.MasterSecret, JPushConfig.AppKey);

//创建option

PushPayload payload = PushPayload.newBuilder()

.setPlatform(Platform.all()) //所有平台的用户

.setAudience(Audience.registrationId(parm.get("RegId")))//registrationId指定用户

.setNotification(Notification.newBuilder()

.addPlatformNotification(IosNotification.newBuilder()

.setAlert(parm.get("msg"))

.setBadge(+1)

.setSound("happy")

.addExtras(parm)

.build())

.addPlatformNotification(AndroidNotification.newBuilder()

.addExtras(parm)

.setAlert(parm.get("msg"))

.build())

.build())

.setOptions(Options.newBuilder().setApnsProduction(true).build())//指定开发环境

.setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息

.build();

try {

PushResult pu = jpushClient.sendPush(payload);

} catch (APIConnectionException e) {

e.printStackTrace();

} catch (APIRequestException e) {

e.printStackTrace();

}

}

/**

* android推送

* @param parm

*/

public static void jpushAndroid(Map parm) {

//创建JPushClient

JPushClient jpushClient = new JPushClient(JPushConfig.MasterSecret, JPushConfig.AppKey);

//推送的关键,构造一个payload

PushPayload payload = PushPayload.newBuilder()

.setPlatform(Platform.android())//指定android平台的用户

.setAudience(Audience.all())//你项目中的所有用户

.setNotification(Notification.android(parm.get("msg"), "XXX", parm))

.setOptions(Options.newBuilder().setApnsProduction(false).build())

//这里是指定开发环境,不用设置也没关系

.setMessage(Message.content(parm.get("msg")))

.build();

try {

PushResult pu = jpushClient.sendPush(payload);

} catch (APIConnectionException e) {

e.printStackTrace();

} catch (APIRequestException e) {

e.printStackTrace();

}

}

/**

* 推送IOS

* @param parm

*/

public static void jpushIOS(Map parm) {

//创建JPushClient

JPushClient jpushClient = new JPushClient(JPushConfig.MasterSecret, JPushConfig.AppKey);

PushPayload payload = PushPayload.newBuilder()

.setPlatform(Platform.ios())//ios平台的用户

.setAudience(Audience.all())//所有用户

.setNotification(Notification.newBuilder()

.addPlatformNotification(IosNotification.newBuilder()

.setAlert(parm.get("msg"))

.setBadge(+1)

.setSound("happy")

.addExtras(parm)

.build())

.build())

.setOptions(Options.newBuilder().setApnsProduction(false).build())

.setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息

.build();

try {

PushResult pu = jpushClient.sendPush(payload);

} catch (APIConnectionException e) {

e.printStackTrace();

} catch (APIRequestException e) {

e.printStackTrace();

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值