ios-notification

https://travis-ci.org/nnsnodnb/django-ios-notifications.svg?branch=master https://coveralls.io/repos/github/nnsnodnb/django-ios-notifications/badge.svg?branch=master https://badge.fury.io/py/ios-notification.svg https://img.shields.io/pypi/pyversions/ios-notification.svg https://img.shields.io/pypi/status/ios-notification.svg https://img.shields.io/pypi/wheel/ios-notification.svg https://img.shields.io/pypi/format/ios-notification.svg https://img.shields.io/pypi/implementation/ios-notification.svg https://img.shields.io/pypi/l/ios-notification.svg

Contents

Introduction

A Django plugin for Apple Push Notification Service.

Supported python versions

2.7, 3.4, 3.5, 3.6

Supported django versions

1.7 - 1.11

Installation
$ pip install ios-notification
Author

nnsnodnb

LICENSE

MIT License

Prepare

Set up

settings.py

Add notification into INSTALLED_APPS in settings.py file

INSTALLED_APPS += (
    'notification',
)
urls.py

Add notification routing in urls.py file

from django.conf.urls import include

urlpatterns += (
    url(r'^ios/', include('notification.urls', namespace='notification')),
)
Migration
$ python manage.py migrate
Prepare application
$ python manage.py createsuperuser
$ python manage.py runserver

Please access http://127.0.0.1:8000/cert_upload and login by superuser, then upload push notification’s certificate.

Note

  • PEM file not locked.
  • Secure PEM file which is not double transfer etc.
$ openssl pkcs12 -in hoge.p12 -out hoge.pem -nodes -clcerts

Routes

Detail

/receive

Send device token http://127.0.0.1:8000/receive with UUID.

Request parameters
Name Type Required Note
device_token String YES 64 letters
uuid String YES 36 letters
sandbox Bool YES
  • Request method: PUT
  • Content-Type: application/json
  • Model: notification.models.DeviceToken
  • Success response: {"result": "success"}
  • Failure response: {"error": "Bad Request"}

notification.models.DeviceToken has user field so you can also associate users by applying django.contrib.auth.models.User.

Sample Request
$ curl -X PUT http://127.0.0.1:8000/receive \
  -d '{"device_token": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "uuid": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", "sandbox": true}'
/send

It is possible to send a push notification easily.

Real route is /send/<TARGET_MODE>/<DEVICE_TOKEN>.

TARGET_MODE
  • Development: 0
  • Distribution: 1
DEVICE_TOKEN
  • Device token registered DATABASE.
Request parameter
Name Type Required Note
message String NO URL encoded
  • Request method: GET
Sample Request
Use sandbox
http://127.0.0.1:8000/send/0/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX?message=test%20push%20notification
NOT use sandbox
http://127.0.0.1:8000/send/1/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
/cert_upload

Upload push notifications’ certificates by superuser.

Note

  • PEM file not locked.
  • Secure PEM file which is not double transfer etc.
/login

Login to session.

/logout

Logout from session.

Command

Single push notification

Send a push notification to one device token.

Sample command
$ cd /path/to/your_django_project/
$ python manage.py singlepush (--sandbox) \
                              --token XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \
                              --title Notification's\ title \
                              (--subtitle Notification's\ subtitle) \
                              (--body Notification's\ body) \
                              (--sound default) \
                              (--badge 1) \
                              (--contentavailable) \
                              (--mutablecontent) \
                              (--extra '{"key":"value","key2":"value2"}')
About each argument for single push notification
Argument name Description Required Default
--sandbox, -s Use apple sandbox. NO False
--token, -t Target device token. YES None
--title Title displayed in push notification. YES None
--subtitle Subtitle displayed in push notification. NO None
--body Body displayed in push notification. NO None
--sound Sounds to be heard when push notification is received. NO default
--badge Badge displayed on application icon. NO 0
--contentavailable, -c Use content-available. (Support for iOS7 or higher) NO False
--mutablecontent, -m Use mutable-content. (Support for iOS9 or higher) NO False
--extra, -e Custom notification payload values as a JSON dictionary. NO None

Multiple push notifications

Send push notifications to some device tokens.

Sample command
$ cd /path/to/your_django_project/
$ python manage.py multipush (--sandbox) \
                             -t XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY \
                             (--all) \
                             --title Notification's\ title \
                             (--subtitle Notification's\ subtitle) \
                             (--body Notification's\ body) \
                             (--sound default) \
                             (--badge 1) \
                             (--contentavailable) \
                             (--mutablecontent) \
                             (--extra '{"key":"value","key2":"value2"}')
About each argument for multiple push notification
Argument name Description Required Default
--sandbox, -s Use apple sandbox. NO False
--token, -t Target device token. YES None
--all, -a Target all device tokens. NO False
--title Title displayed in push notification. YES None
--subtitle Subtitle displayed in push notification. NO None
--body Body displayed in push notification. NO None
--sound Sounds to be heard when push notification is received. NO default
--badge Badge displayed on application icon. NO 0
--contentavailable, -c Use content-available. (Support for iOS7 or higher) NO False
--mutablecontent, -m Use mutable-content. (Support for iOS9 or higher) NO False
--extra, -e Custom notification payload values as a JSON dictionary. NO None

Indices and tables