2011-08-16 105 views
2

我很難搞清楚如何從我的服務器向APNS發送消息。我已經使用了Moon-APNS和APNS-Sharp,並且我陷入了「參數不正確」的錯誤。我使用KeyChain生成了p12文件。我將文件拖入我的Win 7虛擬環境,並將其放入bin \ debug文件夾中。以下是Moon-APNS的代碼:使用Moon-APNS或APNS-Sharp的Apple推送通知

static void Main(string[] args) 
     { 
      var deviceToken = "21212d6fefebde4d317cab41afff65631b5a4d47e5d85da305ec610b4013e616"; 

      var payload = new NotificationPayload(deviceToken, "hello world"); 
      var notificationList = new List<NotificationPayload>() { payload }; 

      var push = new PushNotification(true, "PushNotificationTest.p12", "pushchat"); 
      var result = push.SendToApple(notificationList); 

      Console.WriteLine("Hello World"); 
     } 

任何人有想法嗎?

回答

0

您是否嘗試過使用APNS-Sharp示例項目發送通知?我使用類似的代碼,它工作得很好......這就是我的方法之一,看起來像

public void SendToSome(List<string> tokens) 
    { 
     //Variables you may need to edit: 
     //--------------------------------- 
     bool sandbox = true; 
     //Put your device token in here 


     //Put your PKCS12 .p12 or .pfx filename here. 
     // Assumes it is in the same directory as your app 
     string p12File = "Certificates.p12"; 

     //This is the password that you protected your p12File 
     // If you did not use a password, set it as null or an empty string 
     string p12FilePassword = ""; 



     //Number of milliseconds to wait in between sending notifications in the loop 
     // This is just to demonstrate that the APNS connection stays alive between messages 
     // int sleepBetweenNotifications = 15000; 


     //Actual Code starts below: 
     //-------------------------------- 

     string p12Filename = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, p12File); 

     NotificationService service = new NotificationService(sandbox, p12Filename, p12FilePassword, 1); 

     service.SendRetries = 5; //5 retries before generating notificationfailed event 
     service.ReconnectDelay = 5000; //5 seconds 

     service.Error += new NotificationService.OnError(service_Error); 
     service.NotificationTooLong += new NotificationService.OnNotificationTooLong(service_NotificationTooLong); 

     service.BadDeviceToken += new NotificationService.OnBadDeviceToken(service_BadDeviceToken); 
     service.NotificationFailed += new NotificationService.OnNotificationFailed(service_NotificationFailed); 
     service.NotificationSuccess += new NotificationService.OnNotificationSuccess(service_NotificationSuccess); 
     service.Connecting += new NotificationService.OnConnecting(service_Connecting); 
     service.Connected += new NotificationService.OnConnected(service_Connected); 
     service.Disconnected += new NotificationService.OnDisconnected(service_Disconnected); 

     //The notifications will be sent like this: 
     //  Testing: 1... 
     //  Testing: 2... 
     //  Testing: 3... 
     // etc... 
     for (int i = 0; i < tokens.Count; i++) 
     { 
      //Create a new notification to send 
      Notification alertNotification = new Notification(); 

      alertNotification.DeviceToken = tokens[i]; 
      alertNotification.Payload.Alert.Body = Text; 
      alertNotification.Payload.Sound = "default"; 
      alertNotification.Payload.Badge = 1; 

      //Queue the notification to be sent 
      if (service.QueueNotification(alertNotification)) 
       Console.WriteLine("Notification Queued!"); 
      else 
       Console.WriteLine("Notification Failed to be Queued!"); 

      //Sleep in between each message 
      if (i < tokens.Count) 
      { 
       // Console.WriteLine("Sleeping " + sleepBetweenNotifications + " milliseconds before next Notification..."); 
       // System.Threading.Thread.Sleep(sleepBetweenNotifications); 
      } 
     } 

     Console.WriteLine("Cleaning Up..."); 

     //First, close the service. 
     //This ensures any queued notifications get sent befor the connections are closed 
     service.Close(); 

     //Clean up 
     service.Dispose(); 


    } 
+0

我通過使用openssl方法而不是使用KeyChain生成.p12文件來解決我的問題。這裏是我遵循的鏈接:http://code.google.com/p/apns-sharp/wiki/HowToCreatePKCS12Certificate – azamsharp

3

我認爲這將有助於你:

OSX鑰匙扣

您已經創建了一個iPhone開發者計劃門戶網站相應的推送通知證書,你應該已經下載了一個類似apn_developer_identity.cer文件命名後。如果你還沒有這樣做,你應該打開/導入這個文件到鑰匙串,進入你的登錄部分。

最後,如果您過濾鑰匙串以顯示您的登錄容器的證書,您應該看到您的證書列出。展開證書,並在其下面/附加一個密鑰。

右鍵單擊或Ctrl +單擊相應的證書並選擇導出。鑰匙串會要求您選擇一個密碼導出。挑一個並記住它。你應該得到一個.p12文件。您需要此文件和您選擇的密碼才能在此處使用通知和反饋庫。 OpenSSL的

下面是如何使用開放SSL,你需要你的開發私有密鑰(這可以從鑰匙串導出)和CertificateSigningRequest ??創建一個PKCS12格式的文件。certSigningRequest

1. Convert apn_developer_identity.cer (der format) to pem: 

openssl x509 -in apn_developer_identity.cer -inform DER -out apn_developer_identity.pem -outform PEM} 

2. Next, Convert p12 private key to pem (requires the input of a minimum 4 char password): 

openssl pkcs12 -nocerts -out private_dev_key.pem -in private_dev_key.p12 

3. (Optional): If you want to remove password from the private key: 

openssl rsa -out private_key_noenc.pem -in private_key.pem 

4. Take the certificate and the key (with or without password) and create a PKCS#12 format file: 

openssl pkcs12 -export -in apn_developer_identity.pem -inkey private_key_noenc.pem -certfile CertificateSigningRequest??.certSigningRequest -name "apn_developer_identity" -out apn_developer_identity.p12 

我發現Moon-APNS更容易在我的應用中使用和配置。

+7

可能是因爲它是你自己的? –