参考如下代码package com.imooc.test;import java.util.ArrayList;
import java.util.List;import javapns.notification.AppleNotificationServer;
import javapns.notification.AppleNotificationServerBasicImpl;
import javapns.notification.PayloadPerDevice;
import javapns.notification.PushNotificationPayload;
import javapns.notification.transmission.NotificationProgressListener;
import javapns.notification.transmission.NotificationThread;
import javapns.notification.transmission.NotificationThreads;public class PushTest2 {
public static void main(String[] args) {
String keystore = "G:/IOS_Certs/socketClient.p12";// 证书路径和证书名
String password = "111111"; // 证书密码
String token = "3675587a3d341e3e5525851b7fbdaeb4abbefba7d2305d5fda013f8d2a0d11e9";// 手机唯一标识
boolean production = false; // 设置true为正式服务地址,false为开发者地址
int threadThreads = 1; // 线程数
try {
// 建立与Apple服务器连接
AppleNotificationServer server = new AppleNotificationServerBasicImpl(
keystore, password, production);
List<PayloadPerDevice> list = new ArrayList<PayloadPerDevice>();
PushNotificationPayload payload = new PushNotificationPayload();
payload.addAlert("推送内容");
payload.addSound("default");// 声音
payload.addBadge(1);// 图标小红圈的数值
payload.addCustomDictionary("url", "www.baidu.com");// 添加字典
PayloadPerDevice pay = new PayloadPerDevice(payload, token);// 将要推送的消息和手机唯一标识绑定
list.add(pay); NotificationThreads work = new NotificationThreads(server, list,
threadThreads);//
work.setListener(DEBUGGING_PROGRESS_LISTENER);// 对线程的监听,一定要加上这个监听
work.start(); // 启动线程
work.waitForAllThreads();// 等待所有线程启动完成 } catch (Exception e) {
e.printStackTrace();
}
} // 线程监听
public static final NotificationProgressListener DEBUGGING_PROGRESS_LISTENER = new NotificationProgressListener() {
public void eventThreadStarted(NotificationThread notificationThread) {
System.out.println(" [EVENT]: thread #"
+ notificationThread.getThreadNumber() + " started with "
+ " devices beginning at message id #"
+ notificationThread.getFirstMessageIdentifier());
} public void eventThreadFinished(NotificationThread thread) {
System.out.println(" [EVENT]: thread #"
+ thread.getThreadNumber() + " finished: pushed messages #"
+ thread.getFirstMessageIdentifier() + " to "
+ thread.getLastMessageIdentifier() + " toward "
+ " devices");
} public void eventConnectionRestarted(NotificationThread thread) {
System.out.println(" [EVENT]: connection restarted in thread #"
+ thread.getThreadNumber() + " because it reached "
+ thread.getMaxNotificationsPerConnection()
+ " notifications per connection");
} public void eventAllThreadsStarted(
NotificationThreads notificationThreads) {
System.out.println(" [EVENT]: all threads started: "
+ notificationThreads.getThreads().size());
} public void eventAllThreadsFinished(
NotificationThreads notificationThreads) {
System.out.println(" [EVENT]: all threads finished: "
+ notificationThreads.getThreads().size());
} public void eventCriticalException(
NotificationThread notificationThread, Exception exception) {
System.out.println(" [EVENT]: critical exception occurred: "
+ exception);
}
};
}
import java.util.List;import javapns.notification.AppleNotificationServer;
import javapns.notification.AppleNotificationServerBasicImpl;
import javapns.notification.PayloadPerDevice;
import javapns.notification.PushNotificationPayload;
import javapns.notification.transmission.NotificationProgressListener;
import javapns.notification.transmission.NotificationThread;
import javapns.notification.transmission.NotificationThreads;public class PushTest2 {
public static void main(String[] args) {
String keystore = "G:/IOS_Certs/socketClient.p12";// 证书路径和证书名
String password = "111111"; // 证书密码
String token = "3675587a3d341e3e5525851b7fbdaeb4abbefba7d2305d5fda013f8d2a0d11e9";// 手机唯一标识
boolean production = false; // 设置true为正式服务地址,false为开发者地址
int threadThreads = 1; // 线程数
try {
// 建立与Apple服务器连接
AppleNotificationServer server = new AppleNotificationServerBasicImpl(
keystore, password, production);
List<PayloadPerDevice> list = new ArrayList<PayloadPerDevice>();
PushNotificationPayload payload = new PushNotificationPayload();
payload.addAlert("推送内容");
payload.addSound("default");// 声音
payload.addBadge(1);// 图标小红圈的数值
payload.addCustomDictionary("url", "www.baidu.com");// 添加字典
PayloadPerDevice pay = new PayloadPerDevice(payload, token);// 将要推送的消息和手机唯一标识绑定
list.add(pay); NotificationThreads work = new NotificationThreads(server, list,
threadThreads);//
work.setListener(DEBUGGING_PROGRESS_LISTENER);// 对线程的监听,一定要加上这个监听
work.start(); // 启动线程
work.waitForAllThreads();// 等待所有线程启动完成 } catch (Exception e) {
e.printStackTrace();
}
} // 线程监听
public static final NotificationProgressListener DEBUGGING_PROGRESS_LISTENER = new NotificationProgressListener() {
public void eventThreadStarted(NotificationThread notificationThread) {
System.out.println(" [EVENT]: thread #"
+ notificationThread.getThreadNumber() + " started with "
+ " devices beginning at message id #"
+ notificationThread.getFirstMessageIdentifier());
} public void eventThreadFinished(NotificationThread thread) {
System.out.println(" [EVENT]: thread #"
+ thread.getThreadNumber() + " finished: pushed messages #"
+ thread.getFirstMessageIdentifier() + " to "
+ thread.getLastMessageIdentifier() + " toward "
+ " devices");
} public void eventConnectionRestarted(NotificationThread thread) {
System.out.println(" [EVENT]: connection restarted in thread #"
+ thread.getThreadNumber() + " because it reached "
+ thread.getMaxNotificationsPerConnection()
+ " notifications per connection");
} public void eventAllThreadsStarted(
NotificationThreads notificationThreads) {
System.out.println(" [EVENT]: all threads started: "
+ notificationThreads.getThreads().size());
} public void eventAllThreadsFinished(
NotificationThreads notificationThreads) {
System.out.println(" [EVENT]: all threads finished: "
+ notificationThreads.getThreads().size());
} public void eventCriticalException(
NotificationThread notificationThread, Exception exception) {
System.out.println(" [EVENT]: critical exception occurred: "
+ exception);
}
};
}
解决方案 »
- iphone 开发入门问题
- home键退出应用后,再点击应用会调用哪个系统方法?
- 用于开发测试,iphone 4和iphone 4s 买哪个更合适???
- 新手 sqlite3 存储图片的问题
- iphone动画结束问题
- 求教可在iphone运行的jquery mobile小程序,论坛分+RMB感谢
- 介绍iOS程序的完整启动过程及原理
- 新手求助:为什么layoutSubview方法不调用?
- 根据图片的路径删除显示在view中的图片
- apns推送:ios推送消息中的emoji表表情发送不成功
- linker command failed with exit code 1 (use -v to see invocation)求解答,快哭了
- 关于XIB写个人中心
【估计卡住的人通常是和apns握手失败。这原因主要是java、.net和mac的ssl连接区别.
java需要把下载的文件再次转换才可以使用。】注意你用到的.p12并文件不是一开始从苹果官网下载过来的那个,而是要通过转换的,步骤如下:
(ps: socketClient.p12 是那个从苹果官网下载过来的,而最后使用的是apns_dev_key_for_java.p12)
1. openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem2. openssl pkcs12 -nocerts -out PushChatKey.pem -in socketClient.p123. openssl pkcs12 -export -in PushChatCert.pem -inkey PushChatKey.pem -certfile socketClient.certSigningRequest -name "apns_dev_key_for_java" -out apns_dev_key_for_java.p12