直接提供工具类, 调用时直接调用其 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. Oracle 集合

    --合并(UNION.UNION ALL) select * from empwhere ename like '%A%'unionselect * from empwhere ename like ...

  2. linux和性能相关的命令及系统性能诊断

    常用的和性能有关的命令 Iostat/vmstat/top/mpstat/time/strace/ipcs/ipcrm/ifconfig/tethereal/netstat/free/uptime 关 ...

  3. java中的抽象方法与抽象类

    在继承时,会遇到一个问题.如果很多子类都要继承父类的一个方法,但是实现的逻辑都不一样. 这时候父类只提供了方法名,但是没有具体的方法体. 例如,男孩类和女孩类都继承人类这一个父类.人类有爱好这个方法, ...

  4. SQL Server 2008的MSSQLSERVER 请求失败或服务未及时响应

    我的是SQL server 2008R2, 以前可以正常的启动SQL server(SQLEXPRESS).SQL server(MSSQLSERVER),有几天没有打开了,就在昨天 开机之后就无法启 ...

  5. 今天开始学模式识别与机器学习(PRML),章节5.1,Neural Networks神经网络-前向网络。

    今天开始学模式识别与机器学习Pattern Recognition and Machine Learning (PRML),章节5.1,Neural Networks神经网络-前向网络. 话说上一次写 ...

  6. python 用abc模块构建抽象基类Abstract Base Classes

    见代码: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/08/01 16:58 from abc import ABCMet ...

  7. 邂逅Sass和Compass之Sass篇

    对于一个从后台转到前端的web开发者来说,最大的麻烦就是写CSS,了解CSS的人都知道,它可以开发网页样式,但是没法用它编程,感觉耦合性相当的高,如果想要方便以后维护,只能逐句修改甚至重写相当一部分的 ...

  8. 【剑指offer】面试题 64. 求 1+2+3+...+n

    面试题 64. 求 1+2+3+...+n 题目:求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 1.采 ...

  9. POJ 2318 TOYS(点与直线的关系 叉积&&二分)

    题目链接 题意: 给定一个矩形,n个线段将矩形分成n+1个区间,m个点,问这些点的分布. 题解: 思路就是叉积加二分,利用叉积判断点与直线的距离,二分搜索区间. 代码: 最近整理了STL的一些模板,发 ...

  10. zoj 4020 The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light(广搜)

    题目链接:The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light 题解: 题意 ...