Mobile Push API for Python SDK

The Mobile Push Notifications API of the PubNub SDK for Python registers a device token on channels. PubNub forwards a message with a push payload to each device registered on its channel, through Google Android FCM (Firebase Cloud Messaging) or Apple iOS APNs (Apple Push Notification service). To learn more, read about Mobile Push Notifications.

Every parameter is also a builder method of the same name, so you can pass device_id as a keyword argument or chain .device_id(). A failed request raises a PubNubException from .sync(). With .pn_async(callback), the callback receives a status where status.is_error() returns True.

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.

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

    1def my_callback_function(result, status):
    2 print(f'TT: {result.timetoken}, status: {status.category.name}')
    3
    4pubnub.publish() \
    5 .channel("myChannel") \
    6 .message("Hello from PubNub Python SDK") \
    7 .pn_async(my_callback_function)

Add a device to a push notifications channel​

pubnub.add_channels_to_push() enables mobile push notifications for a device on a set of channels with a PubNub client for Python.

required
channels required#
Type: List[str]
Default: no default
Channels on which to enable mobile push notifications for the device. Pass a list of channel names. A single string is not accepted. Pass at least one channel. A missing value, an empty list, or a string fails validation with the message Channel missing.
device_id required#
Type: str
Default: no default
Device token that identifies the device. The push service issues the token when the app registers for notifications. For PNPushType.APNS2, the token is a hexadecimal string of any length, and the service rejects any other format. For PNPushType.FCM, the token is an opaque string and the service accepts any value. An empty value fails validation with the message Device ID is missing for push operation.
push_type required#
Type: PNPushType
Default: no default
Push service that delivers the notifications to the device. Accepted values: PNPushType.FCM, PNPushType.APNS2. PNPushType.GCM is deprecated. Use PNPushType.FCM instead. A missing value fails validation with the message Push Type is missing.
topic#
Type: str
Default: no default
APNs topic of the app, which is its bundle identifier. The APNs2 push service requires a topic. A request with PNPushType.APNS2 and no topic fails validation with the message Push notification topic is missing. Required only if push type is APNS2..
environment#
Type: str
Default: PNPushEnvironment.DEVELOPMENT
APNs environment in which the device is registered. Only the APNs2 push service uses the environment. Accepted values: PNPushEnvironment.DEVELOPMENT, PNPushEnvironment.PRODUCTION. The SDK applies the default only when push_type is PNPushType.APNS2.

Returns​

add_channels_to_push() returns an Envelope whose result is a PNPushAddChannelResult and whose status is a PNStatus.

PNPushAddChannelResult has no fields, and str(result) returns Channel successfully added.

Syntax

1pubnub.add_channels_to_push(
2 channels: List[str],
3 device_id: str,
4 push_type: PNPushType,
5 topic: Optional[str] = None,
6 environment: Optional[str] = None
7) -> AddChannelsToPush

List push notifications channels for a device​

pubnub.list_push_channels() returns the channels on which a device receives mobile push notifications, using a PubNub client for Python.

required
device_id required#
Type: str
Default: no default
Device token that identifies the device. The push service issues the token when the app registers for notifications. For PNPushType.APNS2, the token is a hexadecimal string of any length, and the service rejects any other format. For PNPushType.FCM, the token is an opaque string and the service accepts any value. An empty value fails validation with the message Device ID is missing for push operation.
push_type required#
Type: PNPushType
Default: no default
Push service that delivers the notifications to the device. Accepted values: PNPushType.FCM, PNPushType.APNS2. PNPushType.GCM is deprecated. Use PNPushType.FCM instead. A missing value fails validation with the message Push Type is missing.
topic#
Type: str
Default: no default
APNs topic of the app, which is its bundle identifier. The APNs2 push service requires a topic. A request with PNPushType.APNS2 and no topic fails validation with the message Push notification topic is missing. Required only if push type is APNS2..
environment#
Type: str
Default: PNPushEnvironment.DEVELOPMENT
APNs environment in which the device is registered. Only the APNs2 push service uses the environment. Accepted values: PNPushEnvironment.DEVELOPMENT, PNPushEnvironment.PRODUCTION. The SDK applies the default only when push_type is PNPushType.APNS2.

Returns​

list_push_channels() returns an Envelope whose result is a PNPushListProvisionsResult and whose status is a PNStatus.

channels#
Type: List[str]
The names of the channels with mobile push notifications enabled for the device. The list is empty when the device has none.

Syntax

1pubnub.list_push_channels(
2 device_id: str,
3 push_type: PNPushType,
4 topic: Optional[str] = None,
5 environment: Optional[str] = None
6) -> ListPushProvisions

Remove a device from push notifications channels​

pubnub.remove_channels_from_push() disables mobile push notifications for a device on the selected channels with a PubNub client for Python.

required
channels required#
Type: List[str]
Default: no default
Channels on which to disable mobile push notifications for the device. Pass a list of channel names. A single string is not accepted. Pass at least one channel. A missing value, an empty list, or a string fails validation with the message Channel missing.
device_id required#
Type: str
Default: no default
Device token that identifies the device. The push service issues the token when the app registers for notifications. For PNPushType.APNS2, the token is a hexadecimal string of any length, and the service rejects any other format. For PNPushType.FCM, the token is an opaque string and the service accepts any value. An empty value fails validation with the message Device ID is missing for push operation.
push_type required#
Type: PNPushType
Default: no default
Push service that delivers the notifications to the device. Accepted values: PNPushType.FCM, PNPushType.APNS2. PNPushType.GCM is deprecated. Use PNPushType.FCM instead. A missing value fails validation with the message Push Type is missing.
topic#
Type: str
Default: no default
APNs topic of the app, which is its bundle identifier. The APNs2 push service requires a topic. A request with PNPushType.APNS2 and no topic fails validation with the message Push notification topic is missing. Required only if push type is APNS2..
environment#
Type: str
Default: PNPushEnvironment.DEVELOPMENT
APNs environment in which the device is registered. Only the APNs2 push service uses the environment. Accepted values: PNPushEnvironment.DEVELOPMENT, PNPushEnvironment.PRODUCTION. The SDK applies the default only when push_type is PNPushType.APNS2.

Returns​

remove_channels_from_push() returns an Envelope whose result is a PNPushRemoveChannelResult and whose status is a PNStatus.

PNPushRemoveChannelResult has no fields, and str(result) returns Channel successfully removed.

Syntax

1pubnub.remove_channels_from_push(
2 channels: List[str],
3 device_id: str,
4 push_type: PNPushType,
5 topic: Optional[str] = None,
6 environment: Optional[str] = None
7) -> RemoveChannelsFromPush

Remove a device from all push notifications channels​

pubnub.remove_device_from_push() disables mobile push notifications for a device on all channels registered for its device token, using a PubNub client for Python.

required
device_id required#
Type: str
Default: no default
Device token that identifies the device. The push service issues the token when the app registers for notifications. For PNPushType.APNS2, the token is a hexadecimal string of any length, and the service rejects any other format. For PNPushType.FCM, the token is an opaque string and the service accepts any value. An empty value fails validation with the message Device ID is missing for push operation.
push_type required#
Type: PNPushType
Default: no default
Push service that delivers the notifications to the device. Accepted values: PNPushType.FCM, PNPushType.APNS2. PNPushType.GCM is deprecated. Use PNPushType.FCM instead. A missing value fails validation with the message Push Type is missing.
topic#
Type: str
Default: no default
APNs topic of the app, which is its bundle identifier. The APNs2 push service requires a topic. A request with PNPushType.APNS2 and no topic fails validation with the message Push notification topic is missing. Required only if push type is APNS2..
environment#
Type: str
Default: PNPushEnvironment.DEVELOPMENT
APNs environment in which the device is registered. Only the APNs2 push service uses the environment. Accepted values: PNPushEnvironment.DEVELOPMENT, PNPushEnvironment.PRODUCTION. The SDK applies the default only when push_type is PNPushType.APNS2.

Returns​

remove_device_from_push() returns an Envelope whose result is a PNPushRemoveAllChannelsResult and whose status is a PNStatus.

PNPushRemoveAllChannelsResult has no fields, and str(result) returns All channels successfully removed.

Syntax

1pubnub.remove_device_from_push(
2 device_id: str,
3 push_type: PNPushType,
4 topic: Optional[str] = None,
5 environment: Optional[str] = None
6) -> RemoveDeviceFromPush

Was this page useful?

Last updated on