Welcome to django-scarface
Send push notifications to mobile devices using Amazon SNS.
You can install Scarface directly from pypi using pip:
pip install django-scarface
Edit your settings.py file:
INSTALLED_APPS = ( ... "scarface", )
||Acess key of your AWS user*.||Yes||-|
||Secret key of your AWS user.||Yes||-|
||The region your SNS application is located||Yes||'eu-west-1'|
||If true the push messages are logged to the DB.||
||A list of additional platform strategies to integrate other AWS platforms.||No||
||The length of a push notification, defaults to 140 chars. Please note that there are platform specific restrictions.||No||
|**We assume that user has all the privileges to create Applications, Endpoints and Topics *|
You can extract the SCARFACE_APNS_CERTIFICATE and SCARFACE_APNS_PRIVATE_KEY settings from a .p12 file exported from Keychain Access. The usage is as simple as the following example:
python manage.py extract_keys --file=Certificate.p12 --password=<MYPASSWORD>
The output can be copied and pasted into your settings file.
The code it self is good documented. You may also check the unittests (
tests.py) or implementation details.
This is a tutorial how you create AWS Applications, Platforms etc. programmatically. Alternatively you can create the modeinstances in the django admin area.
Create Applications and Platforms
First you have to create a new application.
app = Application.objects.create(name='test_application)
Once you have successfully created an application you can add platforms to that it. Currently only 'Google Cloud Messaging' and 'Apple Push Notification Service' are available. If you wish, you can add further strategies to support more platforms.
apns_platform = Platform.objects.create( platform='APNS', application=app, arn=TEST_ARN_TOKEN_APNS ) gcm_platform = Platform.objects.create( platform='GCM', application=app, arn=TEST_ARN_TOKEN_APNS )
The available values for the platform parameter are:
||Google Cloud Messaging|
||Apple Push Notification Service|
||Apple Push Notification Service Sandbox|
Having a setup platform you are now ready to register new devices to that platform.
apple_device = Device.objects.create( device_id=<device_id>, push_token=<device_push_token>, platform=apns_platform ) andorid_device = Device.objects.create( device_id=<device_id>, push_token=<device_push_token>, platform=gcm_platform ) apple_device.register() android_device.register()
Before you can subscribe a device to a topic we have to create that topic.
topic = Topic.objects.create( name='test_topic', application = app, ) topic.register()
Subscribe to Topic
Once the platforms, devices and topics have been set up you can register the devices to topics.
All the above mentioned classes which support the
register() method can be deregistered by using their
deregister() method. Further, when you delete them, they automatically deregister.
Send Push Notifications
Register a device like seen above.
Send a push message:
message = PushMessage( badge_count=1, context='url_alert', context_id='none', has_new_content=True, message="Hello world!", sound="default" ) ios_device.send(message)
If logging is enabled, all sent push messages are logged in the table scarface_pushmessage.
Register New Platforms
If you want to register a new platform create a new subclass of the abstract
PlatformStrategy class. Take a look on an existin implementation for an example.
Then add it to the
SCARFACE_PLATFORM_STRATEGIES settings parameter:
SCARFACE_PLATFORM_STRATEGIES = [ 'path.to.my.PlatformStrategy' ]
By giving you own class the same ID as the one of an existing implementation, you overwrite that implementation.
How do I change the credentials?
There is no direct way yet to exchange the credentials. To exchange the API credentials you'll have to replace the old one in the settings file and in the AWS console manually.
Now have fun using this library and push it to the limit.