Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apple Push Notifications (APN) service server side Python 2 script in iOS

I have configured my iOS application with Apple Push Notification (APN) service enabled. I was able to send notifications to devices with PHP and Python 3 scripts. I tested both on local server with local machine. But now I need to write the script in Python2.

Below is the script I've written and when I run this I get nothing. Neither a notification to my device nor error in command line.

import socket, ssl, json, struct
import binascii
import os

deviceToken = 'my_device_tocken_without_spaces' 

thePayLoad = {
     'aps': {
          'alert':'My first push notification!',
          'sound':'default'
          }
     }

theCertfile = 'ck3_2.pem'

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

data = json.dumps( thePayLoad )

theFormat = '!BH32sH%ds' % len(data)

theNotification = struct.pack( theFormat, 0, 32, deviceToken, len(data), data )

ssl_sock = ssl.wrap_socket( socket.socket( socket.AF_INET, socket.SOCK_STREAM ), certfile = theCertfile )

ssl_sock.connect( theHost )

ssl_sock.write( theNotification )

ssl_sock.close()

What did I miss? How can I check where is the mistake happen?

I ran PHP script in localhost using XAMPP and I ran Python script in command line because I was unable to set-up Python with XAMPP which I already posted a question here.

like image 293
AnujAroshA Avatar asked Sep 15 '26 07:09

AnujAroshA


1 Answers

you may consider https://github.com/djacobs/PyAPNs that wrapped lot of useful features, including:

  • error handling
  • support enhanced message format and auto resend messages which are sent before error response
  • non-blocking ssl socket connection with great performance
like image 188
Jim Horng Avatar answered Sep 17 '26 20:09

Jim Horng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!