极光推送java案例_极光推送实例 - 橙子hhh的个人空间 - OSCHINA - 中文开源技术交流社区...

极光网址:https://www.jiguang.cn/

需求:先需要给手机推送一个消息,或者一个连接,点击消息唤醒APP,打开APP进入相对应的界面。

推送原理:极光可根据每个登陆者生成一个UUID 即  RegistrationID  作为标识 然后 根据这个 RegistrationID  去推送消息。

1、引入jar

cn.jpush.api

jpush-client

3.2.19

cn.jpush.api

jiguang-common

1.1.1

工具类:

import cn.jiguang.common.ClientConfig;

import cn.jiguang.common.resp.APIConnectionException;

import cn.jiguang.common.resp.APIRequestException;

import cn.jpush.api.JPushClient;

import cn.jpush.api.push.PushResult;

import cn.jpush.api.push.model.Options;

import cn.jpush.api.push.model.Platform;

import cn.jpush.api.push.model.PushPayload;

import cn.jpush.api.push.model.audience.Audience;

import cn.jpush.api.push.model.notification.AndroidNotification;

import cn.jpush.api.push.model.notification.IosAlert;

import cn.jpush.api.push.model.notification.IosNotification;

import cn.jpush.api.push.model.notification.Notification;

public class JiguangPush {

//登录极光后台的生成的

private static String masterSecret = "";

//极光后台获取的key

private static String appKey = "";

private static int time =86400 ;

/**

* 极光推送

*/

public static PushResult jiguangPush(String[] arr,String notification_title, String msg_title, String msg_content,String url,String accptMd){

log.info("设备id" + arr.toString() + "的用户推送信息ALERT="+msg_content);

PushResult result = push(arr,notification_title,msg_title,msg_content,url,accptMd);

if(result != null && result.isResultOK()){

log.info("设备id" + result.toString() + "的信息推送成功!");

}else{

log.info("设备id" + result.toString() + "的信息推送失败!");

}

return result;

}

/**

* 生成极光推送对象PushPayload(采用java SDK)ios

* @param alias

* @param alert

* @param url

* @return PushPayload

*/

private static PushPayload buildPushObject_ios_alias_alert(String[] arr,String notification_title, String msg_title, String msg_content, String url){

return PushPayload.newBuilder()

.setPlatform(Platform.ios())//此处表明是什么设备,如 android ,ios ,winphone。

.setAudience(Audience.registrationId(arr))

.setNotification(Notification.newBuilder()

.addPlatformNotification(IosNotification.newBuilder()

.addExtra("pushUrl", url)

.setAlert(IosAlert.newBuilder().setTitleAndBody("",msg_title, msg_content).build())

.setSound("default")

.incrBadge(1)

.build())

.build())//推送内容

/* .setMessage(Message.newBuilder()

.setMsgContent(msg_content)

.setTitle(msg_title)

.build())*/

.setOptions(Options.newBuilder()

.setApnsProduction(false)//true-推送生产环境 false-推送开发环境(测试使用参数)

.setTimeToLive(time)//消息在JPush服务器的失效时间(测试使用参数)

.build())

.build();

}

/**

* 生成极光推送对象PushPayload(采用java SDK)android

* @param alias

* @param alert

* @param url

* @return PushPayload

*/

private static PushPayload buildPushObject_android_alias_alert(String[] arr,String notification_title, String msg_title, String msg_content, String url){

return PushPayload.newBuilder()

.setPlatform(Platform.android())//此处表明是什么设备,如 android ,ios ,winphone。

.setAudience(Audience.registrationId(arr))

.setNotification(Notification.newBuilder()

.addPlatformNotification(AndroidNotification.newBuilder()

.setTitle(msg_title)

.setAlert(msg_content)

.addExtra("pushUrl", url)

.build())

.build())//推送内容

/* .setMessage(Message.newBuilder()

.setMsgContent(msg_content)

.setTitle(msg_title)

.build())*/

.setOptions(Options.newBuilder()

.setApnsProduction(false)//true-推送生产环境 false-推送开发环境(测试使用参数)

.setTimeToLive(time)//消息在JPush服务器的失效时间(测试使用参数)

.build())

.build();

}

/**

* 生成极光推送对象PushPayload(采用java SDK)all 不指定设备

* @param alias

* @param alert

* @param url

* @return PushPayload

*/

private static PushPayload buildPushObject_android_ios_alias_alert(String[] arr,String notification_title, String msg_title, String msg_content, String url){

return PushPayload.newBuilder()

.setPlatform(Platform.android_ios())//此处表明是什么设备,如 android ,ios ,winphone。

.setAudience(Audience.all())

.setNotification(Notification.newBuilder()

.addPlatformNotification(AndroidNotification.newBuilder()

.setTitle(msg_title)

.setAlert(msg_content)

.addExtra("pushUrl", url)

.build())

.addPlatformNotification(IosNotification.newBuilder()

.addExtra("pushUrl", url)

.setAlert(IosAlert.newBuilder().setTitleAndBody("",msg_title, msg_content).build())

.setSound("default")

.incrBadge(1)

.build())

.build())//推送内容

/* .setMessage(Message.newBuilder()

.setMsgContent(msg_content)

.setTitle(msg_title)

.build())*/

.setOptions(Options.newBuilder()

.setApnsProduction(false)//true-推送生产环境 false-推送开发环境(测试使用参数)

.setTimeToLive(time)//消息在JPush服务器的失效时间(测试使用参数)

.build())

.build();

}

/**

* 生成极光推送对象PushPayload(采用java SDK)all 指定设备

* @param alias

* @param alert

* @param url

* @return PushPayload

*/

private static PushPayload buildPushObject_android_ios_alias_alert_appoint_device(String[] arr,String notification_title, String msg_title, String msg_content, String url){

return PushPayload.newBuilder()

.setPlatform(Platform.android_ios())//此处表明是什么设备,如 android ,ios ,winphone。

.setAudience(Audience.registrationId(arr))

.setNotification(Notification.newBuilder()

.addPlatformNotification(AndroidNotification.newBuilder()

.setTitle(msg_title)

.setAlert(msg_content)

.addExtra("pushUrl", url)

.addExtra("push_type", "2")

.build())

.addPlatformNotification(IosNotification.newBuilder()

.addExtra("pushUrl", url)

.addExtra("push_type", "2")

.setAlert(IosAlert.newBuilder().setTitleAndBody("",msg_title, msg_content).build())

.setSound("default")

.incrBadge(1)

.build())

.build())//推送内容

/*.setMessage(Message.newBuilder()

.setMsgContent(msg_content)

.setTitle(msg_title)

.build())*/

.setOptions(Options.newBuilder()

.setApnsProduction(false)//true-推送生产环境 false-推送开发环境(测试使用参数)

.setTimeToLive(time)//消息在JPush服务器的失效时间(测试使用参数)

.build())

.build();

}

/**

* 极光推送方法(采用java SDK)

* @param alias

* @param alert

* @param url

* @return PushResult

*/

private static PushResult push(String[] arr,String notification_title, String msg_title, String msg_content, String url ,String accptMd){

PushResult pushResult = new PushResult();

ClientConfig clientConfig = ClientConfig.getInstance();

JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);

PushPayload payload = null;

if("2".equals(accptMd)){

payload = buildPushObject_ios_alias_alert(arr,notification_title,msg_title,msg_content,url);

}else if("3".equals(accptMd)){

payload = buildPushObject_android_alias_alert(arr,notification_title,msg_title,msg_content,url);

}else if("23".equals(accptMd)){

payload = buildPushObject_android_ios_alias_alert_appoint_device(arr, notification_title, msg_title, msg_content, url);

}else if("all".equals(accptMd)){

payload = buildPushObject_android_ios_alias_alert(arr,notification_title,msg_title,msg_content,url);

}

try {

pushResult = jpushClient.sendPush(payload);

} catch (APIConnectionException e) {

log.error("Connection error. Should retry later. ", e);

} catch (APIRequestException e) {

log.error("Error response from JPush server. Should review and fix it. ", e);

log.info("HTTP Status: " + e.getStatus());

log.info("Error Code: " + e.getErrorCode());

log.info("Error Message: " + e.getErrorMessage());

log.info("Msg ID: " + e.getMsgId());

}

return pushResult;

}

}

调用:

//根据客户查询 ID

JGpustDto dtoDec = jGpushMapper.qryDeviceNo("");

String[] devideNoStr = new String[0];

List devideNoList = new ArrayList();

devideNoList.add("");

devideNoStr = devideNoList.toArray(new String[devideNoList.size()]);

// 推送

PushResult result = JiguangPush.jiguangPush(devideNoStr, "测试", "测试", "测试", "url", "23");

/**

* 对推送结果入库

*/

if (result != null && result.isResultOK()) {

JGpustDto dto = new JGpustDto();

dto.setStatus("0");

dto.setContent("");

dto.setCustno("");

dto.setDeviceno("");

dto.setTitle("");

dto.setUrl("");

} else {

JGpustDto dto = new JGpustDto();

dto.setStatus("1");

dto.setContent("");

dto.setCustno("");

dto.setDeviceno("");

dto.setTitle("");

dto.setUrl("");

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值