Push Notifications

How to send push notifications

0 MIN READ • PubNub Labs Team on Feb 26, 2025
How to send push notifications

To send a push notification, your app collects a unique device token from the user. Your backend server stores this token and uses a service like Firebase Cloud Messaging (FCM) or Apple Push Notification Service (APNs) to send messages.

In the foreground, the app displays notifications instantly.

In the background or closed, iOS and Android impose restrictions:

  • iOS (APNs): Notifications are delivered but may not trigger background execution unless the app has special permissions (e.g., VoIP, location updates). Standard notifications appear in the notification center but won’t execute background tasks unless configured as silent notifications (update app data in the background without alerting the user, useful for syncing or content updates)
  • Android (FCM): Notifications can wake the app in the background if configured as high-priority messages, but battery optimizations (Doze Mode that limits background processes) may delay or restrict delivery. Silent notifications require a foreground service for background processing.

Steps to send push notifications

  1. Use Firebase Cloud Messaging (FCM) or Apple Push Notification Service (APNs).
  2. Get device tokens from users and store them securely.
  3. Set up a backend server to send notifications via FCM/APNs APIs.
  4. Format payload (title, message, data) in JSON.
  5. Authenticate using API keys (FCM) or certificates (APNs).
  6. Handle received notifications in the app (foreground/background logic)

Sending push notifications with PubNub

Integrating PubNub for push notifications allows you to deliver real-time mobile and desktop alerts across Android, iOS, and Web platforms. 

Want to give it a try?

see our Push Notifications DEMO

or check Push Notifications API examples

Overview of PubNub Push Notifications

PubNub's Mobile Push Gateway bridges native message publishing with third-party push notification services, including Apple Push Notification service (APNs) and Firebase Cloud Messaging (FCM). This integration enables you to send push alerts to users even when they are offline or not actively using your app.

Setting Up PubNub Push Notifications

Step 1: Create a PubNub Account and Configure Keys

  1. Sign Up: Create an account on the PubNub Admin Portal.
  2. Create an App: Within the dashboard, create a new app to obtain your Publish Key and Subscribe Key.
  3. Enable Push Notifications: Navigate to your app's Keyset settings and enable Mobile Push Notifications.

Step 2: Integrate with Push Notification Services

  • For Android (FCM):
  • Firebase Setup: Register your app with Firebase to obtain the google-services.json file.
  • PubNub Configuration: In the PubNub Admin Portal, add your FCM Server Key to enable communication between PubNub and FCM.
  • For iOS (APNs):
  • Apple Developer Setup: Configure your app to support push notifications and obtain the necessary APNs certificates.
  • PubNub Configuration: Upload your APNs certificate to the PubNub Admin Portal to establish a connection between PubNub and APNs.

Step 3: Register Device Tokens with PubNub

Each device registers its unique push token with PubNub and associates it with specific channels. This ensures that when a message is published to a channel, all devices registered to that channel receive the notification.

Example for Android (Java):

PNConfiguration initializes with UUIDs, which should ideally be persistent and unique per device/user to ensure consistent push behavior.

Example for iOS (Swift):

Step 4: Publishing Messages

To send a push notification, publish a message to the desired channel. PubNub will forward this message to the associated push notification service (FCM or APNs), and deliver it to the device.

Java Example:

5. Managing Push Subscription Removal

To prevent unwanted notifications, devices should unregister from channels when push notifications are no longer needed, such as when a user opts out or logs out. Use the removePushNotificationsFromChannels method to remove a device from specific channels:

Example for Android (Java):

Example for iOS (Swift):

Error handling

In a production environment, robust error handling is essential to diagnose and recover from push registration failures.

Instead of simply logging errors, developers should implement retry mechanisms, alerting systems, or user prompts to reattempt registration. For instance, if a device token is invalid or the push service is temporarily unavailable, the app could queue the request and retry after a short delay. Additionally, for Swift, handling failures gracefully—such as prompting users to check their network connection or refreshing the device token—can improve reliability. Implementing detailed logging and monitoring tools can also help identify recurring issues and optimize push notification delivery.

Token Expiration: Regularly check and update the device’s push token. If expired or invalid, re-register to obtain a new token for uninterrupted delivery.

When an app is uninstalled and reinstalled, its push token changes. The app needs to re-register with the push service (APNs or FCM) to obtain a new token for push notifications.

Technical Details

Message Payloads: when publishing messages intended for push notifications, ensure the payload aligns with the requirements of the target platform:

  • FCM: Use the "pn_gcm" key in your message payload.
  • APNs: Use the "pn_apns" key in your message payload.

JSON Example Payload:

API Limits

PubNub enforces certain limits to ensure optimal performance:

  • Requests Per Second (RPS): API requests are limited to 25 requests per second (RPS). If your application needs a higher limit, reach out to PubNub Support
  • Publish Rate: recommended to publish 10-15 messages per second per channel.

Handling Exceeded Limits

If these limits are exceeded, PubNub does not drop messages immediately. Instead, messages may be queued temporarily and delivered once the rate falls below the limit. However, excessive queuing can introduce delays, and in extreme cases, messages might expire if they cannot be processed in time. For time-sensitive applications like real-time chat or live event updates, it’s crucial to implement rate limiting and batching strategies to optimize message delivery. If your use case requires consistently higher throughput, consider sharding messages across multiple channels or requesting a higher rate limit from PubNub Support.

Potential Delays and Failure Points

Several factors can impact push notification delivery beyond PubNub's API limits. APNs device tokens can expire, requiring the app to request and update them periodically. FCM enforces throttling if too many messages are sent to the same device shortly, leading to delays or dropped notifications. Additionally, push tokens can change over time, especially after an app reinstall or device OS update. To ensure reliable delivery, apps should handle token updates by re-registering devices when necessary and implementing error handling for expired or invalid tokens. Monitoring delivery logs and implementing retry mechanisms can further help mitigate these issues.

Latency

PubNub is designed for low-latency message delivery:

  • Average Latency: 50% of messages are delivered in under 20 milliseconds.
  • Intranet Latency: Below 1 millisecond.
  • WAN Latency: Sub-30 milliseconds.