极光推送Java后台实现
来源:互联网 发布:网络超时请检查防火墙 编辑:程序博客网 时间:2024/06/05 21:01
1极光推送的几种方式:
- 广播(所有人)
- 注册ID(RegistrationID)
- 别名(alias)
- 标签(tag,分组)
- 用户分群(Segment)
2.引入jar包(这里使用maven方式)
<!-- 极光推送插件 -->
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.0.8</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.6.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<!-- For log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.0.8</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.6.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<!-- For log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- 极光推送插件 -->
3.JpushClientUtil类
private final static String APP_KEY = "ced5eace6c76dc60d701635";
private final static String MASTER_SECRET = "f58c70ec7aedac19f75956";
private final static String MASTER_SECRET = "f58c70ec7aedac19f75956";
private static JPushClient jPushClient = new JPushClient(MASTER_SECRET,APP_KEY);
public static int pushMsg(String alias,String title,String msgContent){
JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
int result = 0;
try {
//PushPayload pushPayload= JpushClientUtil.buildPushObject_all_registrationId_alertWithTitle(registrationId,notification_title,msg_title,msg_content,extrasparam);
//别名推送
PushPayload pushPayload= JpushClientUtil.buildPushObject_android_and_iosByAlias(alias, title, msgContent);
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;
}
/**
* registrationId推送
*/
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等字段
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("androidNotification extras key",extrasparam)
.build())
//指定当前推送的iOS通知
.addPlatformNotification(IosNotification.newBuilder()
//传一个IosAlert对象,指定apns title、title、subtitle等
.setAlert(notification_title)
.incrBadge(1)
.setSound("sound.caf")
.addExtra("iosNotification extras key",extrasparam)
.build())
.build())
.setMessage(Message.newBuilder()
.setMsgContent(msg_content)
.setTitle(msg_title)
.addExtra("message extras key",extrasparam)
.build())
.setOptions(Options.newBuilder()
.setApnsProduction(false)
.setSendno(1)
.setTimeToLive(86400)
.build())
.build();
}
/**
* 标签推送
*/
public static PushPayload buildPushObject_android_and_iosByTag(String tag,String title,String content) {
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.tag(tag))
.setNotification(Notification.newBuilder()
.setAlert(content)
.addPlatformNotification(AndroidNotification.newBuilder()
.setTitle(title).build())
.addPlatformNotification(IosNotification.newBuilder()
.incrBadge(1)
.addExtra(title, content).build())
.build())
.build();
}
/**
* 别名推送
*/
public static PushPayload buildPushObject_android_and_iosByAlias(String alias,String title,String content) {
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.alias(alias))
.setNotification(Notification.newBuilder()
.setAlert(content)
.addPlatformNotification(AndroidNotification.newBuilder()
.setTitle(title).build())
.addPlatformNotification(IosNotification.newBuilder()
.incrBadge(1)
.addExtra(title, content).build())
.build())
.build();
}
try {
//PushPayload pushPayload= JpushClientUtil.buildPushObject_all_registrationId_alertWithTitle(registrationId,notification_title,msg_title,msg_content,extrasparam);
//别名推送
PushPayload pushPayload= JpushClientUtil.buildPushObject_android_and_iosByAlias(alias, title, msgContent);
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;
}
/**
* registrationId推送
*/
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等字段
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("androidNotification extras key",extrasparam)
.build())
//指定当前推送的iOS通知
.addPlatformNotification(IosNotification.newBuilder()
//传一个IosAlert对象,指定apns title、title、subtitle等
.setAlert(notification_title)
.incrBadge(1)
.setSound("sound.caf")
.addExtra("iosNotification extras key",extrasparam)
.build())
.build())
.setMessage(Message.newBuilder()
.setMsgContent(msg_content)
.setTitle(msg_title)
.addExtra("message extras key",extrasparam)
.build())
.setOptions(Options.newBuilder()
.setApnsProduction(false)
.setSendno(1)
.setTimeToLive(86400)
.build())
.build();
}
/**
* 标签推送
*/
public static PushPayload buildPushObject_android_and_iosByTag(String tag,String title,String content) {
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.tag(tag))
.setNotification(Notification.newBuilder()
.setAlert(content)
.addPlatformNotification(AndroidNotification.newBuilder()
.setTitle(title).build())
.addPlatformNotification(IosNotification.newBuilder()
.incrBadge(1)
.addExtra(title, content).build())
.build())
.build();
}
/**
* 别名推送
*/
public static PushPayload buildPushObject_android_and_iosByAlias(String alias,String title,String content) {
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.alias(alias))
.setNotification(Notification.newBuilder()
.setAlert(content)
.addPlatformNotification(AndroidNotification.newBuilder()
.setTitle(title).build())
.addPlatformNotification(IosNotification.newBuilder()
.incrBadge(1)
.addExtra(title, content).build())
.build())
.build();
}
阅读全文
0 0
- 极光推送Java后台实现
- 极光推送经验之谈-Java后台服务器实现极光推送的两种实现方式
- 极光推送经验之谈-Java后台服务器实现极光推送的两种实现方式
- JAVA SDK 极光推送后台DEMO
- JPUSH后台极光推送
- 极光推送>>java SDK服务端集成后台项目
- 极光推送>>java SDK服务端集成后台项目
- 极光推送>>java SDK服务端集成后台项目
- 极光推送服务端实现
- Java 极光推送分组推送
- java 极光推送
- JAVA极光推送
- 极光推送java服务端
- 极光推送java服务端
- java极光推送实例
- iOS推送实现 + JAVA后台
- 极光推送服务器端代码(java服务器后台向手机端自定义推送消息)
- 极光后台服务器推送消息通知
- Oracle数据库导入导出总结
- GIL全局解释器锁(转)
- iOS网络——AFNetworking AFURLSessionManager源码解析
- 如何直接访问github上的项目
- 作业6
- 极光推送Java后台实现
- Date.parse()的兼容性问题
- 接收udp组播信息的方法
- 【转+修改】Secedit:命令行下操作组策略
- MongoDB+ES 学习
- 素数环
- MATLAB 霍夫直线检测
- 90.Subsets II leetcode java
- jdk7监听文件系统变化通知新特性