Featured Image
Software Development

How to Manage Mobile Push Notifications in Real-time Chat Using PubNub

This blog continues our series, focusing on the dynamic integration of push notifications within real-time chat applications using PubNub. In a previous blog, we discussed how we can use PubNub to design a workflow for implementing end-to-end 1:1 and group chatting.

Assuming we have designed a working chat implemented in any specific framework/programming language, we will now explore how to add push notifications to our chat application. 

Despite the advantages of in-app messaging, you, as a developer, will encounter scenarios when you want to deliver messages as push notifications. For example, when the app is not open or the device is locked, push notifications will ensure that important messages still reach the user. This is where third-party services like Firebase Messaging and Apple Push Notification Service (APNs) come into play.

So, how do we get push notification components up and running in our chat application workflow? The answer lies with the PubNub Mobile Push Service. Before we explore how to use it to deliver push notifications for your real-time chat application, let’s have a quick overview of what PubNub Mobile Push service is and what it can do for you.

If you are new to PubNub and want to understand the basics of it, then do check out our first blog in this series.

What is PubNub Mobile Push service? 

PubNub Mobile Push service is a feature that allows you to deliver messages as push notifications to iOS and Android devices, using Firebase Cloud Messaging (FCM) or Apple Push Notification Service (APNs). It is an important component of any real-time chat application, as it can help you to:

  • Engage and retain your users, by sending them timely and relevant messages through push notifications, even when they are not active in your app.
  • Provide your users with useful information, such as new messages, mentions, invitations, etc., to improve their experience.
  • Reduce your app’s battery and bandwidth consumption to increase its performance. You don’t need to keep a persistent connection to PubNub.

In the next section, we will see how to enable and configure PubNub Mobile Push service for your app.

Prerequisites 

Before we move ahead with our discussion, there are some prerequisites:

  • You have some idea about the basics of PubNub.
  • You understand PubNub’s core concepts: Channels, Publish, and Subscribe messages.
  • You have some idea of working with push gateway services, such as Firebase or APNs (Only for iOS devices).
  • You have configured either one push notification service: Firebase or APNs, and obtained the relevant credentials from it (API keys, certificate, etc.).

Once you have met these prerequisites, you are ready to proceed with the rest of the blog. 

How to enable and configure the PubNub Mobile Push service?

To use the PubNub Mobile Push service, you need to enable and configure it on the PubNub admin portal. Here are the steps to do that:

Step 1: Log in to the PubNub admin portal and select the app and keyset for which you want to enable the mobile push notifications.

Step 2: In the configuration section for your selected keyset, you will find a section named “Mobile Push Notifications”. Select the toggle to enable the feature.

Step 3: Provide the necessary credentials for the service of your choice. For APNs, it is team ID, auth key ID and token file. For Firebase, it is the FCM API key. 

That’s it! You have enabled and configured the PubNub Mobile Push service for your app. Now you can start sending and receiving push notifications for your messages. 

How does PubNub Mobile Push notification work? 

For managing push notifications using PubNub messaging, we must consider two important components: the PubNub channel and the mobile device token.

We know about the usage of PubNub channels in publishing/receiving messages. Mobile device tokens are unique identifiers usually generated by third-party push notification services, such as FCM or APNs.

Push notifications will be delivered by publishing a message with a specific type of payload (called a push payload or notification payload) on any PubNub channel.

PubNub Mobile Push service bridges native PubNub publishing with third-party push notification services. When you enable this feature, you can associate unique mobile device tokens with PubNub channel names. After setting up the device tokens with the channels, any message sent to a channel will be simultaneously dispatched as a push notification to all linked devices through the appropriate FCM or APN service.

Behind the scenes, PubNub Mobile Push service establishes an authenticated connection to the push providers based on your registered configuration.

For detailed steps on configuring mobile push notifications for both iOS and Android apps, refer to the official documentation on iOS and Android Mobile Push Notifications

Implementing PubNub Mobile Push in your app

To use the PubNub Mobile Push service in your app, you need to follow these steps:

  • Design your application workflow such that there are different channels in your system for message transfer. For example, you can have a channel for each chat room, group, or user.
  • Next, ensure that the devices/users that need to receive notifications have their unique device tokens registered to the relevant channels. Upon publishing a message with the push payload to a channel, the PubNub’s Push Gateway checks device registrations and forwards the message to push providers.

FCM/APN providers then deliver the push notification to the appropriate devices. The primary advantage of leveraging the PubNub mobile push is that you do not need to manage third-party API calls/SDKs separately to relay push notifications while managing messaging using PubNub. PubNub takes care of that.

All you need to do is, manage the registration/de-registration of mobile device tokens and the message payload for push notifications.

With the PubNub mobile push, you can also troubleshoot or test the push notifications as well. See the Mobile Push Troubleshooting section in the official documentation for more reference.

Important things to note for mobile push notification management 

To effectively manage push notifications from end-to-end, there are some important considerations:

  • Whether a device receives a notification for messages published on a channel does not depend on channel subscription; it depends on device token registration. Therefore, a device/user doesn’t need to be subscribed to a channel to receive notifications. However, the device token must be registered with the channel to receive notifications for messages published on that channel.
  • For managing the registration and de-registration of mobile device tokens and the message payload for push notifications, consider keeping the operations related to device token registrations with channel operations in your backend APIs/Services. Your backend will decide which device tokens will be registered in which channel based on application workflow.
  • From the mobile app side, you only need to manage publishing the messages with the push payload. Properly dividing these responsibilities can help you customize the workflow of notifications as per business requirements.

Comparison: differences between PubNub Mobile Push Service and other similar services

Mobile Push Service is not the only way of delivering push notifications for your real-time chat app. The following are some other methods that you can utilize to achieve the same results:

Using FCM or APNs directly without PubNub

You have the option of using FCM or APNs native SDKs or APIs to send and receive push notifications for your application. However, this method has its own disadvantages, such as:

  • There is a need to manage multiple connections and credentials for different platforms and providers.
  • The retries and failures of push notifications would also require proper management, whilst recognizing differences and limitations that exist across each provider.
  • Unique logic needs to be implemented to filter out and format messages that require being pushed.

Using third-party push notification services 

OneSignal, Pusher or Airship provide a simple way to send and receive push notifications in your app across various platforms and providers but also suffer from some demerits, such as:

  • You need to pay for the service as well as depend on its availability and reliability.
  • You need an integration strategy with both the backend and the app using their SDKs/APIs.
  • You need to comply with their terms and policies by sharing your data and users with them.
  • Another point of consideration here is the synchronization of these push notifications with real-time messaging systems while avoiding duplication and irrelevant ones.

While these above methods solve the problem, they come at a cost, not just monetarily either. So how does PubNub’s solution stack up? Here are just a few ways:

  • Your connection and credentials will work across platforms and providers, so there’s no need to manage multiple ones at once.
  • You can enhance your push notifications by tapping into features like Functions, Presence, Access Manager, etc.
  • You won’t have to worry about scalability since PubNub already provides reliability in its network delivery.
  • You can simplify everything by using their SDKs, which will handle push notifications for you.

Also readGuide to Building a Flutter Chat App with PubNub

Conclusion

In this blog, we’ve covered how to enable, configure, and use the PubNub Mobile Push service for your real-time chat app. We also discussed how the PubNub Mobile Push Notification works and the key points that you should consider when making use of mobile push notification management. Moreover, we have highlighted how the PubNub Mobile Push service differs from other similar services or solutions.

In our upcoming blog, we will discuss how to manage notification customizations for different chat groups/channels and for direct user mentions.

If you require any assistance with incorporating push notifications into your chat app, then contact us

We are a certified PubNub advanced tier integration partner, and we will be able to help you integrate the PubNub Mobile Push service into your application and tailor it to your requirements. Whether it’s iOS, Android, or cross-platform assistance that you need, look no further as we have everything covered.  

author
Soham Patel
I am a software developer with keen interest in back-end development and DevOps. I am passionate about and have experience in understanding business requirements and translating those into efficient, well-maintainable and reliable software products.