準備工做以前得準備三個文件:一個是CSR請求文件(CertificateSigningRequest)、一個是aps_development.cer的SSL證書文件、還有一個是推送的PushDeveloper.p12祕鑰文件。以下圖所示:php
到蘋果開發官網描述下載證書,以開發證書爲案例。以下圖所示:java
打開鑰匙串訪問,找到咱們的專用密鑰(專用祕鑰就是咱們下載推送證書安裝本地的私人密鑰),以下圖所示:ios
導出.p12文件的時候須要輸入密碼,我這裏選擇簡單點爲123456,這密碼須要記住,後面有用到。git
上面步驟完成後打開終端 cd到p12目錄,就是放那三個文件所在的文件夾目錄github
1.把aps_development.cer的SSL證書轉換爲PushChatCert.pem文件,執行命令:bash
openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem
複製代碼
在p12目錄下會生成一個PushChatCert.pem文件app
2.把PushDeveloper.p12格式的私鑰轉化成PushChatKey.pem,須要設置密碼,密碼爲abc123ide
openssl pkcs12 -nocerts -out PushChatKey.pem -in PushDeveloper.p12
複製代碼
3.用certificate和PushChatKey.pem建立apns_developer_identity.p12文件(java後臺配置用到的),須要輸入密語:abc123函數
openssl pkcs12 -export -in PushChatCert.pem -inkey PushChatKey.pem -certfile CertificateSigningRequest.certSigningRequest -name "apns_developer_identity" -out apns_developer_identity.p12
複製代碼
ios工程測試測試
在AppDelegate裏didFinishLaunchingWithOptions函數裏寫
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}else{
//這裏仍是原來的代碼
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSLog(@"regisger success:%@", [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">"withString:@""]stringByReplacingOccurrencesOfString:@" "withString:@""]);
}
//前臺收到消息
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
// 處理推送消息
NSLog(@"userinfo:%@",userInfo);
NSLog(@"收到推送消息:%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Registfail%@",error);
NSLog(@"DeviceToken獲取失敗,緣由:%@",error);
}
複製代碼
java後臺代碼
public static void main(String[] args) throws Exception{
int badge = 1; // 圖標小紅圈的數值
String sound = "default"; // 鈴音
String msgCertificatePassword = "123456";//導出證書時設置的密碼
//90fb73e94659a1822caa51ca079734de6a7e60e44f260a1bfd1326bb4648d734
String deviceToken = "a1f52f971ca720d6ffc98ce7212c74edf347234ab2cc34fefcb541314e2e13e1"; //手機設備token號
String message = "test push message to ios device11111111111";
List<String> tokens = new ArrayList<String>();
tokens.add(deviceToken);
//java必需要用導出p12文件 php的話是pem文件
String certificatePath = "/Users/xxxx/Desktop/p12/apns_developer_identity.p12";
boolean sendCount = true;
PushNotificationPayload payload = new PushNotificationPayload();
payload.addAlert(message); // 消息內容
payload.addBadge(badge);
//payload.addCustomAlertBody(msgEX);
if (null == sound || "".equals(sound)) {
payload.addSound(sound);
}
PushNotificationManager pushManager = new PushNotificationManager();
// false:表示的是產品測試推送服務 true:表示的是產品發佈推送服務
pushManager.initializeConnection(new AppleNotificationServerBasicImpl(
certificatePath, msgCertificatePassword, false));
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==========消息推送完畢");
}
複製代碼
您的pom.xml添加如下依賴項:
<dependency>
<groupId>com.github.fernandospr</groupId>
<artifactId>javapns-jdk16</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
複製代碼