Mobile Push Notifications API for Python SDK

The Mobile Push Notifications feature connects native PubNub publishing to third-party push services. Supported services include Google Android FCM (Firebase Cloud Messaging) and Apple iOS APNs (Apple Push Notification service).

To learn more, read about Mobile Push Notifications.

Request execution and return values

You can decide whether to perform the Python SDK operations synchronously or asynchronously.

  • .sync() returns an Envelope object, which has two fields: Envelope.result, whose type differs for each API, and Envelope.status of type PnStatus.

    pubnub.publish() \
    .channel("myChannel") \
    .message("Hello from PubNub Python SDK") \
    .sync()
  • .pn_async(callback) returns None and passes the values of Envelope.result and Envelope.status to a callback you must define beforehand.

    def my_callback_function(result, status):
    print(f'TT: {result.timetoken}, status: {status.category.name}')

    pubnub.publish() \
    .channel("myChannel") \
    .message("Hello from PubNub Python SDK") \
    .pn_async(my_callback_function)

Add a device to a push notifications channel

note
Requires Mobile Push Notifications add-on

Enable Mobile Push Notifications for your key in the Admin Portal. See how to enable add-on features.

Enable mobile push notifications on a set of channels.

Method(s)

To run Adding Device to Channel you can use the following method(s) in the Python SDK:

pubnub.add_channels_to_push() \
.push_type(PNPushType) \
.channels(List) \
.device_id(String) \
.topic(String) \
.environment(PNPushEnvironment)
* required
ParameterDescription
push_type *
Type: PNPushType
Default:
n/a
The available push types. Accepted values: PNPushType.GCM, PNPushType.APNS2
channels *
Type: List
Default:
n/a
The channels to add the mobile push notifications to.
device_id *
Type: String
Default:
n/a
The device ID (token) to associate with the mobile push notifications.
topic
Type: String
Default:
n/a
The topic name for the notification. For the Apple platform, this is the application's bundle identifier. Required only if push_type is set to PNPushType.APNS2.
environment
Type: String
Default:
PNPushEnvironment.DEVELOPMENT
The environment where the device should manage the list of channels with enabled notifications. Required only if push_type is set to PNPushType.APNS2.

Sample code

Add device to channel

Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.
import os
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.enums import PNPushType, PNPushEnvironment
from pubnub.exceptions import PubNubException


def add_device_to_channel(pubnub: PubNub):
try:
# For FCM/GCM
pubnub.add_channels_to_push() \
.push_type(PNPushType.GCM) \
.channels(["ch1", "ch2", "ch3"]) \
.device_id("deviceId") \
.sync()
show all 48 lines

Returns

The add_channels_to_push() operation does not return actionable data. You can check the status object for the outcome by inspecting status.is_error().

List channels for device

note
Requires Mobile Push Notifications add-on

This method requires that the Mobile Push Notifications add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

List channels that have push notifications enabled for the specified device token.

Method(s)

Use the following method(s) in the Python SDK:

pubnub.list_push_channels() \
.push_type(PNPushType) \
.device_id(String) \
.topic(String) \
.environment(PNPushEnvironment)
* required
ParameterDescription
push_type *
Type: PNPushType
Default:
n/a
The available push types. Accepted values: PNPushType.GCM, PNPushType.APNS2
device_id *
Type: String
Default:
n/a
Device token.
topic
Type: String
Default:
n/a
APNs topic (bundle identifier). Required if push_type is PNPushType.APNS2.
environment
Type: String
Default:
PNPushEnvironment.DEVELOPMENT
APNs environment. Required if push_type is PNPushType.APNS2.

Sample code

List channels for device

from pubnub.enums import PNPushType, PNPushEnvironment

# for FCM/GCM
envelope = pubnub.list_push_channels() \
.push_type(PNPushType.GCM) \
.device_id("deviceId") \
.sync()

# for APNS2
envelope = pubnub.list_push_channels() \
.push_type(PNPushType.APNS2) \
.device_id("deviceId") \
.topic("myapptopic") \
.environment(PNPushEnvironment.DEVELOPMENT) \
.sync()

Returns

The list_push_channels() operation returns an Envelope which contains the following fields:

FieldTypeDescription
result
PNPushListProvisionsResult
A detailed object containing the result of the operation.
status
PNStatus
A status object with additional information.

PNPushListProvisionsResult

MethodDescription
Channels
Type: List
List of channels associated for mobile push notifications.

Remove device from channel

note
Requires Mobile Push Notifications add-on

Enable Mobile Push Notifications for your key in the Admin Portal. See how to enable add-on features.

Disable mobile push notifications on a set of channels.

Method(s)

Use the following method(s) in the Python SDK:

pubnub.remove_channels_from_push() \
.push_type(PNPushType) \
.channels(List) \
.device_id(String) \
.topic(String) \
.environment(PNPushEnvironment)
* required
ParameterDescription
push_type *
Type: PNPushType
Default:
n/a
The available push types. Accepted values: PNPushType.GCM, PNPushType.APNS2
channels *
Type: List
Default:
n/a
Channels to disable for push notifications.
device_id *
Type: String
Default:
n/a
Device token.
topic
Type: String
Default:
n/a
APNs topic (bundle identifier). Required if push_type is PNPushType.APNS2.
environment
Type: String
Default:
PNPushEnvironment.DEVELOPMENT
APNs environment. Required if push_type is PNPushType.APNS2.

Sample code

Remove device from channel

from pubnub.enums import PNPushType, PNPushEnvironment

# for FCM/GCM
envelope = pubnub.remove_channels_from_push() \
.push_type(PNPushType.GCM) \
.channels("ch1", "ch2", "ch3") \
.device_id("deviceId") \
.sync()

# for APNS2

envelope = pubnub.remove_channels_from_push() \
.push_type(PNPushType.APNS2) \
.channels("ch1", "ch2", "ch3") \
.device_id("deviceId") \
show all 18 lines

Returns

The remove_channels_from_push() operation does not return actionable data. You can check the status object for the outcome by inspecting status.is_error().

Remove all mobile push notifications

note
Requires Mobile Push Notifications add-on

Enable Mobile Push Notifications for your key in the Admin Portal. See how to enable add-on features.

Disable mobile push notifications from all channels registered with the specified device token.

Method(s)

Use the following method(s) in the Python SDK:

pubnub.remove_device_from_push() \
.push_type(PNPushType) \
.device_id(String) \
.topic(String) \
.environment(PNPushEnvironment)
* required
ParameterDescription
push_type *
Type: PNPushType
Default:
n/a
The available push types. Accepted values: PNPushType.GCM, PNPushType.MPNS, PNPushType.APNS2
device_id *
Type: String
Default:
n/a
Device token.
topic
Type: String
Default:
n/a
APNs topic (bundle identifier). Required if push_type is PNPushType.APNS2.
environment
Type: String
Default:
PNPushEnvironment.DEVELOPMENT
APNs environment. Required if push_type is PNPushType.APNS2.

Sample code

Remove all mobile push notifications

from pubnub.enums import PNPushType, PNPushEnvironment

# for FCM/GCM
envelope = pubnub.remove_device_from_push() \
.push_type(PNPushType.GCM) \
.device_id("deviceId") \
.sync()

# for APNS2
envelope = pubnub.remove_device_from_push() \
.push_type(PNPushType.APNS2) \
.device_id("deviceId") \
.topic("myapptopic") \
.environment(PNPushEnvironment.DEVELOPMENT) \
.sync()

Returns

The remove_device_from_push() operation does not return actionable data. You can check the status object for the outcome by inspecting status.is_error().

Last updated on