2

I am having really hard time figuring out how to send messages from my server to the APNS. I have used Moon-APNS and APNS-Sharp and I am stuck at the same error which is "parameter is incorrect". I generated the p12 file using KeyChain. I dragged the file into my Win 7 virtual environment and put it inside the bin\debug folder. Here is the code for Moon-APNS:

 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");  
        }

Anyone has ideas?

Michaël
  • 6,676
  • 3
  • 36
  • 55
azamsharp
  • 19,710
  • 36
  • 144
  • 222

3 Answers3

3

I think this will help you :

OSX Keychain

After you've created the appropriate Push Notification Certificate in iPhone Developer Program Portal you should have downloaded a file named something like apn_developer_identity.cer. If you have not done so already, you should open/import this file into Keychain, into your login section.

Finally, if you filter Keychain to show your login container's Certificates, you should see your Certificate listed. Expand the certificate, and there should be a Key underneath/attached to it.

Right Click or Ctrl+Click on the appropriate Certificate and choose Export. Keychain will ask you to choose a password to export to. Pick one and remember it. You should end up with a .p12 file. You will need this file and the password you picked to use the Notification and Feedback Libraries here. OpenSSL

Here is how to create a PKCS12 format file using open ssl, you will need your developer private key (which can be exported from the keychain) and the CertificateSigningRequest??.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

I found Moon-APNS easier to use and configure on my app.

Alex
  • 274
  • 5
  • 15
1

Solved the problem by following the link:

http://code.google.com/p/apns-sharp/wiki/HowToCreatePKCS12Certificate

and then generating the .p12 file using the openssl approach.

azamsharp
  • 19,710
  • 36
  • 144
  • 222
0

Have you tried using APNS-Sharp example project to send notifications? I am using similar code, it works just fine...this is what one of my methods looks like

 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();


    }
Daniel
  • 22,363
  • 9
  • 64
  • 71
  • I solved my problem by generating the .p12 file using the openssl approach instead of using KeyChain. Here is the link I followed: http://code.google.com/p/apns-sharp/wiki/HowToCreatePKCS12Certificate – azamsharp Aug 16 '11 at 17:21