Skip to content

Commit 18e1be4

Browse files
author
Javen
committed
Add registrationID push support; clear some code.
1 parent 5d0c0c3 commit 18e1be4

File tree

4 files changed

+26
-106
lines changed

4 files changed

+26
-106
lines changed
Lines changed: 14 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package cn.jpush.api.examples;
22

3-
import java.util.HashMap;
4-
import java.util.Map;
5-
63
import org.slf4j.Logger;
74
import org.slf4j.LoggerFactory;
85

96
import cn.jpush.api.JPushClient;
7+
import cn.jpush.api.common.DeviceEnum;
108
import cn.jpush.api.push.CustomMessageParams;
11-
import cn.jpush.api.push.IOSExtra;
129
import cn.jpush.api.push.MessageResult;
1310
import cn.jpush.api.push.ReceiverTypeEnum;
1411
import cn.jpush.api.report.ReceivedsResult;
@@ -20,105 +17,33 @@ public class JPushClientExample {
2017
private static final String appKey ="dd1066407b044738b6479275";
2118
private static final String masterSecret = "2b38ce69b1de2a7fa95706ea";
2219

23-
private static JPushClient _client = null;
20+
private static final String msgTitle = "Test from API example";
21+
private static final String msgContent = "Test Test";
22+
private static final String registrationID = "0900e8d85ef";
2423

25-
/**
26-
* 保存离线的时长。秒为单位。最多支持10天(864000秒)。
27-
* 0 表示该消息不保存离线。即:用户在线马上发出,当前不在线用户将不会收到此消息。
28-
* 此参数不设置则表示默认,默认为保存1天的离线消息(86400秒)。
29-
*/
30-
private static long timeToLive = 60 * 60 * 24;
24+
private static JPushClient jpushClient = null;
3125

3226
public static void main(String[] args) {
33-
/*
34-
* Example1: 初始化,默认发送给android和ios,同时设置离线消息存活时间
35-
* jpush = new JPushClient(masterSecret, appKey, timeToLive);
36-
*/
37-
38-
/*
39-
* Example2: 只发送给android
40-
* jpush = new JPushClient(masterSecret, appKey, DeviceEnum.Android);
41-
*/
42-
43-
/*
44-
* Example3: 只发送给IOS
45-
* jpush = new JPushClient(masterSecret, appKey, DeviceEnum.IOS);
46-
*/
47-
48-
/*
49-
* Example4: 只发送给android,同时设置离线消息存活时间
50-
* jpush = new JPushClient(masterSecret, appKey, timeToLive, DeviceEnum.Android);
51-
*/
52-
53-
54-
_client = new JPushClient(masterSecret, appKey);
27+
jpushClient = new JPushClient(masterSecret, appKey, 0, DeviceEnum.Android, false);
5528

56-
/*
57-
* 是否启用ssl安全连接, 可选
58-
* 参数:启用true, 禁用false,默认为非ssl连接
59-
*/
60-
//jpush.setEnableSSL(true);
61-
62-
63-
//测试发送消息或者通知
6429
testSend();
6530
}
6631

6732
private static void testSend() {
68-
// 在实际业务中,建议 sendNo 是一个你自己的业务可以处理的一个自增数字。
69-
int sendNo = getRandomSendNo();
70-
String msgTitle = "Test from javen";
71-
String msgContent = "Test Test";
72-
73-
/*
74-
* IOS设备扩展参数,
75-
* 设置badge,设置声音
76-
*/
77-
Map<String, Object> extra = new HashMap<String, Object>();
78-
IOSExtra iosExtra = new IOSExtra(10, "WindowsLogonSound.wav");
79-
extra.put("ios", iosExtra);
80-
81-
/*
82-
* 通知、消息 两者区别。请参考:http://docs.jpush.cn/pages/viewpage.action?pageId=3309701
83-
*/
84-
85-
String regID = "050ffb64f67";
86-
8733
CustomMessageParams params = new CustomMessageParams();
88-
params.setReceiverType(ReceiverTypeEnum.TAG);
89-
params.setReceiverValue("nexus7_0986b893");
90-
MessageResult msgResult = _client.sendCustomMessage(msgTitle, msgContent, params, null);
91-
92-
//对所有用户发送消息。
93-
//MessageResult msgResult = jpush.sendCustomMessageWithAppKey(sendNo,msgTitle, msgContent);
94-
34+
params.setReceiverType(ReceiverTypeEnum.REGISTRATION_ID);
35+
params.setReceiverValue(registrationID);
36+
MessageResult msgResult = jpushClient.sendCustomMessage(msgTitle, msgContent, params, null);
9537

96-
//覆盖指定msgId的消息,msgId可以从msgResult.getMsgid()获取。
97-
//MessageResult msgResult = jpush.sendNotificationWithAppKey(sendNo, msgTitle, msgContent, 0, extra,msgResult.getMsgid());
98-
99-
10038
if (null != msgResult) {
101-
LOG.info("content - " + msgResult.responseResult.responseContent);
102-
System.out.println("服务器返回数据: " + msgResult.toString());
103-
} else {
104-
System.out.println("无法获取数据");
39+
LOG.info("responseContent - " + msgResult.responseResult.responseContent);
40+
LOG.info("msgResult - " + msgResult);
10541
}
106-
107-
ReceivedsResult rrr = _client.getReportReceiveds("1708010723,1774452771");
42+
43+
ReceivedsResult rrr = jpushClient.getReportReceiveds("1708010723,1774452771");
10844
LOG.info("content - " + rrr.responseResult.responseContent);
10945
LOG.debug("Received - " + rrr);
11046
}
11147

112-
public static final int MAX = Integer.MAX_VALUE;
113-
public static final int MIN = (int) MAX/2;
114-
115-
/**
116-
* 保持 sendNo 的唯一性是有必要的
117-
* It is very important to keep sendNo unique.
118-
* @return sendNo
119-
*/
120-
public static int getRandomSendNo() {
121-
return (int) (MIN + Math.random() * (MAX - MIN));
122-
}
123-
12448
}
49+

src/cn/jpush/api/push/IOSExtra.java renamed to src/cn/jpush/api/push/IosExtras.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
package cn.jpush.api.push;
22

33
/*
4-
* IOS 发送通知 附加扩展类
4+
* Should be set into Notification extras with key "ios"
55
*/
6-
public class IOSExtra {
6+
public class IosExtras {
77

8-
public IOSExtra(int badge, String sound) {
8+
public IosExtras(int badge, String sound) {
99
this.badge = badge;
1010
this.sound = sound;
1111
}
1212

13-
public IOSExtra(String sound) {
13+
public IosExtras(String sound) {
1414
this.sound = sound;
1515
}
1616

17-
public IOSExtra(int badge) {
17+
public IosExtras(int badge) {
1818
this.badge = badge;
1919
}
2020

2121
/*
2222
* Badge Notification,默认是(0)
2323
*/
2424
private int badge = 0;
25+
2526
/*
2627
* 当前软件里面的所拥有的铃声名称(如:message.wav)。
2728
* 不设置,手机默认通知铃声

src/cn/jpush/api/push/ReceiverTypeEnum.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
package cn.jpush.api.push;
22

33
public enum ReceiverTypeEnum {
4-
//指定的 IMEI。此时必须指定 appKeys。
54
IMEI(1),
6-
7-
//指定的 tag。
85
TAG(2),
9-
10-
//指定的 alias。
116
ALIAS(3),
12-
13-
//对指定appkeys 的所有用户推送消息。
14-
APP_KEY(4);
7+
APP_KEY(4),
8+
REGISTRATION_ID(5);
159

1610
private final int value;
1711
private ReceiverTypeEnum(final int value) {

test/cn/jpush/api/PushFunctionTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.junit.Test;
99

1010
import cn.jpush.api.common.DeviceEnum;
11-
import cn.jpush.api.push.IOSExtra;
11+
import cn.jpush.api.push.IosExtras;
1212
import cn.jpush.api.push.MessageResult;
1313
import cn.jpush.api.push.NotificationParams;
1414
import cn.jpush.api.push.ReceiverTypeEnum;
@@ -43,7 +43,7 @@ public void sendNotificationAll_android(){
4343
public void sendNotificationAll_ios(){
4444
HashMap<String, Object> extra = new HashMap<String, Object>();
4545
extra.put("jpush-key","jpush-value");
46-
IOSExtra iosExtra = new IOSExtra(1,"test.mp3");
46+
IosExtras iosExtra = new IosExtras(1,"test.mp3");
4747
extra.put("ios", iosExtra);
4848

4949
NotificationParams params = new NotificationParams();
@@ -67,7 +67,7 @@ public void sendNotificationWithAlias() {
6767
public void sendNotificationWithAlias_ios(){
6868
HashMap<String, Object> extra = new HashMap<String, Object>();
6969
extra.put("jpush-key","jpush-value");
70-
IOSExtra iosExtra = new IOSExtra(1,"test.mp3");
70+
IosExtras iosExtra = new IosExtras(1,"test.mp3");
7171
extra.put("ios", iosExtra);
7272

7373
NotificationParams params = new NotificationParams();
@@ -92,7 +92,7 @@ public void sendNotificationWithTag(){
9292
public void sendNotificationWithTagByExtra(){
9393
HashMap<String, Object> extra = new HashMap<String, Object>();
9494
extra.put("jpush-key","jpush-value");
95-
IOSExtra iosExtra = new IOSExtra(1,"test.mp3");
95+
IosExtras iosExtra = new IosExtras(1,"test.mp3");
9696
extra.put("ios", iosExtra);
9797

9898
NotificationParams params = new NotificationParams();

0 commit comments

Comments
 (0)