极光推送JAVA代码示例
一、 准备工作
1. 登录极光推送官网https://www.jpush.cn/,注册账号并登录
- package com.weiwend.jdpush;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import cn.jpush.api.JPushClient;
- import cn.jpush.api.common.resp.APIConnectionException;
- import cn.jpush.api.common.resp.APIRequestException;
- 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.audience.AudienceTarget;
- 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 Jdpush {
- protected static final Logger LOG = LoggerFactory.getLogger(Jdpush.class);
- // demo App defined in resources/jpush-api.conf
- public static final String TITLE = "申通快递";
- public static final String ALERT = "祝大家新春快乐";
- public static final String MSG_CONTENT = "申通快递祝新老客户新春快乐";
- public static final String REGISTRATION_ID = "0900e8d85ef";
- public static final String TAG = "tag_api";
- public static JPushClient jpushClient=null;
- public static void testSendPush(String appKey ,String masterSecret) {
- jpushClient = new JPushClient(masterSecret, appKey, 3);
- // HttpProxy proxy = new HttpProxy("localhost", 3128);
- // Can use this https proxy: https://github.com/Exa-Networks/exaproxy
- // For push, all you need do is to build PushPayload object.
- //PushPayload payload = buildPushObject_all_all_alert();
- //生成推送的内容,这里我们先测试全部推送
- PushPayload payload=buildPushObject_all_alias_alert();
- try {
- System.out.println(payload.toString());
- PushResult result = jpushClient.sendPush(payload);
- System.out.println(result+"................................");
- LOG.info("Got result - " + result);
- } 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());
- }
- }
- public static PushPayload buildPushObject_all_all_alert() {
- return PushPayload.alertAll(ALERT);
- }
- public static PushPayload buildPushObject_all_alias_alert() {
- return PushPayload.newBuilder()
- .setPlatform(Platform.all())//设置接受的平台
- .setAudience(Audience.all())//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到
- .setNotification(Notification.alert(ALERT))
- .build();
- }
- public static PushPayload buildPushObject_android_tag_alertWithTitle() {
- return PushPayload.newBuilder()
- .setPlatform(Platform.android())
- .setAudience(Audience.all())
- .setNotification(Notification.android(ALERT, TITLE, null))
- .build();
- }
- public static PushPayload buildPushObject_android_and_ios() {
- return PushPayload.newBuilder()
- .setPlatform(Platform.android_ios())
- .setAudience(Audience.tag("tag1"))
- .setNotification(Notification.newBuilder()
- .setAlert("alert content")
- .addPlatformNotification(AndroidNotification.newBuilder()
- .setTitle("Android Title").build())
- .addPlatformNotification(IosNotification.newBuilder()
- .incrBadge(1)
- .addExtra("extra_key", "extra_value").build())
- .build())
- .build();
- }
- public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
- return PushPayload.newBuilder()
- .setPlatform(Platform.ios())
- .setAudience(Audience.tag_and("tag1", "tag_all"))
- .setNotification(Notification.newBuilder()
- .addPlatformNotification(IosNotification.newBuilder()
- .setAlert(ALERT)
- .setBadge(5)
- .setSound("happy")
- .addExtra("from", "JPush")
- .build())
- .build())
- .setMessage(Message.content(MSG_CONTENT))
- .setOptions(Options.newBuilder()
- .setApnsProduction(true)
- .build())
- .build();
- }
- public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
- return PushPayload.newBuilder()
- .setPlatform(Platform.android_ios())
- .setAudience(Audience.newBuilder()
- .addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
- .addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))
- .build())
- .setMessage(Message.newBuilder()
- .setMsgContent(MSG_CONTENT)
- .addExtra("from", "JPush")
- .build())
- .build();
- }
- }
package com.weiwend.jdpush.servlet; import Java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sun.org.apache.xml.internal.serializer.utils.Utils; import com.weiwend.jdpush.Jdpush; import cn.jpush.*; /** * Servlet implementation class tuisong */ public class tuisong extends HttpServlet { private static final long serialVersionUID = 1L; private static final String appKey ="84cf5ee2099c654aa03a5d70"; private static final String masterSecret = "7cf23f25a41806d5fd29e3c5"; public tuisong() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Jdpush.testSendPush(appKey,masterSecret); System.out.println("sucess"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
<servlet> <servlet-name >tuisong</servlet-name> <servlet-class>com.weiwend.jdpush.servlet.tuisong</servlet-class> </servlet> <servlet-mapping> <servlet-name >tuisong</servlet-name> <url-pattern>/tuisong</url-pattern> </servlet-mapping>
<form action="tuisong" method="get"> <input type="text" name="username"></input> <input type="password" name="userpassword"></input> <br><input type="submit" value="denglu">
点击登录按钮,触发servlet。界面样式就不改了,我相信除了处女座的程序员,没有人会在意这个。
log4j:WARN No appenders could be found for logger (cn.jpush.api.common.connection.NativeHttpClient). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. {"platform":"all","audience":"all","notification":{"alert":"祝大家新春快乐"},"options":{"sendno":34919015,"apns_production":false}} {"msg_id":3270259240,"sendno":34919015}................................ sucess
发送失败的原因是因为我们的应用并没有绑定某一个具体的客户端应用,所以推送失败了。但是本地服务器与推送服务器之间的通讯是完好的。
极光推送JAVA代码示例的更多相关文章
- 极光推送java代码
package com.zheng.cms.web.jpush.util; import cn.jpush.api.JPushClient; import cn.jpush.api.common.AP ...
- JPush极光推送 Java调用服务器端API开发
极光推送是:使得开发者可以即时地向其应用程序的用户推送通知或者消息,与用户保持互动,从而有效地提高留存率,提升用户体验.简单的说就是通过JPush后台管理网站进行app消息的推送.可以让用户及时 ...
- 极光推送-Java后台实现方式一:Http API
Java后台实现极光推送有两种方式,一种是使用极光推送官方提供的推送请求API:https://api.jpush.cn/v3/push,另一种则是使用官方提供的第三方Java APIjar包,这里先 ...
- 极光推送>>java SDK服务端集成后台项目(使用详解)
PS:如果你是第一次用推送,那就直接按照我的步骤来,再去看官方文档,这样,更容易能理解操作流程.还有——-请耐心看 极光文档(java SDK)请参考 [ 极光文档 ] 步骤一: 首先,你必须在 [极 ...
- 总结:极光推送java服务端(1)
遇到的问题: 1.怎么用极光推送 2.极光推送发送失败报错 返回{ } 3.透传和推送区别以及怎么设置 我的解决方案: 问题1.极光推送类里面有不同的方法,需要发给那些人就调用相应的方法.有安卓.io ...
- 最新的极光推送服务器端代码(java服务器后台向手机端自定义推送消息)
一共两个类 一个Jdpush 一个JpushClientUtil 代码如下 注解都写的很清楚 package com.sm.common.ajpush; import org.slf4j.Log ...
- JPush极光推送Java服务器端API
// 对android和ios设备发送 JPushClient jpush = new JPushClient(masterSecret, appKey); // 对android和ios设备发送 ...
- JPush极光推送Java服务器端实例
import cn.jpush.api.JPushClient; import cn.jpush.api.common.resp.APIConnectionException; import cn.j ...
- 用JPUSH极光推送实现服务端向安装了APP应用的手机推送消息(C#服务端接口)
这次公司要我们做一个功能,就是当用户成功注册以后,他登录以后要收到消息,当然这个消息是安装了我们的手机APP应用的手机咯. 极光推送的网站的网址是:https://www.jpush.cn/ 极光推送 ...
随机推荐
- 【Zabbix】Zabbix-agent自动化脚本
zabbix-agent自动化脚本 作用:批量部署zabbix-agent.用于上百台虚拟机都可以被Zabbix监控. 脚本名:inst-agent.sh #!/bin/bash echo " ...
- .Net Core 实践 - 使用log4net记录日志(2)
实现目标:将log4net的相关操作封装成一个 .Net Standard类库 demo地址:https://github.com/PuzzledAlien/log4net_demo/tree/mas ...
- 为什么选择 Visual Studio Code
为什么选择 Visual Studio Code 你在 VS Code 中找到的每个功能都完成一项出色的工作,构建了一些简单的功能集,包括语法高亮.智能补全.集成 git 和编辑器内置调试工具等,将使 ...
- C#列表页面后台代码
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- Oracle 连接 另一个Oracle数据库 服务器连接
一.场景 两台不同的服务器A.B分别装有不同业务的oracle数据库,因业务需要,现需要将B中test表的数据,定时同步到A中. 二.实现 根据以上场景,我想到了oracle中的dblink, ...
- Java编程的逻辑 (51) - 剖析EnumSet
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- Java 内存模型 JMM 浅析
JMM简介 Java Memory Model简称JMM, 是一系列的Java虚拟机平台对开发者提供的多线程环境下的内存可见性.是否可以重排序等问题的无关具体平台的统一的保证.(可能在术语上与Java ...
- 关系型数据库中主键(primary key)和外键(foreign key)的概念。
刚接触关系型数据库的同学,会听过主键和外键的概念.这是关系型数据库的基本概念,需要清楚理解.今天我就以简洁的语言总结一下这个概念. 主键.一句话概括:一张表中,可以用于唯一标识一条记录的字段组(或者说 ...
- 【代码笔记】Web-CSS-CSS盒子模型
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- Android为TV端助力 MediaPlayer 错误代码(error code)总结 转载
public static final int MEDIA_ERROR_IO Added in API level 17 File or network related operation error ...