Push Notifications
What is a Push Notification?
A push notification is a message typically sent from a server to a device that instructs mobile applications (iOS and Android) and browsers to perform some action. Push notifications can deliver information to users on their devices when the application is in the foreground, background, or even closed. You can draw users back to your app by reminding and informing them of important notices, messages, timers, and events.
Why use Push notifications?
Although a real time communication platform such as PubNub’s low latency and reliable communication network allows your users to receive messages, updates, and notifications reliably and in real time, your application needs to be connected to the internet to be able to communicate with the PubNub network. When users move an application to the background or completely close the application, this will terminate the connection to the internet for most applications. How would your app then know when to signal the end-user that there are notifications, updates, or for them to take action?
You want to use push notifications to bring the user back to your application to complete an action, notify them of any application news, or for any social notifications, such as receiving a direct message while you were offline. PubNub offers push notifications as a way to better keep your users engaged and informed: you can trigger event or state-based alerts that bring your users back to games, stores, classes, and more. You can keep your customers informed with real time alerts, highlight exceptions, or update statuses.
Push notifications can be and are used in a variety of ways, and companies can take advantage of PubNub’s infrastructure to use push notifications in their market:
FanHub Media uses PubNub to deliver notifications, updates, and alerts to their users in sports fan applications.
A telemedicine app, such as how Hearo helps patients to live more independently, may issue push notifications to prompt patients to take medicine and log their treatment progress, helping them adhere to a treatment plan and achieve a more positive health outcome.
An e-commerce app can drive customer engagement with personalized notifications. For example, they may reach out to shoppers with an abandoned cart, prompting them to return to the storefront and buy those items.
Game developers like PocketGems often host special time-bound events like tournaments, which drive player activity with unique gameplay and rewards. Push notifications let games reach out to players far and wide when these live events start, even if they haven’t played in a while. And because users can simply tap on the notification to open the game, the notification itself serves as a vital shortcut for distractible players.
Check out how PubNub’s other customers have successfully integrated push notifications into their application with PubNub.
Types of Push Notifications
Up to this point, we’ve been referring to push notifications collectively. In reality, while they generally look and behave the same in any context, there are important differences in the details between iOS, Android, and web push notifications that affect how you should think about and deploy them.
Mobile push notifications
Broadly, these are the most common kind of push notifications and should be familiar to almost all mobile users.
While both iOS and Android support push notifications, there are key differences in how each mobile platform treats them. When shipping an app, you need to make considerations for differences in permissions and visibility for different devices.
Permissions
Users on both iOS and Android have ultimate control over which apps can send notifications. On Android, users must opt out of notifications manually, while iOS apps must get users to opt-in. You need to ensure that when you are sending push notifications they are useful and do not annoy the end user.
Visibility
On both mobile platforms, push notifications appear on a phone’s lock screen when the device is asleep. The differences kick in once a user unlocks their device. On an iPhone, the notification itself is cleared, even though the notification messages themselves are still accessible from the notification center. On Android devices, notifications persist until a user takes deliberate action to clear or interact with the alert. For Apple developers, this adds extra incentive to make push notifications relevant and attention-grabbing, so that users opt to interact with them directly when they arrive.
Web push notifications
On the modern web, the line between traditional websites and fully-fledged web apps has been blurred but luckily, modern browsers like Chrome also support their own version of push notifications that extend all the benefits of out-of-app alerts to desktop users.
Users must first opt into web notifications. Once they do, those notifications can appear on a desktop in a way that’s nearly identical to a mobile push notification.
As with those for mobile devices, web push notifications allow you to send customized, relevant content to web users, even if they’re not currently on your site or using your web app. This allows you to route users to content, return them to their cart, ask them to adjust a critical setting, or anything else needed for your use case.
How do Push Notifications fit in with PubNub?
Push notifications normally work by having your application register user devices. A service provider such as Apple Push Notification service (APNs) for iOS devices and Firebase Cloud Messaging (FCM) for Android devices will then deliver messages to these registered or certified devices that use your application. To be able to use these service providers in your application to deliver push notifications, you would have to integrate each framework into your application. This means that you’ll need to include the extra libraries, SDKs, and code handling for each type of device that you want to send push notifications.
However, PubNub supports push notifications with the same SDK that you normally use to integrate your application with PubNub’s real-time messaging. Since the application has already registered the device, you just need to provide the push registration token and format the message payload, which contains a reserved section for push notifications. You can then add your iOS, Android, and web notifications in one call, without needing to depend on the frameworks and libraries of the other services - all you need to do is include information in the payload, and let PubNub handle the rest.
1
PubNub can perform this action by acting as a bridge between your application and the service. This feature is called PubNub’s Mobile Push Gateway.
PubNub’s Mobile Push Gateway
When PubNub’s Mobile Push Gateway is enabled, you may associate unique mobile device tokens to PubNub channel names. Once this association is made and when a message is published to a channel, all associated devices will receive that message via their associated FCM or APNs providers. Behind the scenes, the PubNub Mobile Push Gateway establishes an authenticated connection to the push providers based on your registered configuration.
Push notifications are delivered in the following manner:
The application publishes to PubNub via a channel that has a device registered to receive push notifications with the push payload.
PubNub inspects the payload. If a specific mobile payload is found, it checks for device registrations on the channel. If found, PubNub forwards the push to that service provider (FCM/APNs).
FCM/APNs providers then deliver the push notification to one or more devices.
This means that PubNub does not deliver push notifications to devices directly - we forward them to the service provider based on if the specific mobile payload is found, and if the device is registered on that channel.
You might be wondering then - why bother to use PubNub if we are not delivering the push notifications and using the service providers to do this? As we discussed earlier, you can send a push to multiple device types (APNs or FCM) with one function call by including these different types in the same payload that you are using for in-app messaging - no need to have to integrate with the different frameworks and their SDKs, and send multiple calls to deliver push notifications to the different service providers.
Push Notifications and PubNub’s Real time Messaging
You might be asking yourself: “Why wouldn’t I just use push notifications for messaging instead of in-app messaging if I need to always be connected to the internet and won’t work if the app is in the background or closed?” You certainly could do it this way. However, there are severe limitations for the type/size of the push notification, high-latency/delivery, and user settings: your users would have a poor and frustrating experience.
Size and Types of Messages
Push notifications are meant to be fairly small in size. Both FCM and APNs are based on the XMPP protocol, which means that the message types are XML-based and generally heavyweight. FCM message sizes are restricted to 4KB and APNs message sizes are restricted to 2KB in size. This means that you have to be careful with exceeding the maximum size for the payload. PubNub supports messages up to 32KB in size, and can deliver varying message types, including text, images, and emojis.
High Latency and Delivery
As mentioned earlier, both FCM and APNs are based on the XMPP protocol. While a powerful protocol for real time communication platforms, these service providers can take a long time to deliver these push notifications, as it is dependent on their services. In fact, FCM will throttle your messages, limiting the speed and amount of messages your users can receive. If the device that is the intended recipient of a push notification is unreachable by the push service within the expiry window for the push notification, then the push notification will be lost or silently consumed. Push notification networks also generally cannot acknowledge that the push notification was successfully received on the device (no bi-directional communication). PubNub’s communication platform is fast and reliable, and missed messages on the PubNub Network can be retrieved using PubNub’s Message Persistence Feature, and can thus be successfully received by the recipient.
User Settings
Even if the service provider can successfully deliver the push notification on time, many push notifications may never even reach the intended users. This may not be the fault of the service providers; it can be caused due to user settings. Users can choose to place notifications in silent mode or even opt-out of push notifications. This means that they won’t see or even be properly notified of these push notifications. While there are companies like Airship and Netcore that provide services to help increase the success rate of push notifications, they can still be denied based on user settings.
Due to these issues, PubNub uses push notifications as a fall-back mechanism: push notifications can then be used to notify users about news, updates, or messages in the background or when the app is closed, even if there is no direct PubNub connection. Using both real-time messaging and push notifications on the PubNub Network means that you can take advantage of low-latency and reliable in-app messaging, and any missed notifications can be retrieved at a later time.
Setting up Push Notifications with PubNub
Normally, the first step to use push notifications is to get set up with the provider services. To do this, developers register their app with the necessary push notification services for their target platforms. They ensure that user devices that install their app also register with that service to receive notifications. Then, it’s just a matter of sending the notifications, which has two parts: getting the information that will go into your notification, then sending the notification itself to the desired endpoint, whether it’s a desktop device, browser, or mobile app.
PubNub simplifies this process into a single, seamless activity, and makes it easy to automatically align your in-app and push activity. With our solutions for mobile push notifications, you don’t need to develop push notifications separately from in-app alerts or activity. Instead, our easy-to-use APIs let you automatically trigger push notifications from real-time messages for users that are offline, or have simply put your app in the background.
With PubNub, it’s easy to get push notifications up and running in your app. Our in-depth documentation walks you through each step, but we’ll summarize the simple process right here:
Register your app with APNs for iOS devices and FCM for Android Devices.
Enable the mobile push gateway feature for your app in the PubNub Admin Portal and upload your FCM API Key and/or the APNS Certification. (Documentation for FCM and iOS)
When users install your app, register that device with its respective push notification provider and obtain a device token for that device from APNs or FCM.
In the client-side application code using that SDK, register the device with specific PubNub channels using the device token.
Implement code to receive push notifications.
Send your push notifications by formatting the payload to meet the needs of your targeted platforms:
1
After you send your message, let PubNub handle the rest! PubNub lets you define the contents of your push notification right alongside your real-time messages, so you don’t have to spend additional development time just to set up alerts, and you don’t have to worry about keeping them in sync and accurate to in-app activity. Instead, with a single message, you can reach both active and out-of-app users with engaging, relevant content.
Follow along with our Android Kotlin tutorial if you would like to follow a detailed, step-by-step guide on adding push notifications to a chat application using PubNub.
Best Practices
Because of the volume of messages sent in an active app, it can be difficult to effectively sync real-time activity, in-app alerts, and push notifications. Developers often need to make considerations not to issue push alerts while users are active in the app to avoid damaging the user experience with unnecessary notifications.
You also need to consider which activity to send as push payloads. For example, if every message in a group chat channel triggers an automated message, users may quickly opt out of push notifications entirely. In a related sense, you should decide how and whether to automatically limit app notifications based on user properties like user time zones.
Overall, manually managing real-time, in-app activity, and push notifications separately can become a burden for your developers. Developing, managing, and supporting multiple services (and streams of information) can quickly become a burden on time and resources better spent elsewhere.
Troubleshooting
You can debug push notification issues by inspecting the messages published to a {channelName}-pndebug
channel. For example, if you publish a message on channel ch1
, the corresponding debug channel is ch1-pndebug
.
You can subscribe to a debug channel using the PubNub Debug Console to troubleshoot push errors like invalid device tokens or invalid push payloads. For more information about push troubleshooting, refer to our Mobile Push Troubleshooting documentation.
If you are continuing to experience issues, please reach out to PubNub’s support team and include the results of the tests documented in the troubleshooting documentation.
What's Next?
Push notifications are a great way to notify online and offline users about appointments, updates, previews of conversations, special offers, and so much more. Combine this with PubNub’s real-time communication platform, and you have a complete application experience, giving you a way to continually engage and offer value to your users. PubNub’s Mobile Push Gateway makes it easy to send push notifications to your users, by acting as a bridge between the service providers and your application, meaning you don’t have to use different frameworks and SDKs to send different types of push notifications: you can do it all in one place, and let PubNub take care of the rest.
If you’re ready to start sending relevant, real-time, and precise push notifications to your users, check out our documentation and tutorials on push notifications. If you would like more personal assistance, please reach out to us. We’ll walk you through the best way to add push notifications using our platform, and will work with you to get up and running with alerts that will inform, energize, and engage your users.