Mobile Push API for Python-Asyncio SDK
The Mobile Push Notifications API of the PubNub SDK for Python-Asyncio 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.
A builder sends its request when you await its future(). A request that fails, or fails validation, does not raise an exception. future() resolves with a PubNubAsyncioException whose result is None and whose status holds the error. Call is_error() on the awaited value before you read result.
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().
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-Asyncio.
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() resolves with an AsyncioEnvelope 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
1await pubnub.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).future() -> AsyncioEnvelope
Add device to channel (Builder Pattern)
1from pubnub.enums import PNPushEnvironment, PNPushType
2
3# for FCM
4envelope = await pubnub.add_channels_to_push() \
5 .push_type(PNPushType.FCM) \
6 .channels(["ch1", "ch2", "ch3"]) \
7 .device_id("deviceId") \
8 .future()
9
10# for APNS2
11envelope = await pubnub.add_channels_to_push() \
12 .push_type(PNPushType.APNS2) \
13 .channels(["ch1", "ch2", "ch3"]) \
14 .device_id("5646e7e88187c402636cf1a838175a1c3f5ccdff69d071e68630a73ab1a4955a") \
15 .topic("myapptopic") \
show all 22 linesAdd device to channel (Named Arguments)
1from pubnub.enums import PNPushEnvironment, PNPushType
2
3kwargs = {
4 "push_type": PNPushType.FCM,
5 "channels": ["ch1", "ch2", "ch3"],
6 "device_id": "deviceId",
7}
8envelope = await pubnub.add_channels_to_push(**kwargs).future()
9if envelope.is_error():
10 print(f"Error: {envelope.status.error_data}")
11
12kwargs = {
13 "push_type": PNPushType.APNS2,
14 "channels": ["ch1", "ch2", "ch3"],
15 "device_id": "5646e7e88187c402636cf1a838175a1c3f5ccdff69d071e68630a73ab1a4955a",
show all 21 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-Asyncio.
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
Returns
list_push_channels() resolves with an AsyncioEnvelope 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
1await pubnub.list_push_channels(
2 device_id: str,
3 push_type: PNPushType,
4 topic: Optional[str] = None,
5 environment: Optional[str] = None
6).future() -> AsyncioEnvelope
List channels for device
List channels for device
1from pubnub.enums import PNPushEnvironment, PNPushType
2
3# for FCM
4envelope = await pubnub.list_push_channels() \
5 .push_type(PNPushType.FCM) \
6 .device_id("deviceId") \
7 .future()
8if envelope.is_error():
9 print(f"Error: {envelope.status.error_data}")
10else:
11 fcm_channels = envelope.result.channels
12
13# for APNS2
14envelope = await pubnub.list_push_channels() \
15 .push_type(PNPushType.APNS2) \
show all 23 linesRemove 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-Asyncio.
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
Returns
remove_channels_from_push() resolves with an AsyncioEnvelope 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
1await pubnub.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).future() -> AsyncioEnvelope
Remove device from channel
Remove device from channel
1from pubnub.enums import PNPushEnvironment, PNPushType
2
3# for FCM
4envelope = await pubnub.remove_channels_from_push() \
5 .push_type(PNPushType.FCM) \
6 .channels(["ch1", "ch2", "ch3"]) \
7 .device_id("deviceId") \
8 .future()
9if envelope.is_error():
10 print(f"Error: {envelope.status.error_data}")
11
12# for APNS2
13envelope = await pubnub.remove_channels_from_push() \
14 .push_type(PNPushType.APNS2) \
15 .channels(["ch1", "ch2", "ch3"]) \
show all 21 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-Asyncio.
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
Returns
remove_device_from_push() resolves with an AsyncioEnvelope 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
1await pubnub.remove_device_from_push(
2 device_id: str,
3 push_type: PNPushType,
4 topic: Optional[str] = None,
5 environment: Optional[str] = None
6).future() -> AsyncioEnvelope
Remove all mobile push notifications
Remove all mobile push notifications
1from pubnub.enums import PNPushEnvironment, PNPushType
2
3# for FCM
4envelope = await pubnub.remove_device_from_push() \
5 .push_type(PNPushType.FCM) \
6 .device_id("deviceId") \
7 .future()
8if envelope.is_error():
9 print(f"Error: {envelope.status.error_data}")
10
11# for APNS2
12envelope = await pubnub.remove_device_from_push() \
13 .push_type(PNPushType.APNS2) \
14 .device_id("5646e7e88187c402636cf1a838175a1c3f5ccdff69d071e68630a73ab1a4955a") \
15 .topic("myapptopic") \
show all 19 lines