Send push notifications to mobile devices through GCM or APNS in Django.
Project description
django-push-notifications
=========================
.. image:: https://api.travis-ci.org/jleclanche/django-push-notifications.png
:target: https://travis-ci.org/jleclanche/django-push-notifications
A minimal Django app that implements Device models that can send messages through APNS and GCM.
The app implements two models: GCMDevice and APNSDevice. Those models share the same attributes:
- name (optional): A name for the device.
- is_active (default True): A boolean that determines whether the device will be sent notifications.
- user (optional): A foreign key to auth.User, if you wish to link the device to a specific user.
- device_id (optional): A UUID for the device obtained from Android/iOS APIs, if you wish to uniquely identify it.
- registration_id (required): The GCM registration id or the APNS token for the device.
The app also implements an admin panel, through which you can test single and bulk notifications. Select one or more
GCM or APNS devices and in the action dropdown, select "Send test message" or "Send test message in bulk", accordingly.
Note that sending a non-bulk test message to more than one device will just iterate over the devices and send multiple
single messages.
Dependencies
------------
All versions of Django 1.0 and newer should be supported, however no guarantees are made for versions older than 1.4.
The app also depends on django-uuidfield.
Tastypie support should work on Tastypie 0.9.11 and newer.
Django versions older than 1.5 require 'six' to be installed.
Setup
-----
You can install the library directly from pypi using pip::
$ pip install django-push-notifications
Edit your settings.py file::
INSTALLED_APPS = (
...
"push_notifications"
)
PUSH_NOTIFICATIONS_SETTINGS = {
"GCM_API_KEY": "<your api key>",
"APNS_CERTIFICATE": "/path/to/your/certificate.pem",
}
Settings list
-------------
All settings are contained in a PUSH_NOTIFICATIONS_SETTINGS dict.
In order to use GCM, you are required to include GCM_API_KEY.
For APNS, you are required to include APNS_CERTIFICATE.
- APNS_CERTIFICATE: Absolute path to your APNS certificate file. Certificates with passphrases are not supported.
- GCM_API_KEY: Your API key for GCM.
- APNS_HOST: The hostname used for the APNS sockets. When DEBUG=True, this defaults to gateway.sandbox.push.apple.com.
When DEBUG=False, this defaults to gateway.push.apple.com.
- APNS_PORT: The port used along with APNS_HOST. Defaults to 2195.
- GCM_POST_URL: The full url that GCM notifications will be POSTed to. Defaults to https://android.googleapis.com/gcm/send.
- GCM_MAX_RECIPIENTS: The maximum amount of recipients that can be contained per bulk message. If the registration_ids list
is larger than that number, multiple bulk messages will be sent. Defaults to 1000 (the maximum amount supported by GCM).
Sending messages
----------------
GCM and APNS services have slight different semantics. The app tries to offer a common interface for both when using the models.
::
from push_notifications.models import APNSDevice, GCMDevice
device = GCMDevice.objects.get(registration_id=gcm_reg_id)
# The first argument will be sent as "message" to the intent extras Bundle
# Retrieve it with intent.getExtras().getString("message")
device.send_message("You've got mail")
# If you want to customize, send an extra dict and a None message.
# the extras dict will be maped into the intent extras Bundle. Remember, GCM converts everything to strings!
device.send_message(None, extra={"foo": "bar"})
device = APNSDevice.objects.get(registration_id=apns_token)
device.send_message("You've got mail") # Alert message may only be sent as text.
device.send_message(None, badge=5) # No alerts but with badge.
device.send_message(None, badge=1, extra={"foo": "bar"}) # Silent message with badge and added custom data.
Note that APNS does not support sending payloads that exceed 256 bytes. The message is only one part of the payload, if
once constructed the payload exceeds the maximum size, an APNSDataOverflow exception will be raised before anything is sent.
Sending messages in bulk
------------------------
::
from push_notifications.models import APNSDevice, GCMDevice
devices = GCMDevice.objects.filter(user__first_name="James")
devices.send_message({"message": "Happy name day!"})
Sending messages in bulk makes use of the bulk mechanics offered by GCM and APNS. It is almost always preferable to send
bulk notifications instead of single ones.
Exceptions
----------
- NotificationError(Exception): Base exception for all notification-related errors.
- gcm.GCMError(NotificationError): An error was returned by GCM. This is never raised when using bulk notifications.
- apns.APNSError(NotificationError): Something went wrong upon sending APNS notifications.
- apns.APNSDataOverflow(APNSError): The APNS payload exceeds its maximum size and cannot be sent.
Tastypie support
----------------
The app includes tastypie-compatible resources in push_notifications.api. These can be used as-is, or as base classes
for more involved APIs.
The following resources are available:
- APNSDeviceResource
- GCMDeviceResource
- APNSDeviceAuthenticatedResource
- GCMDeviceAuthenticatedResource
The base device resources will not ask for authentication, while the authenticated ones will link the logged in user to
the device they register.
Subclassing the authenticated resources in order to add a SameUserAuthentication and a user ForeignKey is recommended.
When registered, the APIs will show up at <api_root>/device/apns and <api_root>/device/gcm, respectively.
Python 3 support
----------------
django-push-notifications has been tested on Python 3 and should work. However, the django-uuidfield dependency does not
officially support Python 3. A pull request is pending and can be used for the time being::
pip install -e git://github.com/dominicrodger/django-uuidfield.git@python3#egg=django_uuidfield
=========================
.. image:: https://api.travis-ci.org/jleclanche/django-push-notifications.png
:target: https://travis-ci.org/jleclanche/django-push-notifications
A minimal Django app that implements Device models that can send messages through APNS and GCM.
The app implements two models: GCMDevice and APNSDevice. Those models share the same attributes:
- name (optional): A name for the device.
- is_active (default True): A boolean that determines whether the device will be sent notifications.
- user (optional): A foreign key to auth.User, if you wish to link the device to a specific user.
- device_id (optional): A UUID for the device obtained from Android/iOS APIs, if you wish to uniquely identify it.
- registration_id (required): The GCM registration id or the APNS token for the device.
The app also implements an admin panel, through which you can test single and bulk notifications. Select one or more
GCM or APNS devices and in the action dropdown, select "Send test message" or "Send test message in bulk", accordingly.
Note that sending a non-bulk test message to more than one device will just iterate over the devices and send multiple
single messages.
Dependencies
------------
All versions of Django 1.0 and newer should be supported, however no guarantees are made for versions older than 1.4.
The app also depends on django-uuidfield.
Tastypie support should work on Tastypie 0.9.11 and newer.
Django versions older than 1.5 require 'six' to be installed.
Setup
-----
You can install the library directly from pypi using pip::
$ pip install django-push-notifications
Edit your settings.py file::
INSTALLED_APPS = (
...
"push_notifications"
)
PUSH_NOTIFICATIONS_SETTINGS = {
"GCM_API_KEY": "<your api key>",
"APNS_CERTIFICATE": "/path/to/your/certificate.pem",
}
Settings list
-------------
All settings are contained in a PUSH_NOTIFICATIONS_SETTINGS dict.
In order to use GCM, you are required to include GCM_API_KEY.
For APNS, you are required to include APNS_CERTIFICATE.
- APNS_CERTIFICATE: Absolute path to your APNS certificate file. Certificates with passphrases are not supported.
- GCM_API_KEY: Your API key for GCM.
- APNS_HOST: The hostname used for the APNS sockets. When DEBUG=True, this defaults to gateway.sandbox.push.apple.com.
When DEBUG=False, this defaults to gateway.push.apple.com.
- APNS_PORT: The port used along with APNS_HOST. Defaults to 2195.
- GCM_POST_URL: The full url that GCM notifications will be POSTed to. Defaults to https://android.googleapis.com/gcm/send.
- GCM_MAX_RECIPIENTS: The maximum amount of recipients that can be contained per bulk message. If the registration_ids list
is larger than that number, multiple bulk messages will be sent. Defaults to 1000 (the maximum amount supported by GCM).
Sending messages
----------------
GCM and APNS services have slight different semantics. The app tries to offer a common interface for both when using the models.
::
from push_notifications.models import APNSDevice, GCMDevice
device = GCMDevice.objects.get(registration_id=gcm_reg_id)
# The first argument will be sent as "message" to the intent extras Bundle
# Retrieve it with intent.getExtras().getString("message")
device.send_message("You've got mail")
# If you want to customize, send an extra dict and a None message.
# the extras dict will be maped into the intent extras Bundle. Remember, GCM converts everything to strings!
device.send_message(None, extra={"foo": "bar"})
device = APNSDevice.objects.get(registration_id=apns_token)
device.send_message("You've got mail") # Alert message may only be sent as text.
device.send_message(None, badge=5) # No alerts but with badge.
device.send_message(None, badge=1, extra={"foo": "bar"}) # Silent message with badge and added custom data.
Note that APNS does not support sending payloads that exceed 256 bytes. The message is only one part of the payload, if
once constructed the payload exceeds the maximum size, an APNSDataOverflow exception will be raised before anything is sent.
Sending messages in bulk
------------------------
::
from push_notifications.models import APNSDevice, GCMDevice
devices = GCMDevice.objects.filter(user__first_name="James")
devices.send_message({"message": "Happy name day!"})
Sending messages in bulk makes use of the bulk mechanics offered by GCM and APNS. It is almost always preferable to send
bulk notifications instead of single ones.
Exceptions
----------
- NotificationError(Exception): Base exception for all notification-related errors.
- gcm.GCMError(NotificationError): An error was returned by GCM. This is never raised when using bulk notifications.
- apns.APNSError(NotificationError): Something went wrong upon sending APNS notifications.
- apns.APNSDataOverflow(APNSError): The APNS payload exceeds its maximum size and cannot be sent.
Tastypie support
----------------
The app includes tastypie-compatible resources in push_notifications.api. These can be used as-is, or as base classes
for more involved APIs.
The following resources are available:
- APNSDeviceResource
- GCMDeviceResource
- APNSDeviceAuthenticatedResource
- GCMDeviceAuthenticatedResource
The base device resources will not ask for authentication, while the authenticated ones will link the logged in user to
the device they register.
Subclassing the authenticated resources in order to add a SameUserAuthentication and a user ForeignKey is recommended.
When registered, the APIs will show up at <api_root>/device/apns and <api_root>/device/gcm, respectively.
Python 3 support
----------------
django-push-notifications has been tested on Python 3 and should work. However, the django-uuidfield dependency does not
officially support Python 3. A pull request is pending and can be used for the time being::
pip install -e git://github.com/dominicrodger/django-uuidfield.git@python3#egg=django_uuidfield
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file django-push-notifications-1.1.0.tar.gz
.
File metadata
- Download URL: django-push-notifications-1.1.0.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5cdc31e642f9de40fd1f1175281d5110fc13c55cf4cb7eb013a54711d26b924b |
|
MD5 | d861b96901e92311ff7f62dfffd448c4 |
|
BLAKE2b-256 | a3504fe2fde5a0bcf906e36423dc56a02c1d1f172dcc54f19a3d159df773d74c |