直接提供工具类, 调用时直接调用其 sendToRegistrationId() 方法

  1 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.Message;
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.IosNotification;
import cn.jpush.api.push.model.notification.Notification; public class JpushClientUtil { private final static String appKey = "xxxxxxxxxxxxxxxxxxxxx自己填";
private final static String masterSecret = "xxxxxxxxxxxxxxxxxxxxxxx自己填";
private static JPushClient jPushClient = new JPushClient(masterSecret, appKey); /**
* 推送给设备标识参数的用户
*
* @param registrationId
* 设备标识
* @param notification_title
* 通知内容标题
* @param msg_title
* 消息内容标题
* @param msg_content
* 消息内容
* @param extrasparam
* 扩展字段
* @return 0推送失败,1推送成功
*/
public static int sendToRegistrationId(String registrationId, String notification_title, String msg_title,String msg_content, String extrasparam) {
int result = 0;
try {
PushPayload pushPayload = JpushClientUtil.buildPushObject_all_registrationId_alertWithTitle(registrationId,
notification_title, msg_title, msg_content, extrasparam);
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;
} private static PushPayload buildPushObject_all_registrationId_alertWithTitle(String registrationId,
String notification_title, String msg_title, String msg_content, String extrasparam) { System.out.println("----------buildPushObject_all_all_alert");
// 创建一个IosAlert对象,可指定APNs的alert、title等字段
// IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody("title",
// "alert body").build();
return PushPayload.newBuilder()
// 指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
.setPlatform(Platform.all())
// 指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration
// id
.setAudience(Audience.registrationId(registrationId))
// jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
.setNotification(Notification.newBuilder()
// 指定当前推送的android通知
.addPlatformNotification(AndroidNotification.newBuilder().setAlert(notification_title)
.setTitle(notification_title)
// 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
.addExtra("sss", extrasparam).build())
// 指定当前推送的iOS通知
.addPlatformNotification(IosNotification.newBuilder()
// 传一个IosAlert对象,指定apns title、title、subtitle等
.setAlert(notification_title)
// 直接传alert
// 此项是指定此推送的badge自动加1
.incrBadge(1)
// 此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
.setSound("sound.caf")
// 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
.addExtra("content", extrasparam).build())
// 此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
// 取消此注释,消息推送时ios将无法在锁屏情况接收
// .setContentAvailable(true)
.build())
// Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
.setMessage(Message.newBuilder() .setMsgContent(msg_content) .setTitle(msg_title) .addExtra("message extras key", extrasparam) .build()) .setOptions(Options.newBuilder()
// 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
.setApnsProduction(false)
// 此字段是给开发者自己给推送编号,方便推送者分辨推送记录
.setSendno(1)
// 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;
.setTimeToLive(86400) .build()) .build(); } }

java接入极光推送的更多相关文章

  1. Java整合极光推送 ( 简单 )

    Java 整合极光推送官方文档:https://github.com/jpush/jpush-api-java-client 这里记录一下简单的使用步骤:创建一个普通的 Maven 工程然后添加依赖 ...

  2. Flutter接入极光推送

    (1)搜索 https://pub.dartlang.org/packages/jpush_flutter ,安装插件,并且按照官方配置 /android/app/build.gradle andro ...

  3. 极光推送-Java后台实现方式一:Http API

    Java后台实现极光推送有两种方式,一种是使用极光推送官方提供的推送请求API:https://api.jpush.cn/v3/push,另一种则是使用官方提供的第三方Java APIjar包,这里先 ...

  4. 极光推送经验之谈-Java后台服务器实现极光推送的两种实现方式

    原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6439313.html Java后台实现极光推送有两种方式,一种是使用极光推送官方提供的推送请 ...

  5. 1、Android Studio集成极光推送(Jpush) 报错 java.lang.UnsatisfiedLinkError: cn.jpush.android.service.PushProtoco

    Android studio 集成极光推送(Jpush) (华为手机)报错, E/JPush: [JPushGlobal] Get sdk version fail![获取sdk版本失败!] W/Sy ...

  6. java 极光推送

    Web.xml配置文件 <context-param> <param-name>contextConfigLocation</param-name> <par ...

  7. JPush极光推送 Java调用服务器端API开发

       极光推送是:使得开发者可以即时地向其应用程序的用户推送通知或者消息,与用户保持互动,从而有效地提高留存率,提升用户体验.简单的说就是通过JPush后台管理网站进行app消息的推送.可以让用户及时 ...

  8. 极光推送JAVA代码示例

    一. 准备工作 1. 登录极光推送官网https://www.jpush.cn/,注册账号并登录 2. 创建应用 创建应用过程,详见百度经验:http://jingyan.baidu.com/arti ...

  9. 极光推送>>java SDK服务端集成后台项目(使用详解)

    PS:如果你是第一次用推送,那就直接按照我的步骤来,再去看官方文档,这样,更容易能理解操作流程.还有——-请耐心看 极光文档(java SDK)请参考 [ 极光文档 ] 步骤一: 首先,你必须在 [极 ...

随机推荐

  1. C高级 跨平台协程库

    1.0 协程库引言 协程对于上层语言还是比较常见的. 例如C# 中 yield retrun, lua 中 coroutine.yield 等来构建同步并发的程序. 本文就是探讨如何从底层实现开发级别 ...

  2. C基础 万能动态数组

    引言 - 动态数组切入 开发中动态类型无外乎list 或者 vector, 这里就是在C中实现vector结构容器部分. 对于C中使用的数据结构, 可以参照下面感觉很不错框架源码学习 , 感觉是< ...

  3. 【VI Script】你不知道的脚本编程

    前言 近期,小黑在写程序的时候,经常会遇到一些重复性的工作.尤其是在写到QMH(Queued Message Handler)程序时,经常需要创建UI界面上的一些控件引用,并且在程序中捆绑成簇使用. ...

  4. ASPxgridview 编辑列初始化事件

    在初始化编辑咧的时候,给其赋值或者是disable等等.... 贴上代码 protected void master_CellEditorInitialize(object sender, ASPxG ...

  5. P2327

    code[class*="language-"] { padding: .1em; border-radius: .3em; white-space: normal; backgr ...

  6. php上传文件常见错误

    今天在文件上传过程中遇到的文件上传不过去,和网页报错,最后经查看总结有以下几个方面 上传文件错误码 error=0 正常上传 error=1 上传的大小超过了input[type=file]的文件上传 ...

  7. 《深入浅出MyBatis技术原理与实战》——6. MyBatis的解析和运行原理

    MyBatis的运行分为两大部分,第一部分是读取配置文件缓存到Configuration对象,用以创建SqlSessionFactory,第二部分是SqlSession的执行过程. 6.1 涉及的技术 ...

  8. OpenCL学习笔记(二):并行编程概念理解

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入. 并行编程的需求是显而易见的,其 ...

  9. Scikit Learn: 在python中机器学习

    转自:http://my.oschina.net/u/175377/blog/84420#OSC_h2_23 Scikit Learn: 在python中机器学习 Warning 警告:有些没能理解的 ...

  10. 学习LoadRunner之C语言函数

    学习LoadRunner之C语言函数 Action() { /*strchr和strrchr的区别*/ /* char *strTest1="citms citms"; char ...