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
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)
Parameter | Description |
---|---|
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
# 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 linesReturns
No payload is returned. Check status.is_error()
on the status object.
List push notifications channels for a device
note
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)
Parameter | Description |
---|---|
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 linesReturns
Returns PNPushListProvisionsResult
with:
Method | Description |
---|---|
Channels Type: List | Channels associated with push notifications. |
Remove a device from push notifications channels
note
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)
Parameter | Description |
---|---|
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 linesReturns
No payload is returned. Check status.is_error()
on the status object.
Remove a device from all push notifications channels
note
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)
Parameter | Description |
---|---|
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 linesRest response from server
No payload is returned. Check status.is_error()
on the status object.