Skip to main content

Abstraction library to send push notifications through APNs, GCM and OneSignal.

Requirements

For APNs an @oeegor/apns2-client fork is used, pypn branch

https://github.com/alej0varas/apns2-client.git

For GCM @geeknam/python-gcm develop branch is used.

https://github.com/geeknam/python-gcm.git

For OneSignal yaosac.

https://pypi.python.org/pypi/yaosac

Usage

APNS

apns = pypn.Notification(pypn.APNS)
apns.send(token, data)

Possible values for token are:

  • A string

  • A list of string

Currently sending multiple notifications means iterating over the list and send one request per user.

GCM

gcm = pypn.Notification(pypn.APNS)
gcm.send(token, data)

Possible values for token are:

  • A string

    • registration_id or topic (just the name, without “/topic/”)

  • A list of string, registration_ids

OneSignal

ones = pypn.Notification(pypn.OS)
ones.send(token, data)

Possible values for token are:

  • A list of OneSignal’s player_id

You need an App Auth Key and App Id. They can be set as environment variables ‘OS_APP_AUTH_KEY’ and ‘OS_APP_ID’ or assigned to the client via user_auth_key and app_id attributes.

Dummy

dummy = pypn.Notification(pypn.DUMMY)
dummy.send(token, data)

Data

PyPN has been built as a layer in between a “back-end”(I’m working on a Django application to handle data) and the existing push notification libraries. That means notifications are defined including all the required fields for each provider. This data is then passed to PyPN together with the provider “name”.

Example data

Not every field value is set.

data = {
   # Common fields
   'body': '1, 2, 3, ... push sucks!'
   'sound': 'default',
   'priority': 'high',
   'title': 'Hello, World!',

   # APNs aps
   'apns_badge': 69,
   'apns_content_available': 1,
   'apns_category': '',
   'apns_mutable_content': True,

   # APNs alert
   'apns_alert_title_loc_key': '',
   'apns_alert_title_loc_args': '',
   'apns_alert_loc_key': '',
   'apns_alert_loc_args': '',
   'apns_alert_action_loc_key': '',
   'apns_alert_launch_image': '',

   # APNs data
   'apns_custom': {'custom': 'values'},

   # GCM data
   'gcm_data': {'custom': 'values'},

   # GCM notification
   'gcm_notification_icon': '',
   'gcm_notification_tag': '',
   'gcm_notification_color': '',
   'gcm_notification_click_action': '',
   'gcm_notification_body_loc_key': '',
   'gcm_notification_body_loc_args': '',
   'gcm_notification_title_loc_key': '',
   'gcm_notification_title_loc_args': '',

   # GCM options
   'gcm_option_collapse_key': '',
   'gcm_option_content_available': '',
   'gcm_option_delay_while_idle': '',
   'gcm_option_time_to_live': 40320,
   'gcm_option_restricted_package_name': '',
}

Credentials

Credentials are expected to be in placed in the environment.

For APNs the path to the .pem certificate and the pass-phrase if any:

  • APNS_CERT_FILE

  • APNS_CERT_PASSWORD

For GCM the server key:

  • GCM_SERVER_KEY

For OneSignal the application id and the API key:

  • OS_APP_AUTH_KEY

  • OS_APP_ID

Debug

Debug is the default value for every provider(you will not get notifications through GCM unless you set debug to false). This can also be set in the environment:

  • APNS_MODE: Possible values are “dev” and “prod”

  • GCM_DRY_RUN: Possible values are 0 and 1

  • There is no debug mode for OneSignal :(

  • Dummy: There is a dummy provider that does nothin

Also logging for the gcm library can be enabled setting GCM_LOGGING environment variable to 1.

Contributing

Feel free to open a pull request or issue in GitHub.

Testing

Install requirements

pip install -r requirements.txt

Copy and update the environment file

cp .env-template .env

DON’T FORGET TO EDIT .env :)

Install honcho

pip install honcho

Unit

honcho run python tests.py

Integration

This will send a notification to your devices.

honcho run python tests_integration.py

Metadata

Release files for pypn 0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pypn 0.9
File Size Uploaded
pypn-0.9.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pypn 0.9
File Interpreter ABI Platform
pypn-0.9-py3-none-any.whl Python 3 none any Details

Total release size: 12.9 kB

Release files / pypn-0.9.tar.gz

Download URL pypn-0.9.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3cd140fa27e0085c2f89e066656e337eea56bf0f26b6e751ca5fc6239b3e3f8b
BLAKE2b-256 checksum
How to use checksums
91f942d7f431df8f9c6d97ace5b492a0af7559dc9f5b3774df9ae4051cbefa9c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / pypn-0.9-py3-none-any.whl

Download URL pypn-0.9-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7ac7eb853b1d4c11933eab0e4ab4422c2839bc90477fa0361dade8565c032b88
BLAKE2b-256 checksum
How to use checksums
123fd65aeb6ff51a8191465aa8fb9a2a003dc98a9294464a2b4b85558aef5006
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.9 This release

2 release files

0.4

2 release files

0.3.6

1 release file

0.3.5

1 release file

0.3.4

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page