javapns 推送消息过程:(我这边是PC发送)
- 客户端(即PC)发送一个请求
- 我们自己的服务端接收请求
- 服务端通过apns,往苹果服务器发送消息推送请求
- 苹果服务器收到请求,根据设备devicetoken,然后往设备发送消息,并返回结果给我们自己的服务端
- 我们服务端返回结果给客户端
需要jar包,我这有现成的
javapns jar 下载
jar包maven地址:
<dependency>
<groupId>com.github.fernandospr</groupId>
<artifactId>javapns-jdk16</artifactId>
<version>2.3.1</version>
</dependency>
下面请看代码 (注意一下 pushManager.initializeConnection)的第三个参数
package pushmessage;
import java.util.ArrayList;
import java.util.List;
import javapns.devices.Device;
import javapns.devices.implementations.basic.BasicDevice;
import javapns.notification.AppleNotificationServerBasicImpl;
import javapns.notification.PushNotificationManager;
import javapns.notification.PushNotificationPayload;
import javapns.notification.PushedNotification;
public class PushMsg {
public static void main(String[] args) throws Exception{
System.out.println("zsl==========开始推送消息");
int badge = 1;
String sound = "default";
String msgCertificatePassword = "12345678";
String deviceToken = "6846f471b699868036547869977935cbf98df3805ecb2c1b7e7140b4fc962c94";
String message = "test push message to ios device";
List<String> tokens = new ArrayList<String>();
tokens.add(deviceToken);
String certificatePath = "F:/mdm.p12";
boolean sendCount = true;
PushNotificationPayload payload = new PushNotificationPayload();
payload.addAlert(message);
payload.addBadge(badge);
payload.addSound(sound);
PushNotificationManager pushManager = new PushNotificationManager();
pushManager.initializeConnection(new AppleNotificationServerBasicImpl(
certificatePath, msgCertificatePassword, true));
List<PushedNotification> notifications = new ArrayList<PushedNotification>();
if (sendCount) {
Device device = new BasicDevice();
device.setToken(deviceToken);
PushedNotification notification = pushManager.sendNotification(
device, payload, true);
notifications.add(notification);
} else {
List<Device> devices = new ArrayList<Device>();
for (String token : tokens) {
devices.add(new BasicDevice(token));
}
notifications = pushManager.sendNotifications(payload, devices);
}
List<PushedNotification> failedNotification = PushedNotification
.findFailedNotifications(notifications);
List<PushedNotification> successfulNotification = PushedNotification
.findSuccessfulNotifications(notifications);
int failed = failedNotification.size();
int successful = successfulNotification.size();
System.out.println("zsl==========成功数:" + successful);
System.out.println("zsl==========失败数:" + failed);
pushManager.stopConnection();
System.out.println("zsl==========消息推送完毕");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
直接运行main函数就ok,测试通过。
注意:该jar包依赖于 log4j.jar 所以你的项目里还应该有log4j的jar包,不然会报错,log4j jar 包不要错了。
log4j maven地址:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.11</version>
</dependency>