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
Install apns with pip after building a virtual environment with Virtualenv
$ pip install apns
Export a pem file for push notifications from your keychain.
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.
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.
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?
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!
GitHub repository.
nnsnodnb/APNs-Push-Notification
Package used this time
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