Mobile Push Notifications API for Python-Asyncio 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).

Use Mobile Push Notifications to eliminate the need to develop, configure, and maintain server-side components for third-party push notification providers.

To learn more, read about Mobile Push Notifications.

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)

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
Push type. One of: PNPushType.GCM, PNPushType.FCM, PNPushType.APNS2.
channels *
Type: List
Default:
n/a
Channels to enable for push notifications.
device_id *
Type: String
Default:
n/a
Device token used for push notifications.
topic
Type: String
Default:
n/a
APNs topic (bundle identifier).
environment
Type: String
Default:
PNPushEnvironment.DEVELOPMENT
APNs environment.

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.
# for FCM/GCM
import asyncio
import os
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub_asyncio import PubNubAsyncio
from pubnub.enums import PNPushType, PNPushEnvironment
from pubnub.exceptions import PubNubException


async def add_device_to_channel(pubnub: PubNubAsyncio, push_type, topic=None, environment=None):
try:
# Add Device to Channels for Push Notifications
kwargs = {
"push_type": push_type,
"channels": ["ch1", "ch2", "ch3"],
show all 53 lines

Returns

No payload is returned. Check status.is_error() on the status object.

List push notifications channels for a device

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.

Get all channels with push notifications for the specified push token.

Method(s)

pubnub.list_push_channels() \
.push_type(PNPushType) \
.device_id(String) \
.topic(String) \
.environment(PNPushEnvironment)
* required
ParameterDescription
push_type *
Type: PNPushType
Default:
n/a
Push type. One of: PNPushType.GCM, PNPushType.FCM, PNPushType.APNS2.
device_id *
Type: String
Default:
n/a
Device token used for push notifications.
topic
Type: String
Default:
n/a
APNs topic (bundle identifier). Required for APNS2.
environment
Type: String
Default:
PNPushEnvironment.DEVELOPMENT
APNs environment. Required for APNS2.

Sample code

List channels for device

# for FCM/GCM
from pubnub.enums import PNPushType

envelope = await pubnub.list_push_channels()\
.push_type(PNPushType.GCM)\
.device_id("deviceId")\
.future()

# for APNS2
from pubnub.enums import PNPushType

envelope = await pubnub.list_push_channels()\
.push_type(PNPushType.APNS2)\
.device_id("deviceId")\
.topic("myapptopic")\
show all 17 lines

Returns

Returns PNPushListProvisionsResult with:

MethodDescription
Channels
Type: List
Channels associated with push notifications.

Remove a device from push notifications channels

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 push notifications on selected channels. If channels is nil, the client removes push notifications from all channels for the push token.

Method(s)

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
Push type. One of: PNPushType.GCM, PNPushType.FCM, PNPushType.APNS2.
channels *
Type: List
Default:
n/a
Channels to disable for push notifications.
device_id *
Type: String
Default:
n/a
Device token used for push notifications.
topic
Type: String
Default:
n/a
APNs topic (bundle identifier).
environment
Type: String
Default:
PNPushEnvironment.DEVELOPMENT
APNs environment.

Sample code

Remove device from channel

# for FCM/GCM
from pubnub.enums import PNPushType

envelope = await pubnub.remove_channels_from_push()\
.push_type(PNPushType.GCM)\
.channels("ch1", "ch2", "ch3")\
.device_id("deviceId")\
.future()

# for APNS2
from pubnub.enums import PNPushType

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

Returns

No payload is returned. Check status.is_error() on the status object.

Remove a device from all push notifications channels

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 push notifications from all channels registered for the specified push token.

Method(s)

pubnub.remove_device_from_push() \
.push_type(PNPushType) \
.device_id(String) \
.topic(String) \
.environment(PNPushEnvironment)
* required
ParameterDescription
push_type *
Type: PNPushType
Default:
n/a
Push type. One of: PNPushType.GCM, PNPushType.FCM, PNPushType.APNS2, PNPushType.MPNS.
device_id *
Type: String
Default:
n/a
Device token used for push notifications.
topic
Type: String
Default:
n/a
APNs topic (bundle identifier). Required for APNS2.
environment
Type: String
Default:
PNPushEnvironment.DEVELOPMENT
APNs environment. Required for APNS2.

Sample code

Remove all mobile push notifications

# for FCM/GCM
from pubnub.enums import PNPushType

envelope = await pubnub.remove_device_from_push()\
.push_type(PNPushType.GCM)\
.device_id("deviceId")\
.future()

# for APNS2
from pubnub.enums import PNPushType

envelope = await pubnub.remove_device_from_push()\
.push_type(PNPushType.APNS2)\
.device_id("deviceId")\
.topic("myapptopic")\
show all 17 lines

Rest response from server

No payload is returned. Check status.is_error() on the status object.

Last updated on