Add native push notifications to your web app using Pushpad.
Features:
- notifications are delivered even when the user is not on your website
- users don't need to install any app or plugin
- you can target specific users or send bulk notifications
Currently push notifications work on the following browsers:
- Chrome (Desktop and Android)
- Firefox (44+)
- Safari
Use pip or easy_install:
pip install pushpad-pythonFirst you need to sign up to Pushpad and create a project there.
Then set your authentication credentials and project:
import pushpad
project = pushpad.Pushpad(auth_token='5374d7dfeffa2eb49965624ba7596a09', project_id=123)auth_tokencan be found in the user account settings.project_idcan be found in the project settings.
Pushpad offers two different ways to collect subscriptions. Learn more
Choose the Custom API if you want to use Javascript for a seamless integration. Read the docs
If you need to generate the HMAC signature for the uid you can use this helper:
project.signature_for(current_user_id)Add a link to let users subscribe to push notifications:
'<a href="{url}">Subscribe anonymous to push notifications</a>'.format(
url=project.path()
)
'<a href="{url}">Subscribe current user to push notifications</a>'.format(
url=project.path_for(current_user_id)
)current_user_id is an identifier (e.g. primary key in the database) of the user currently logged in on your website.
When a user clicks the link is sent to Pushpad, automatically asked to receive push notifications and redirected back to your website.
import pushpad
project = pushpad.Pushpad(auth_token='5374d7dfeffa2eb49965624ba7596a09', project_id=123)
notification = pushpad.Notification(
project,
body="Hello world!",
title="Website Name", # optional, defaults to your project name
target_url="http://example.com" # optional, defaults to your project website
)
# deliver to a user
notification.deliver_to(user_id)
# deliver to a group of users
notification.deliver_to((user1_id, user2_id, user3_id))
# deliver to everyone
notification.broadcast()If no user with that id has subscribed to push notifications, that id is simply ignored.
The methods above return a dictionary: 'scheduled' is the number of devices to which the notification will be sent.
The library is available as open source under the terms of the MIT License.