https://github.com/operasoftware/twisted-apns
Twisted client for Apple Push Notification Service (APNs)
https://github.com/operasoftware/twisted-apns
Last synced: 10 months ago
JSON representation
Twisted client for Apple Push Notification Service (APNs)
- Host: GitHub
- URL: https://github.com/operasoftware/twisted-apns
- Owner: operasoftware
- License: mit
- Created: 2015-05-23T18:44:15.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-09-18T10:35:09.000Z (almost 11 years ago)
- Last Synced: 2025-06-23T17:16:40.910Z (about 1 year ago)
- Language: Python
- Homepage:
- Size: 292 KB
- Stars: 10
- Watchers: 21
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Twisted client for APNs
[](https://travis-ci.org/operasoftware/twisted-apns)
[](https://pypi.python.org/pypi/twisted-apns)
[](https://codeclimate.com/github/operasoftware/twisted-apns)
## Overview
*Twisted-APNs* is an implementation of provider-side client for Apple Push Notification Service, based on [official iOS documentation](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html). It uses [Twisted networking engine](https://twistedmatrix.com).
## Features
* Sending notifications through gateway service
* Querying feedback service for failed remote notifications
## Requirements
* Python>=2.7
* Twisted (version 15.0.0 known to work)
* pyOpenSSL
## Download
* [GitHub](https://github.com/operasoftware/twisted-apns)
## Installation
You can install it easily from PyPi by single command:
```
pip install twisted-apns
```
or clone source code and run:
```
python setup.py install
```
## Usage examples
### Sending a notification
First, do the necessary imports and set up logging for debug purposes:
```python
import logging
from apns.gatewayclient import GatewayClientFactory
from apns.notification import Notification
from twisted.internet import reactor
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
```
Then create an instance of gateway client factory, specifying intended endpoint (`pub` for production or `dev` for development purposes), and setting a path to your provider certificate:
```python
# Make sure /apn-dev.pem exists or pass valid path.
factory = GatewayClientFactory('dev', '/apn-dev.pem')
reactor.connectSSL(factory.hostname,
factory.port,
factory,
factory.certificate.options())
```
The code below sends a sample notification with JSON-encoded payload to device identified with supplied `token` (in hex):
```python
def send():
token = '00' # Set to something valid.
payload = {'aps': {'alert': "What's up?",
'sound': 'default',
'badge': 3}}
notification = Notification(token=token,
expire=Notification.EXPIRE_IMMEDIATELY,
payload=payload)
factory.send(notification)
reactor.callLater(1, send)
reactor.run()
```
If `token` is valid console outputs:
```
Gateway connection made: gateway.push.apple.com:2195
Gateway send notification
```
If not (like `00`) error is returned:
```
Gateway connection made: gateway.sandbox.push.apple.com:2195
Gateway send notification
Gateway error received:
Gateway connection lost: Connection was closed cleanly.
Gateway connection made: gateway.sandbox.push.apple.com:2195
```
### Querying list of invalidated tokens
The following code connects to the feedback service and prints tokens which should not be used anymore:
```python
import logging
from apns.feedbackclient import FeedbackClientFactory
from apns.feedback import Feedback
from twisted.internet import reactor
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
# Make sure /apn-dev.pem exists or pass valid path.
factory = FeedbackClientFactory('dev', '/apn-dev.pem')
reactor.connectSSL(factory.hostname,
factory.port,
factory,
factory.certificate.options())
def onFeedbacks(feedbacks):
for f in feedbacks:
print "It would be better to stop sending notifications to", f.token
factory.listen(FeedbackClientFactory.EVENT_FEEDBACKS_RECEIVED, onFeedbacks)
reactor.run()
```
## Contributing
You are highly encouraged to participate in the development, simply use GitHub's fork/pull request system.