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 anEnvelopeobject, which has two fields:Envelope.result, whose type differs for each API, andEnvelope.statusof typePnStatus.1pubnub.publish() \
2 .channel("myChannel") \
3 .message("Hello from PubNub Python SDK") \
4 .sync() -
.pn_async(callback)returnsNoneand passes the values ofEnvelope.resultandEnvelope.statusto 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
Mobile Push Notifications add-on
This method requires that the Mobile Push Notifications add-on is enabled for your key in the Admin Portal.
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. See Enable the features your app calls.
pubnub.add_channels_to_push() enables mobile push notifications for a device on a set of channels with a PubNub client for Python.
channelsrequired#- 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_idrequired#- 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. ForPNPushType.FCM, the token is an opaque string and the service accepts any value. An empty value fails validation with the messageDevice ID is missing for push operation. push_typerequired#- Push service that delivers the notifications to the device. Accepted values:
PNPushType.FCM,PNPushType.APNS2.PNPushType.GCMis deprecated. UsePNPushType.FCMinstead. A missing value fails validation with the messagePush Type is missing. topic#- 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#- 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 whenpush_typeisPNPushType.APNS2.
Sample code
Add device to channel
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.
Examples for Add a device to a push notifications channel
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
Add device to channel (Builder Pattern)
1from pubnub.enums import PNPushEnvironment, PNPushType
2
3# For FCM
4pubnub.add_channels_to_push() \
5 .push_type(PNPushType.FCM) \
6 .channels(["ch1", "ch2", "ch3"]) \
7 .device_id("deviceId") \
8 .sync()
9
10# For APNS2 device token should be hex
11pubnub.add_channels_to_push() \
12 .push_type(PNPushType.APNS2) \
13 .channels(["ch1", "ch2", "ch3"]) \
14 .device_id("5646e7e88187c402636cf1a838175a1c3f5ccdff69d071e68630a73ab1a4955a") \
15 .topic("myapptopic") \
show all 17 linesAdd device to channel (Named Arguments)
1from pubnub.enums import PNPushEnvironment, PNPushType
2
3# For FCM
4pubnub.add_channels_to_push(
5 push_type=PNPushType.FCM,
6 channels=["ch1", "ch2", "ch3"],
7 device_id="deviceId"
8).sync()
9
10# For APNS2
11pubnub.add_channels_to_push(
12 push_type=PNPushType.APNS2,
13 channels=["ch1", "ch2", "ch3"],
14 device_id="5646e7e88187c402636cf1a838175a1c3f5ccdff69d071e68630a73ab1a4955a",
15 topic="myapptopic",
show all 17 linesList push notifications channels for a device
Mobile Push Notifications add-on
This method requires that the Mobile Push Notifications add-on is enabled for your key in the Admin Portal.
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. See Enable the features your app calls.
pubnub.list_push_channels() returns the channels on which a device receives mobile push notifications, using a PubNub client for Python.
device_idrequired#- 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. ForPNPushType.FCM, the token is an opaque string and the service accepts any value. An empty value fails validation with the messageDevice ID is missing for push operation. push_typerequired#- Push service that delivers the notifications to the device. Accepted values:
PNPushType.FCM,PNPushType.APNS2.PNPushType.GCMis deprecated. UsePNPushType.FCMinstead. A missing value fails validation with the messagePush Type is missing. topic#- 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#- 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 whenpush_typeisPNPushType.APNS2.
Sample code
List channels for device
Returns
list_push_channels() returns an Envelope whose result is a PNPushListProvisionsResult and whose status is a PNStatus.
channels#- The names of the channels with mobile push notifications enabled for the device. The list is empty when the device has none.
Examples for List push notifications channels for a device
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
List channels for device (Builder Pattern)
1from pubnub.enums import PNPushEnvironment, PNPushType
2
3# For FCM
4envelope = pubnub.list_push_channels() \
5 .push_type(PNPushType.FCM) \
6 .device_id("deviceId") \
7 .sync()
8fcm_channels = envelope.result.channels
9
10# For APNS2
11envelope = pubnub.list_push_channels() \
12 .push_type(PNPushType.APNS2) \
13 .device_id("5646e7e88187c402636cf1a838175a1c3f5ccdff69d071e68630a73ab1a4955a") \
14 .topic("myapptopic") \
15 .environment(PNPushEnvironment.DEVELOPMENT) \
show all 17 linesList channels for device (Named Arguments)
1from pubnub.enums import PNPushEnvironment, PNPushType
2
3# For FCM
4envelope = pubnub.list_push_channels(push_type=PNPushType.FCM, device_id="deviceId").sync()
5fcm_channels = envelope.result.channels
6
7# For APNS2
8envelope = pubnub.list_push_channels(
9 push_type=PNPushType.APNS2,
10 device_id="5646e7e88187c402636cf1a838175a1c3f5ccdff69d071e68630a73ab1a4955a",
11 topic="myapptopic",
12 environment=PNPushEnvironment.DEVELOPMENT
13).sync()
14apns2_channels = envelope.result.channels
Remove a device from push notifications channels
Mobile Push Notifications add-on
This method requires that the Mobile Push Notifications add-on is enabled for your key in the Admin Portal.
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. See Enable the features your app calls.
pubnub.remove_channels_from_push() disables mobile push notifications for a device on the selected channels with a PubNub client for Python.
channelsrequired#- 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_idrequired#- 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. ForPNPushType.FCM, the token is an opaque string and the service accepts any value. An empty value fails validation with the messageDevice ID is missing for push operation. push_typerequired#- Push service that delivers the notifications to the device. Accepted values:
PNPushType.FCM,PNPushType.APNS2.PNPushType.GCMis deprecated. UsePNPushType.FCMinstead. A missing value fails validation with the messagePush Type is missing. topic#- 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#- 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 whenpush_typeisPNPushType.APNS2.
Sample code
Remove device from channel
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.
Examples for Remove a device from push notifications channels
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 device from channel (Builder Pattern)
1from pubnub.enums import PNPushEnvironment, PNPushType
2
3# For FCM
4envelope = pubnub.remove_channels_from_push() \
5 .push_type(PNPushType.FCM) \
6 .channels(["ch1", "ch2", "ch3"]) \
7 .device_id("deviceId") \
8 .sync()
9
10# For APNS2
11envelope = pubnub.remove_channels_from_push() \
12 .push_type(PNPushType.APNS2) \
13 .channels(["ch1", "ch2", "ch3"]) \
14 .device_id("5646e7e88187c402636cf1a838175a1c3f5ccdff69d071e68630a73ab1a4955a") \
15 .topic("myapptopic") \
show all 17 linesRemove device from channel (Named Arguments)
1from pubnub.enums import PNPushEnvironment, PNPushType
2
3# For FCM
4envelope = pubnub.remove_channels_from_push(channels=["ch1", "ch2", "ch3"],
5 push_type=PNPushType.FCM,
6 device_id="deviceId") \
7 .sync()
8
9# For APNS2
10envelope = pubnub.remove_channels_from_push(
11 channels=["ch1", "ch2", "ch3"],
12 push_type=PNPushType.APNS2,
13 device_id="5646e7e88187c402636cf1a838175a1c3f5ccdff69d071e68630a73ab1a4955a",
14 topic="myapptopic",
15 environment=PNPushEnvironment.DEVELOPMENT
show all 16 linesRemove a device from all push notifications channels
Mobile Push Notifications add-on
This method requires that the Mobile Push Notifications add-on is enabled for your key in the Admin Portal.
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. See Enable the features your app calls.
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.
device_idrequired#- 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. ForPNPushType.FCM, the token is an opaque string and the service accepts any value. An empty value fails validation with the messageDevice ID is missing for push operation. push_typerequired#- Push service that delivers the notifications to the device. Accepted values:
PNPushType.FCM,PNPushType.APNS2.PNPushType.GCMis deprecated. UsePNPushType.FCMinstead. A missing value fails validation with the messagePush Type is missing. topic#- 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#- 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 whenpush_typeisPNPushType.APNS2.
Sample code
Remove all mobile push notifications
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.
Examples for Remove a device from all push notifications channels
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
Remove all mobile push notifications (Builder Pattern)
1from pubnub.enums import PNPushEnvironment, PNPushType
2
3# For FCM
4envelope = pubnub.remove_device_from_push() \
5 .push_type(PNPushType.FCM) \
6 .device_id("deviceId") \
7 .sync()
8
9# For APNS2
10envelope = pubnub.remove_device_from_push() \
11 .push_type(PNPushType.APNS2) \
12 .device_id("5646e7e88187c402636cf1a838175a1c3f5ccdff69d071e68630a73ab1a4955a") \
13 .topic("myapptopic") \
14 .environment(PNPushEnvironment.DEVELOPMENT) \
15 .sync()
Remove all mobile push notifications (Named Arguments)
1from pubnub.enums import PNPushEnvironment, PNPushType
2
3# For FCM
4envelope = pubnub.remove_device_from_push(device_id="deviceId", push_type=PNPushType.FCM).sync()
5
6# For APNS2
7envelope = pubnub.remove_device_from_push(
8 device_id="5646e7e88187c402636cf1a838175a1c3f5ccdff69d071e68630a73ab1a4955a",
9 push_type=PNPushType.APNS2,
10 topic="myapptopic",
11 environment=PNPushEnvironment.DEVELOPMENT
12).sync()