Platform
Docs
Solutions
ContactLog In

How-To Send Mobile & Web Push Notifications using Python

Get started sending notifications with Courier in minutes, not days!

We have SDKs in Ruby, Go, Python, Javascript, Node.js, and React.

Sign up

In this day and age, push notifications—expanding your app's reach beyond the browser—are an extremely effective method to communicate with the user. They can perform basic things like notify the user of a significant event or display a symbol and a brief blurb of text that the user may click on to access your website. You may also include action buttons in the notice so that the user can engage with your site or application without returning to your website.

This article will teach you about push notifications: how they work, why you should use them, and how to implement them in your application using Python.

What are Push Notifications?

Push notifications are the clickable pop-up messages that appear and generally include a call to action (CTA). Regardless of the form of push notification, they are expected to be non-intrusive and beneficial to the user, as well as valuable to a company's conversion rate. These text-based messages may appear as browser notifications on your desktop/laptop or as an Android/IOS notification on your mobile phone. They may be customized to any system, whether a Windows desktop Mozilla Firefox browser or a Windows Mobile Opera browser. Push notifications are extremely configurable and straightforward to apply, which are their primary advantages.

As such, they have many valuable use cases. Consider a few:

Behavioral Characteristics

Have you ever wondered how customers interact with your website? That is something you must keep an eye on if you want to connect with them in the near future. For example, if a user expresses interest in Kindle on your website but does not add it to his basket, you may send a push message with social proof claiming that Kindle is popular. You may even generate a feeling of urgency by claiming that the product will soon be out of stock. By analysing a visitor's behavioural characteristics, you may determine if they are also interested in other items.

Location

This hyper-personalized method analyses visitor movements on the internet, and then, if they are by chance near a corresponding physical store, it sends them a simple mobile push message to encourage action. Gift vouchers are particularly motivating to this end. If a potential buyer is near your physical location, sending a push notification with a gift certificate only redeemable at a physical location is a solid strategy.

Previous Purchases

This form of personalisation is geared toward your current consumers—those who have already purchased from you. Just because someone has visited your website and completed a purchase cycle does not mean that you end all communication with them. For instance, many businesses send a push message with information regarding the brand's most popular goods that are on sale. If you’ve purchased from them before, you’ll already have an idea of what you might purchase during the sale. Let’s say that you manage an online travel portal and someone has booked a flight to Lagos on your website. You can send a customized push message to any blog or website in the Lagos area to continue engaging them. Since the user is traveling to the city for a few days, you might share your expertise about the city's services to increase the customer’s engagement in your platform.

Reminders for Inactive Users

Today's average applications have dismally poor retention rates. Users tend to spend most of their time on popular applications like Facebook and Twitter, which often leads to the uninstallation of scarcely used applications. To avoid a situation where inactive users decide to uninstall your application, you can send regular push notifications reminding them how useful your application is to them.

Product Evaluation Campaigns

Customer feedback is essential, especially for e-commerce firms. You must have a well-planned review seeking strategy in place for this. Campaigns that encourage your consumers to leave reviews are critical since they affect your sales. However, asking your consumers to post reviews has always been difficult. In such circumstances, well-designed push notification campaigns might assist you in obtaining those crucial evaluations.

Benefits of Using Push Notifications

Push notifications consistently outperform email alerts in digital marketing. Marketing studies have shown that the click rate of push notifications is four to eight times higher than that of email, regardless of kind. One feature that distinguishes them from other alerts is that consumers will only get push notifications if they have opted in. Furthermore, opting out is just as simple as opting in, only a few clicks away.

Using push notifications provides a variety of benefits to your organization, such as:

Increased Brand Awareness

By enrolling subscribers with a simple click, you can boost your company's exposure among your audience by employing various sorts of push notifications.

Increased Return Traffic

When it comes to maintaining clients, sending alerts is an ideal form of interaction. From any browser, the proactive messages drive people back to your website. You may achieve this by enticing them with promotional offers and informative content.

Time Saved

You can communicate with your users at the right time by using several forms of push notifications. When searching for immediate reactions, you can deliver real-time push notifications, and you can even automate messages for particular campaigns or personalize push notifications for specific zones or time zones.

Improved Reach

In most cases, push notifications will outperform SMS or email alerts, which are becoming more obsolete. If your user has a mobile device that supports Google Chrome, Mozilla Firefox, or Opera, you’re good to go.

Multi-Channel Push Notifications

Today, notifications are at the heart of many software products, from applications to e-commerce shops. Instagram, for example, would not survive without push notifications, since users do not keep the app open at all times to watch the activity on their feed. Using push notifications to alert users about new content or information they need to know keeps them interested and engaged.

When businesses recognize that they cannot cater to their users who are present on several channels with a one-size-fits-all strategy, multi-channel alerts become a strategic issue. For example, while email works well for asynchronous communication, users may prefer a summary of travel forum activity via email. However, for an action that requires immediate attention, such as a change in a flight itinerary, a corporation would most likely forego the email notification in favour of a push notification. As you can imagine, there are many scenarios that make having multiple kinds of notifications available advantageous.

How to Send Push Notifications

The remainder of this article will focus on the implementation of push notifications for mobile, web, and multi-channel use. Read on for step-by-step tutorials on how to set up each type of notification.

Mobile Push Notifications

A push notification for a mobile app is a message sent by an application to a customer's mobile device. Customers who have installed your mobile app and opted in to receive messages can get push notifications.

Push notifications in mobile apps are often used to convey product updates, reminders, targeted offers, breaking news, and other information critical to the app's performance. Furthermore, they typically demand particular attention or fast action.

Prerequisites

To follow this tutorial, you will need the following:

  • Boto3 - install from PyPI
  • Python
  • An AWS SNS account

Implementation

To implement mobile push notifications with Python, follow the steps listed below:

Step 1

Create an AWS account, if you don’t have one already. Then navigate to the My Security Credentials tab to get your AWS_KEY_ID and AWS_SECRET.

Step 2

Click on the Access keys (access key ID and secret access key) toggle, and then select Create New Access Key.

Step 3

Copy or download your AWS keys and paste them somewhere you can easily retrieve them.

Step 4

Create an Amazon SNS client using boto3. Copy the code below and replace your AWS keys where appropriate:

1 2 3 4 5 6 7 8 9 10 11 12 import boto3 sns = boto3.client("sns", region_name="us-east-1", aws_access_key_id=AWS_KEY_ID, aws_secret_access_key=AWS_SECRET) print(sns)

If the client initialized successfully, you will receive an output similar to the one in the image below after running the Python code:

Topics

A topic is a communication channel through which you can send messages. To send push notifications, you need to create a topic on Amazon SNS, and then publish the message you want to send to the topic.

Managing Topics

Use the create_topic() method with the required name to create a new topic. You may obtain the ARN of a topic, once generated, by extracting the TopicArn key from the object provided by create_topic(). Copy and run the Python code below to create a topic and extract its ARN key:

1 2 3 4 5 6 7 8 # Create topic response = sns.create_topic(Name="topic_name") #Extract Topic’s ARN Key topic_arn = response["TopicArn"]

Then, use the list topics() method and retrieve the topics key from its output to list existing topics on AWS:

1 2 3 4 5 # List Topics response = sns.list_topics() topics = response["Topics"]

If you want to delete a topic, just send the topic's ARN to the delete topic() method to remove it, as shown in the code sample below:

1 2 3 # Delete topics sns.delete_topic(TopicArn=topic_arn)

Subscriptions

A subscription is a record of a client’s interest in a topic, be it from an endpoint or from a user. Any client subscribed to a topic automatically receives all the messages published to that topic.

Managing Subscriptions

Call the subscribe() method to create a new subscription to a subject, specifying the ARN of the topic to subscribe to, the protocol (e.g. SMS or email), and the endpoint (e.g. a phone number for the SMS protocol or an email address for the email protocol). You may obtain the ARN of a subscription after it has been formed by extracting the SubscriptionArn key from the object returned by subscribe().

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 import boto3 sns = boto3.client("sns", region_name="us-east-1", aws_access_key_id=AWS_KEY_ID, aws_secret_access_key=AWS_SECRET) # Create SMS subscription response = sns.subscribe(TopicArn=topic_arn, Protocol="SMS", Endpoint="+48123456789") #Extract SMS Subscription ARN subscription_arn = response["SubscriptionArn"] # Create an email subscription response = sns.subscribe(TopicArn=topic_arn, Protocol="email", Endpoint="[user@server.com](mailto:user@server.com)") #Extract Email Subscription ARN subscription_arn = response["SubscriptionArn"]

To get a list of all existing subscriptions on AWS, use the list subscriptions() method and extract the subscriptions key from its result. Similarly, the list_subscriptions by the topic() method is used to list subscriptions to a given subject.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import boto3 sns = boto3.client("sns", region_name="us-east-1", aws_access_key_id=AWS_KEY_ID, aws_secret_access_key=AWS_SECRET) # List all subscriptions response = sns.list_subscriptions() subscriptions = response["Subscriptions"] # List subscriptions by topic response = sns.list_subscriptions_by_topic(TopicArn=topic_arn) subscriptions = response["Subscriptions"]

Use the unsubscribe() method and supply its ARN to unsubscribe from a single subscription. You can also loop through subscriptions using a for-loop at the bottom of the code chunk to remove multiple subscriptions that share the same protocol.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import boto3 sns = boto3.client("sns", region_name="us-east-1", aws_access_key_id=AWS_KEY_ID, aws_secret_access_key=AWS_SECRET) # Delete subscription sns.unsubscribe(SubscriptionArn=subscription_arn) # Delete multiple subscriptions (here: all SMS ones) for sub in subscriptions: if sub["Protocol"] == "sms": sns.unsubscribe(sub["SubscriptionArn"])

Publishing Messages to Topics

To send a message to a topic, use the publish() method, passing the topic's ARN, the desired message, and optionally a subject (which will only be used in email messages).

Alternatively, you might send out an SMS message with no subject or subscription. To do this, use the publish() method with the phone number and message content as inputs:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import boto3 sns = boto3.client("sns", region_name="us-east-1", aws_access_key_id=AWS_KEY_ID, aws_secret_access_key=AWS_SECRET) # Publish to topic sns.publish(TopicArn=topic_arn, Message="message text", Subject="subject used in emails only") # Send a single SMS (no topic, no subscription needed) sns.publish(PhoneNumber="+2349012345678", Message="message text")

Web Push Notifications

Web push notifications enable consumers to opt in to timely updates from sites they enjoy, allowing you to re-engage them with personalized, relevant information.

To send a push message to your users, you must perform an API request to a push service. This API request would specify what data to transmit, who to send the message to, and any other criteria for sending the message. This API request is typically made from your server.

A push service accepts a network request, validates it, and sends a push message to the corresponding browser. If the browser is not available, the message is delayed until the browser becomes available.

Developers have no control over which browsers use push services. This isn't an issue, though, because each push service wants the same API request.

In this section, you’ll learn how to implement push notifications using Pushbullet. Pushbullet can also be used to implement push notifications in mobile devices, web applications, and laptops.

Prerequisites

To follow this tutorial, you will need the following:

  • Pushbullet - install from PyPI
  • Pywebio - install from PyPI
  • Python

Implementation

To implement web push notifications with Python, follow the steps listed below:

Step 1

Create an account on Pushbullet if you don’t already have one here.

Step 2

Navigate to the Set up your computer tab and select your preferred browser.

Step 3

Add the Pushbullet extension to your browser.

Step 4

Navigate to Account in the Settings tab and copy your access token.

Step 5

Copy and run the Python code below in your code editor:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 # Import the following modules from pushbullet import PushBullet from pywebio.input import * from pywebio.output import * from pywebio.session import * import time # Get the access token from Pushbullet.com access_token = "your access token" # Taking input from the user data = input('Title') # Taking large text input from the user text = textarea( "Text", rows=3, placeholder="Write something...", required=True) # Get the instance using the access token pb = PushBullet(access_token) # Send the data by passing the main title # and text to be send push = pb.push_note(data, text) # Put a success message after sending # the notification put_success("Message sent successfully...") # Sleep for 3 seconds time.sleep(3) # Clear the screen clear() # Give the pop at last toast("Thanks for using it :)") # hold the session until the whole work finishes hold()
Step 6

After running the code, your browser should open a new tab where you’ll enter the notification message. Enter the subject and message for the notification, and then click on Submit to send.

Step 7

You should now get a push notification from your browser, as seen in the image below.

Multi-Channel Push Notifications

Finally, in this section, you’ll learn how to implement multi-channel push notifications using Courier and Amazon SNS.

Prerequisites

To follow this tutorial, you will need the following:

  • Python
  • An AWS SNS account
  • trycourier - install from PyPI

Implementation

Follow the steps below to implement multi-channel push notifications with Courier and Amazon SNS:

Step 1

If you don’t already have one, create a Courier account .

Step 2

Navigate to the Integrate tab in your account, where you can find all notification channel providers, then select AWS SNS. You can also add Gmail here, so you have multiple channels on which to send notifications.

Step 3

Provide the information required from your AWS SNS account and the SNS topic you created where appropriate, as shown in the image below. Then click on Install Provider.

Be sure to also install the Gmail provider.

Step 4

Navigate to the Designer tab and click on the Create Notification button.

Step 5

Select AWS SNS and Gmail from the configured providers, then click on the Publish Changes button in the top right corner.

Next, click on the Settings icon next to Untitled Notification to rename your notification and get your auth token.

Step 6

Enter a message for AWS SNS and Gmail in the Design tab for the notification you just created, then click on the Publish Changes button to save the messages for each one.

Note: The first channel provider in your notification takes priority for use when sending notifications. For example, if Amazon SNS is the number-one channel provider, your notifications will be sent using Amazon SNS. Alternatively, if Gmail is the number-one channel provider in your notification, then your notifications will be sent using Gmail. You can switch their positions by clicking and dragging them as needed.

Step 7

Create a subscription on your Amazon SNS account for the topic you used to create your notification.

Step 8

Copy and run the Python code below, replacing tokens in the code where appropriate:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 from trycourier import Courier client = Courier(auth_token="your auth token") resp = client.send( event="notification_id", recipient="recepient_email", profile={ "email": "your_profile_email" } )
Step 9

After running the Python code above, you should get a notification like the one in the image below:

Alternatively, when you set Gmail as the first channel in the notification created, you should get a notification like this:

The major difference between Gmail and Amazon SNS notification is that the subject of the notification in Gmail is customizable, while the latter is not.

Conclusion

In this tutorial, you learned about push notifications: their use cases, benefits, and how to implement them with Python. You also learned about multi-channel push notifications and how to implement them using Courier.

Courier is a notification service that allows you to access all your notification channels through a single API. It will enable you to send personalized notifications based on user actions while respecting your preferences and rules.

Courier’s admin tool allows you to customize push notifications using conditionals, ensuring that you reach your users at the right time and on their preferred channel. It also has analytics tools that give you helpful information and metrics about how users are engaging your notifications, allowing you to provide better services for your users. To learn more, visit their website.

Author: Onojakpor Ochuko

Get started sending notifications with Courier in minutes, not days!

We have SDKs in Ruby, Go, Python, Javascript, Node.js, and React.

Sign up

View More Guides

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Platform

Users

Content

Channels

Sending

Workflows

Preferences

Inbox

Workspaces

Observability

API Status

Changelog

© 2024 Courier. All rights reserved.