{"id":2313,"date":"2014-09-21T16:15:21","date_gmt":"2014-09-21T12:15:21","guid":{"rendered":"https:\/\/blog.alwawee.ru\/?p=2313"},"modified":"2014-09-22T17:19:30","modified_gmt":"2014-09-22T13:19:30","slug":"sending-ios-push-notification-using-python-module-pyapns","status":"publish","type":"post","link":"https:\/\/blog.alwawee.ru\/?p=2313","title":{"rendered":"Sending iOS Push Notifications using Python module PyAPNS"},"content":{"rendered":"<p><a href=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/09\/python-django.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/09\/python-django.png\" alt=\"python-django\" width=\"211\" height=\"192\" class=\"aligncenter size-full wp-image-2305\" \/><\/a><\/p>\n<p>I have written a small module, that sends a push notification using <strong>PyAPNS<\/strong>.<\/p>\n<p>This is how my <strong>sender.py<\/strong> module looks like:<\/p>\n<pre># -*- coding: utf-8 -*-\r\nfrom django.db import connection\r\nfrom apns import APNs, Frame, Payload\r\n\r\n\r\nclass Sender:\r\n\tdef send(self):\r\n\t\tapns = APNs(use_sandbox=True, cert_file='Cert.pem', key_file='Key.pem')\r\n\t\ttoken_hex = 'e4d4e96ec8ac3dd003e0129347b3421fe53ba49811b3803c4e99cfdc60293c94'\r\n\t\tmessage = \"Hello\"\r\n\t\tpayload = Payload(alert=message.decode('utf-8'), sound=\"default\", badge=0)\r\n\t\tapns.gateway_server.send_notification(token_hex, payload)\r\n<\/pre>\n<p>Here is a <strong>Sender<\/strong> class inside <strong>sender.py<\/strong> module. It has one method send. Inside this method I create an object of <strong>APNs<\/strong> class, create a payload and send it using this object.<br \/>\n<strong>Cert.pem<\/strong> and <strong>Key.pem<\/strong> files are inside the folder with this module. They can be created using <a title=\"http:\/\/www.raywenderlich.com\/32960\/apple-push-notification-services-in-ios-6-tutorial-part-1\" href=\"http:\/\/www.raywenderlich.com\/32960\/apple-push-notification-services-in-ios-6-tutorial-part-1\" target=\"_blank\">this tutorial<\/a>. Also that folder contains <strong>apns.py<\/strong> module file with<strong> PyAPNS <\/strong>code.<\/p>\n<p>To send a sample push notification from a command line I use following commands. First, I enter the folder with module. It is called <strong>notification_sender<\/strong>:<\/p>\n<pre>$ cd ~\/Documents\/common-app-server\/app_server\/notification_sender\r\n<\/pre>\n<p>Or on server:<\/p>\n<pre>\r\n# cd ~\/www\/den.ladby.ru\/public\/common-app-server\/app_server\/notification_sender\r\n<\/pre>\n<p>Then I enter to the python console:<\/p>\n<pre>$ python\r\n<\/pre>\n<p>Then I import <strong>Sender<\/strong> class from<strong> sender<\/strong> module:<\/p>\n<pre class=\"\">from sender import Sender\r\n<\/pre>\n<p>Create a <strong>sender<\/strong> object of a <strong>Sender<\/strong> class:<\/p>\n<pre>sender = Sender()\r\n<\/pre>\n<p>And finally send a notification:<\/p>\n<pre class=\"\">sender.send()\r\n<\/pre>\n<p>All you need to do to finish the job is to enter a <strong>PEM <\/strong>pass phrase:<\/p>\n<pre class=\"\">Enter PEM pass phrase:\r\n<\/pre>\n<p>Currently, the device token is hardcoded, I am planning to use <strong>SQLite<\/strong> entries with collected tokens. I use <strong>Django<\/strong> to collect tokens.<\/p>\n<p>I would like to add that if you change somehow the source file of a module, you should reenter python console and recreate object to make changes work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have written a small module, that sends a push notification using PyAPNS. This is how my sender.py module looks like: # -*- coding: utf-8 -*- from django.db import connection from apns import APNs, Frame, Payload class Sender: def send(self): apns = APNs(use_sandbox=True, cert_file=&#8217;Cert.pem&#8217;, key_file=&#8217;Key.pem&#8217;) token_hex = &#8216;e4d4e96ec8ac3dd003e0129347b3421fe53ba49811b3803c4e99cfdc60293c94&#8217; message = &#8220;Hello&#8221; payload = Payload(alert=message.decode(&#8216;utf-8&#8217;), sound=&#8221;default&#8221;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,74],"tags":[125,126,127,110],"class_list":["post-2313","post","type-post","status-publish","format-standard","hentry","category-ios","category-web-development","tag-apns","tag-push-notifications","tag-pyapns","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Sending iOS Push Notifications using Python module PyAPNS  - Denis Kutlubaev<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.alwawee.ru\/?p=2313\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sending iOS Push Notifications using Python module PyAPNS  - Denis Kutlubaev\" \/>\n<meta property=\"og:description\" content=\"I have written a small module, that sends a push notification using PyAPNS. This is how my sender.py module looks like: # -*- coding: utf-8 -*- from django.db import connection from apns import APNs, Frame, Payload class Sender: def send(self): apns = APNs(use_sandbox=True, cert_file=&#039;Cert.pem&#039;, key_file=&#039;Key.pem&#039;) token_hex = &#039;e4d4e96ec8ac3dd003e0129347b3421fe53ba49811b3803c4e99cfdc60293c94&#039; message = &quot;Hello&quot; payload = Payload(alert=message.decode(&#039;utf-8&#039;), sound=&quot;default&quot;, [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alwawee.ru\/?p=2313\" \/>\n<meta property=\"og:site_name\" content=\"Denis Kutlubaev\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/alwaweemobile\" \/>\n<meta property=\"article:published_time\" content=\"2014-09-21T12:15:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-09-22T13:19:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/09\/python-django.png\" \/>\n<meta name=\"author\" content=\"Denis Kutlubaev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Denis Kutlubaev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2313#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2313\"},\"author\":{\"name\":\"Denis Kutlubaev\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"headline\":\"Sending iOS Push Notifications using Python module PyAPNS\",\"datePublished\":\"2014-09-21T12:15:21+00:00\",\"dateModified\":\"2014-09-22T13:19:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2313\"},\"wordCount\":217,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2313#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blog.alwawee.ru\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/python-django.png\",\"keywords\":[\"APNS\",\"Push notifications\",\"PyAPNS\",\"Python\"],\"articleSection\":[\"iOS\",\"Web Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2313#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2313\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2313\",\"name\":\"Sending iOS Push Notifications using Python module PyAPNS - Denis Kutlubaev\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2313#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2313#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blog.alwawee.ru\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/python-django.png\",\"datePublished\":\"2014-09-21T12:15:21+00:00\",\"dateModified\":\"2014-09-22T13:19:30+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2313#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2313\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2313#primaryimage\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/python-django.png\",\"contentUrl\":\"https:\\\/\\\/blog.alwawee.ru\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/python-django.png\",\"width\":211,\"height\":192},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2313#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.alwawee.ru\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Sending iOS Push Notifications using Python module PyAPNS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/\",\"name\":\"Denis Kutlubaev\",\"description\":\"iOS Development\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.alwawee.ru\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\",\"name\":\"Denis Kutlubaev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"caption\":\"Denis Kutlubaev\"},\"description\":\"iOS Developer, creator of Tornado Browser and many other popular apps\",\"sameAs\":[\"http:\\\/\\\/dennis.alwawee.com\"],\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Sending iOS Push Notifications using Python module PyAPNS  - Denis Kutlubaev","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.alwawee.ru\/?p=2313","og_locale":"en_US","og_type":"article","og_title":"Sending iOS Push Notifications using Python module PyAPNS  - Denis Kutlubaev","og_description":"I have written a small module, that sends a push notification using PyAPNS. This is how my sender.py module looks like: # -*- coding: utf-8 -*- from django.db import connection from apns import APNs, Frame, Payload class Sender: def send(self): apns = APNs(use_sandbox=True, cert_file='Cert.pem', key_file='Key.pem') token_hex = 'e4d4e96ec8ac3dd003e0129347b3421fe53ba49811b3803c4e99cfdc60293c94' message = \"Hello\" payload = Payload(alert=message.decode('utf-8'), sound=\"default\", [&hellip;]","og_url":"https:\/\/blog.alwawee.ru\/?p=2313","og_site_name":"Denis Kutlubaev","article_publisher":"https:\/\/www.facebook.com\/alwaweemobile","article_published_time":"2014-09-21T12:15:21+00:00","article_modified_time":"2014-09-22T13:19:30+00:00","og_image":[{"url":"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/09\/python-django.png","type":"","width":"","height":""}],"author":"Denis Kutlubaev","twitter_misc":{"Written by":"Denis Kutlubaev","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alwawee.ru\/?p=2313#article","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/?p=2313"},"author":{"name":"Denis Kutlubaev","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"headline":"Sending iOS Push Notifications using Python module PyAPNS","datePublished":"2014-09-21T12:15:21+00:00","dateModified":"2014-09-22T13:19:30+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=2313"},"wordCount":217,"commentCount":0,"image":{"@id":"https:\/\/blog.alwawee.ru\/?p=2313#primaryimage"},"thumbnailUrl":"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/09\/python-django.png","keywords":["APNS","Push notifications","PyAPNS","Python"],"articleSection":["iOS","Web Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.alwawee.ru\/?p=2313#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.alwawee.ru\/?p=2313","url":"https:\/\/blog.alwawee.ru\/?p=2313","name":"Sending iOS Push Notifications using Python module PyAPNS - Denis Kutlubaev","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=2313#primaryimage"},"image":{"@id":"https:\/\/blog.alwawee.ru\/?p=2313#primaryimage"},"thumbnailUrl":"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/09\/python-django.png","datePublished":"2014-09-21T12:15:21+00:00","dateModified":"2014-09-22T13:19:30+00:00","author":{"@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"breadcrumb":{"@id":"https:\/\/blog.alwawee.ru\/?p=2313#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alwawee.ru\/?p=2313"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.alwawee.ru\/?p=2313#primaryimage","url":"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/09\/python-django.png","contentUrl":"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/09\/python-django.png","width":211,"height":192},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alwawee.ru\/?p=2313#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alwawee.ru\/"},{"@type":"ListItem","position":2,"name":"Sending iOS Push Notifications using Python module PyAPNS"}]},{"@type":"WebSite","@id":"https:\/\/blog.alwawee.ru\/#website","url":"https:\/\/blog.alwawee.ru\/","name":"Denis Kutlubaev","description":"iOS Development","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alwawee.ru\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7","name":"Denis Kutlubaev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","caption":"Denis Kutlubaev"},"description":"iOS Developer, creator of Tornado Browser and many other popular apps","sameAs":["http:\/\/dennis.alwawee.com"],"url":"https:\/\/blog.alwawee.ru\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2313"}],"version-history":[{"count":0,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2313\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}