close
很多人應該在尋找如何使用iOS的SNS的方式傳送訊息給apple的device,今天為大家介紹如何使用它。
先從iOS Certificate下載apn.cer,可選擇Developtment 或是 Production,記得必須支援"Apple Push Services"的功能
步驟1:下載完成後,將檔案轉成.pem的格式
openssl x509 -in apn.cer -inform DER -out apn.pem -outform PEM
步驟2:此檔案在Mac OS在Keychain Access所產生的私鑰.p12,密碼例如:"password123",並將此檔案匯出後轉成.pem
openssl pkcs12 -nocerts -in mykey.p12 -out mykey.pem
步驟3:最後再將兩個檔案結合即完成,結合時會需要輸入步驟2產生的私鑰的密碼
openssl pkcs12 -export -inkey mykey.pem -in apn.pem -out java_use_dev.p12
Maven
<dependency> <groupId>com.github.fernandospr</groupId> <artifactId>javapns-jdk16</artifactId> <version>2.4.0</version> </dependency>
Java 程式碼
PushNotificationPayload payload = PushNotificationPayload.complex(); payload.addAlert("Hello World"); payload.addBadge(1); payload.addSound("default"); final List<PushedNotification> NOTIFICATIONS = Push.payload(payload, "java_use_dev.p12", "password123(私鑰密碼)", true, "49fdb29f6948fe1dca8d21e319fa6ebce45a3127cff91498afb4227bc7b56180(iOS產生的Token)"); for (PushedNotification notification : NOTIFICATIONS) { if (!notification.isSuccessful()) { final String invaleToken = notification.getDevice().getToken(); final Exception error = notification.getException(); error.printStackTrace(); final ResponsePacket response = notification.getResponse(); if (response != null) { ActionLog.getInstance().error(error, response.getMessage()); } } }
文章標籤
全站熱搜