Send push notifications to iOS apps with Python2 (with sample code)

Introduction

I wrote a similar article last time, I wrote it without experimenting, and since I actually tried it and it didn't work, I will write the article again including corrections!

Send push notifications to iOS apps in Python

environment

Environmental composition

Install apns with pip after building a virtual environment with Virtualenv

$ pip install apns

Preparation

Export a pem file for push notifications from your keychain.

スクリーンショット_2016-09-17_午前11_04_45.png

  1. Export the Develop and Distribution p12 files, including the public and private keys.

As for the private key, if you use the one with the same name, you can reuse it, so keep it with the same name. 2. In the same way for the private key, export only the ** private key ** in p12 format. 3. Convert each of the three pem files to a pem file with the following command

$ openssl pkcs12 -in secret.p12 -out secret.pem -nodes -clcerts

Please change the name so that you can see it. Please set without a password when exporting.

Source code

Send to one terminal

I am setting up a pem file for Developer.

single.py


# coding: utf-8

import time
from apns import APNs, Payload

apns = APNs(use_sandbox=True, cert_file='develop.pem', key_file='secret.pem')

token_hex = '45f9f318ac6506742a54a3b8bb6493c72cdad9afe2c154d5295e9db15cdf83a7'

content = "test!!"
content = content.decode('utf-8')

payload = Payload(alert=content, sound="default", badge=1)
apns.gateway_server.send_notification(token_hex, payload)

cert_file is the public key. key_file is the private key.

Send to multiple terminals

This time, I tested the one given to DeployGate, so I set the pem file for Distribution.

multi.py


# coding: utf-8

import time
from apns import APNs, Frame, Payload

apns = APNs(use_sandbox=False, cert_file='distribution.pem', key_file='secret.pem', enhanced=True)

token_hex = ['417a7b3329c86240b159b816313a3ae51ff9059b98ac29bc7cbcbd36dd41e2a9',
             '446d18e7a621d371a8683d0d33bf9080091efbf693f5b591f9ec307a1e627d46']

message = "message! !!"
message = message.decode('utf-8')

payload = Payload(alert=message, sound="default", badge=1, custom={'uri': 'https://nnsnodnb.moe'})

frame = Frame()
identifier = 1
expiry = time.time() + 3600
priority = 10

for token in token_hex:
    frame.add_item(token, payload, identifier, expiry, priority)

apns.gateway_server.send_notification_multiple(frame)

When sending to multiple terminals, it seems that it is important to set ʻenhanced in the element of ʻAPNs. I forgot to take the link, but it was posted on the issue on GitHub in the PyAPNs package.

Also, is it possible to support push with images from iOS 10 by setting mutable_content = True to Payload in the same way?

result

IMG_0136.png

I can't show the icons because it's currently under development for private use, but I received a notification like this! You can embed JSON in the place of custom, so it seems that you can do something like that quite easily!

Other

GitHub repository.

nnsnodnb/APNs-Push-Notification

Package used this time

djacobs/PyAPNs

Apparently it doesn't support Python3, so I have to do something about it myself. Even if it is 2to3, it doesn't change for some reason! ??

Recommended Posts

Send push notifications to iOS apps with Python2 (with sample code)
Send push notifications to iOS apps in Python
Sample to send slack notification with python lambda
HTML email with image to send with python
Sample to convert image to Wavelet with Python
Send a message to LINE with Python (LINE Notify)
Send an email to Spushi's address with python
Rewrite Python2 code to Python3 (2to3)
Send email with Python
Python Ver. To introduce WebPay with a little code.
Specific sample code for working with SQLite3 in Python
Push notifications from Python to Android using Google's API
Sample code to get started with GLSL shaders in Processing (either Java or Python)
Get country code with python
Sample data created with python
Python with VS Code (Windows 10)
Connect to BigQuery with Python
Send Japanese email with Python3
Settings when developing App Engine / Python apps with VS Code
How to install NPI + send a message to line with python
Python sample to learn XOR with genetic algorithm with neural network
I tried various methods to send Japanese mail with Python
Connect to Wikipedia with Python
Post to slack with Python 3
Run BNO055 python sample code with I2C (Raspberry Pi 3B)
Debug Python with VS Code
Switch python to 2.7 with alternatives
Write to csv with Python
[Python] Sample code for Python grammar
Convert python 3.x code to python 2.x
Send using Python with Gmail
Operate Jupyter with REST API to extract and save Python code
Send push notifications from your iOS app via Firebase Cloud Messaging
Try to create a python environment with Visual Studio Code & WSL
Sample to use after OAuth authentication of BOX API with Python
How to connect to Cloud Firestore from Google Cloud Functions with python code
How to send a request to the DMM (FANZA) API with python
How to build Python and Jupyter execution environment with VS Code
Python: How to use async with
Link to get started with python
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
Nice to meet you with python
Try to operate Facebook with Python
Output to csv file with Python
[Automation] Send Outlook email with Python
Monitor Python web apps with Prometheus
Convert list to DataFrame with python
How to get started with Python
Implemented iOS push notifications in Firebase
What to do with PYTHON release?
Unable to install Python with pyenv
How to calculate date with python
I want to debug with Python
[Python] Send an email with outlook
Send multipart / form-data with python requests
Send image with python, save with php
Install python with mac vs code
[Python] Create API to send Gmail
Send email via gmail with Python 3.4.3.
What to do if you couldn't send an email to Yahoo with Python.