SSL error while implementing Apple Push Notification

apple push notification not received on device
apple push notification test
apple push services certificate
gateway.push.apple.com 2195
apns sandbox
apns-push-type
apns curl
devicetokennotfortopic

I am trying to implement Apple Push Notification using python and django.

i am using following library to implement it

http://leepa.github.com/django-iphone-push/

Here is my code that create that send the message

from django.http import HttpResponse
from django.utils import simplejson
import json
from push.models import iPhone

def SendMessage(request,data):

        t = iPhone('XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX ') # 64 digit token
        t.send_message("hi") # at this line i am getting ERROR
        return HttpResponse(data,mimetype='application/javascript')

settings.py

import os
PROJECT_ROOT = '/'

# Full path to the APN Certificate / Private Key .pem
IPHONE_SANDBOX_APN_PUSH_CERT = os.path.join(PROJECT_ROOT, "apns-dev-tubeteam.pem")
IPHONE_LIVE_APN_PUSH_CERT = os.path.join(PROJECT_ROOT, "apns-dev-tubeteam.pem")

# Set this to the hostname for the outgoing push server
IPHONE_SANDBOX_APN_HOST = 'gateway.sandbox.push.apple.com'
IPHONE_LIVE_APN_HOST = 'gateway.push.apple.com'

# Set this to the hostname for the feedback server
IPHONE_SANDBOX_FEEDBACK_HOST = 'feedback.sandbox.push.apple.com'
IPHONE_LIVE_FEEDBACK_HOST = 'feedback.push.apple.com'

Error

[Errno 336265218] _ssl.c:337: error:140B0002:SSL routines:SSL_CTX_use_PrivateKey_file:system lib

Can anyone please do tell me how to get rid off it.

I had the exact same problem. Turns out it was a simple error - I had a mistake in IPHONE_SANDBOX_APN_PUSH_CERT and python could not locate my certificate. Once I pointed it to the right location, it started working.

Note that you might want to double-check your certificate first using openssl command line, such as:

openssl x509 -text -in cert.pem

That will give you textual information about your certificate, its validity, etc.

Also, double-check file permissions of the certificate file (the python process must have sufficient rights to access it).

Troubleshooting Push Notifications, Apple Push Notification service (APNs) provides a response to each POST There was an error with the certificate or with the provider's authentication token. 405 also implement the appropriate failure handler methods to determine if APNs  Add your Apple Push Notification service key Enter your team ID as shown on the Apple developer website; Enter the key ID as copied during the previous step; 3 Option B: Using an SSL certificate. Follow the steps below to obtain and set up an Apple Push Notifications service SSL certificate from Apple. 3.1 Signing in to the Apple Developer Center

In my case, what worked for me is like below:

Use the full path like

apns = APNs(use_sandbox=True, cert_file='/usr/local/etc/cert.pem', key_file='/usr/local/etc/key.pem')

rather than

apns = APNs(use_sandbox=True, cert_file='cert.pem', key_file='key.pem')

APNs Overview, 3.6 ) extension in the certificate to identify the topics for your push notifications. For example, if you provide an app with the bundle ID com. Well i do have a .pem file and i have specified the path of that file now i am not aware about the setting up the certificates , can you please tell me in detail – Hunt Apr 26 '11 at 13:24

my solution was that when creating my .pem file i set a blank password and assumed it meant no password. so the server was still expecting to use a password. i had to manually remove the password.

here is a little how to guide if it helps anyone:

NOTE: need to follow directions from apple’s developer website to create certificate first then export the .p12 file, by exporting the embedded private key that is created (in ‘keychain access’), NOT the actual certificate ———————————————————————————————————— ———————————————————————————————————— FOR DEVELOPMENT CERT: After getting the p12 file, it needs to be converted to the PEM format by executing this command from the terminal: $ openssl pkcs12 -clcerts -nokeys -out apns-dev-cert.pem -in apns_dev.p12 $ openssl pkcs12 -nocerts -out apns-dev-key.pem -in apns_dev.p12

If you wish to remove the passphrase execute the following: (NOTE: using a ‘blank’ password when exporting/converting, is still indeed setting a password, hence you should still execute the following if you intend to have no password) $ openssl rsa -in apns-dev-key.pem -out apns-dev-key-noenc.pem

Finally, you need to combine the key and cert files into a apns-dev.pem file we will use when connecting to APNS:

$ cat apns-dev-cert.pem apns-dev-key-noenc.pem > apns-dev.pem

———————————————————————————————————— FOR PRODUCTION CERT: After getting the p12 file, it needs to be converted to the PEM format by executing this command from the terminal: $ openssl pkcs12 -clcerts -nokeys -out apns-prod-cert.pem -in apns_prod.p12 $ openssl pkcs12 -nocerts -out apns-prod-key.pem -in apns_prod.p12

If you wish to remove the passphrase execute the following: (NOTE: using a ‘blank’ password when exporting/converting, is still indeed setting a password, hence you should still execute the following if you intend to have no password) $ openssl rsa -in apns-prod-key.pem -out apns-prod-key-noenc.pem

Finally, you need to combine the key and cert files into a apns-dev.pem file we will use when connecting to APNS:

$ cat apns-prod-cert.pem apns-prod-key-noenc.pem > apns-prod.pem

Handling Notification Responses from APNs, Secure your communications with APNs by installing a certificate on your Select Apple Push Notification service SSL (Sandbox & Production) for the type and  To use Apple Push Notification Service (APNs), your macOS and iOS clients need a direct and persistent connection to Apple's servers. Your iPhone, iPad, or iPod touch might connect to APNs over cellular data (if capable) or Wi-Fi.

Try to use PyAPNs from https://github.com/simonwhitaker/PyAPNs or pip install apns

And be sure to fetch the APNs certificate and key from iOS provisioning portal, install and convert them to .pem files following this guide: http://jainmarket.blogspot.com/2009/11/generate-apple-push-notification.html

This library is quite strait-forward.

Communicating with APNs, I want to set up my app for push notification and am following the respective guide. When uploading I get the message "There was an internal server error when processing your Let me know if you face any issues while implementing push. Notifications. Local and push notifications are great for keeping users informed with timely and relevant content, whether your app is running in the background or inactive. Notifications can display a message, play a distinctive sound, or update a badge on your app icon.

USE THIS CODE:

#!/usr/bin/python2.7

import socket
import ssl
import json
import struct
import argparse



APNS_HOST = ( 'gateway.sandbox.push.apple.com', 2195 )


class Payload:
    PAYLOAD = '{"aps":{${MESSAGE}${BADGE}${SOUND}}}'
    def __init__(self):
        pass

    def set_message(self, msg):
        if msg is None:
            self.PAYLOAD = self.PAYLOAD.replace('${MESSAGE}', '')
        else:
            self.PAYLOAD = self.PAYLOAD.replace('${MESSAGE}', '"alert":"%s",' % msg)

    def set_badge(self, num):
        if num is None:
            self.PAYLOAD = self.PAYLOAD.replace('${BADGE}', '')
        else:
            self.PAYLOAD = self.PAYLOAD.replace('${BADGE}', '"badge":%s,' % num)

    def set_sound(self, sound):
        if sound is None:
            self.PAYLOAD = self.PAYLOAD.replace('${SOUND}', '')
        else:
            self.PAYLOAD = self.PAYLOAD.replace('${SOUND}', '"sound":"%s",' % sound)

    def toString(self):
        return (self.PAYLOAD.replace('${MESSAGE}','').replace('${BADGE}','').replace('${SOUND}',''))

def connectAPNS(host, cert):
    ssl_sock = ssl.wrap_socket( socket.socket( socket.AF_INET, socket.SOCK_STREAM ), certfile = cert )
    ssl_sock.connect( APNS_HOST )
    return ssl_sock

def sendNotification(sslSock, device, message, badge, sound):
    payload = Payload()
    payload.set_message(message)
    payload.set_badge(badge)
    payload.set_sound(sound)
    payloadAsStr = payload.toString()

    format = '!BH32sH%ds' % len(payloadAsStr)
    binaryDeviceToken = device.replace(' ','').decode('hex')
    binaryNotification = struct.pack( format, 0, 32, binaryDeviceToken, len(payloadAsStr), payloadAsStr )

    print ("sending payload: ["+payloadAsStr+"] as binary to device: ["+device+"]")
    sslSock.write(binaryNotification)

def printUsageAndExit():
    print("msg2ios - Version 0.1\nmsg2IOS.py -d <device> -m <message> -s[plays sound] -b <badgeint>  -c <certBundlePath>")
    exit(1)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-d', '--device')
    parser.add_argument('-m', '--message')
    parser.add_argument('-s', '--sound')
    parser.add_argument('-b', '--badge')
    parser.add_argument('-c', '--cert')
    args = parser.parse_args()

    if (args.device is None) or ((args.message is None) and (args.sound is None) and (args.badge is None)) or (args.cert is None):
        printUsageAndExit()

    sslSock = connectAPNS(APNS_HOST, args.cert)
    sendNotification(sslSock, args.device, args.message, args.badge, args.sound)
    sslSock.close()

Establishing a Certificate-Based Connection to APNs, How do I generate an SSL certificate for Apple push notifications? Configuring an App ID for push notification service. Check the Enable for Apple Push Notification service option and click the Configure button displayed to the right of the Development Push SSL Certificate. 2. You will now see the Apple Push Notification service SSL Certificate Assistant screen. Click Continue (see Figure 10). Figure 10. The

iOS push notification setup not accepting my certificate : Kinvey , In this article we will show you how to generate an APNS certificate for your application. Step 1: Creating a CSR from Your Keychain. On your MAC open Keychain  Today Apple has finally released a long awaited update to APNs (Apple Push Notification Service) APIs. Despite the lack of an official announcement, this has been a long awaited improvement. Despite the lack of an official announcement, this has been a long awaited improvement.

iOS, To enable an app to send push notifications to iOS devices (iPhone, iPad and You link the push certificate to the app when you publish the app for Generally, you upload your push certificate in Swrve when configuring the  For the files that shouldn't be accessible from the web, /home/user/PushChat should be fine. As for the issue accessing the the site, it is most likely a permissions issue. The www-data user runs the

Local and Remote Notification Programming Guide: APNs Overview, Push Notification Certificate Wizard. How does this work? Configuring Apple (​APNS) push notification certificates can be frustrating. We built this wizard to make  The SSL certificate for Apple push notifications is valid for 12 months from the date when it was issued by Apple. Fourteen days before the certificate expiry date, the Cognos® Mobile server starts logging warnings in the c10_location \logs\mob.log file about the approaching certificate expiry date.

Comments
  • Did you READ the error? Clearly there is a problem with your 64-digit token or your SSL certificate or both. Did you follow the instructions on setting up your certificates?
  • Well i do have a .pem file and i have specified the path of that file now i am not aware about the setting up the certificates , can you please tell me in detail
  • +1 "could not locate certificate". In my case, it was just a typo in the filename. Really feels like ssl.wrap_socket could throw up a simple "file not found" instead of letting the ssl error bubble up
  • It is also worth noting that apparently Apple might close the connection if you try to send too many push messages to unregistered devices (source: github.com/jleclanche/django-push-notifications/issues/…)
  • Some explanation here would be helpful.