首先要準備一個iOS 開發者帳號,後來進Apple developer登入,請到Certificates, Identifiers & Profiles->Identifiers->App IDs
點Edit,移到Push Notifications
1. Development SSL Certificate(開發時用)
2. Production SSL Certificate(上線後用)
在點Create Certificate...
這裡我們要先產生CertificateSigningRequest.certSigningRequest
去Launchpad 找 Key Access,點開,新增完存在你要存的地方
在回到Apple Developer上傳CertificateSigningRequest.certSigningRequest,在下載aps_development.cer,這邊我是選擇 Development
下載完後aps_development.cer點兩下,去Key Access,產生.p12檔(這個檔還不是push server要用),並將檔案輸出,密碼請記下來,這邊我把檔案命名deveploer.p12
目前有三個檔案
請打開終端機,以下三個指令產生檔案,請確認是否有裝openssl
openssl x509 -in aps_development.cer -inform der -out aps.pem
openssl pkcs12 -nocerts -out key.pem -in push.p12
openssl pkcs12 -export -in aps.pem -inkey key.pem -name "push" -out push.p12
就是push server需要的push.p12
二、Swift 4 Code:
以下是需要在你App新增的程式碼AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. let center = UNUserNotificationCenter.current() center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in // Enable or disable features based on authorization. } application.registerForRemoteNotifications() return true } func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)}) print(deviceTokenString) } func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { print("i am not available in simulator \(error)") }
四、Javanps:
import java.util.ArrayList; import java.util.List; import org.json.JSONException; import javapns.communication.exceptions.CommunicationException; import javapns.communication.exceptions.KeystoreException; import javapns.devices.Device; import javapns.devices.exceptions.InvalidDeviceTokenFormatException; import javapns.devices.implementations.basic.BasicDevice; import javapns.notification.AppleNotificationServerBasicImpl; import javapns.notification.PushNotificationManager; import javapns.notification.PushNotificationPayload; import javapns.notification.PushedNotification; public class Main { public static void main(String[] args) throws JSONException, CommunicationException, KeystoreException, InvalidDeviceTokenFormatException { int badge = 1; String sound = "default"; String msgCertificatePassword = "123456"; //90fb73e94659a1822caa51ca079734de6a7e60e44f260a1bfd1326bb4648d734 String deviceToken = "31A7DA03C54A9AAE77E9E0CA01F4B138ABDFDE9712C2FC56738AD92E614B90FE"; String message = "test push message to ios device11111111111"; Listtokens = new ArrayList (); tokens.add(deviceToken); String certificatePath = "C:\\\\push.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(); pushManager.initializeConnection(new AppleNotificationServerBasicImpl( certificatePath, msgCertificatePassword, false)); List notifications = new ArrayList (); if (sendCount) { Device device = new BasicDevice(); device.setToken(deviceToken); PushedNotification notification = pushManager.sendNotification( device, payload, true); notifications.add(notification); } else { List devices = new ArrayList (); for (String token : tokens) { devices.add(new BasicDevice(token)); } notifications = pushManager.sendNotifications(payload, devices); } List failedNotification = PushedNotification .findFailedNotifications(notifications); List successfulNotification = PushedNotification .findSuccessfulNotifications(notifications); int failed = failedNotification.size(); int successful = successfulNotification.size(); System.out.println("zsl=1¡G" + successful); System.out.println("zsl=2¡G" + failed); pushManager.stopConnection(); System.out.println("zsl=3"); } }