Mobile Push Notifications API for C# 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).

To learn more, read about Mobile Push Notifications.

Request execution

Use try/catch when working with the C# SDK.

If a request has invalid parameters (for example, a missing required field), the SDK throws an exception. If the request reaches the server but fails (server error or network issue), the error details are available in the returned status.

1try
2{
3 PNResult<PNPublishResult> publishResponse = await pubnub.Publish()
4 .Message("Why do Java developers wear glasses? Because they can't C#.")
5 .Channel("my_channel")
6 .ExecuteAsync();
7
8 PNStatus status = publishResponse.Status;
9
10 Console.WriteLine("Server status code : " + status.StatusCode.ToString());
11}
12catch (Exception ex)
13{
14 Console.WriteLine($"Request can't be executed due to error: {ex.Message}");
15}

Add a device to a push notifications channel​

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)​

1pubnub.AddPushNotificationsOnChannels()
2 .PushType(PNPushType)
3 .Channels(Array)
4 .DeviceId(string)
5 .Environment(PushEnvironment)
6 .Topic(string)
7 .QueryParam(Dictionary<string,object>)
required
PushType required#
Type: PNPushType
Push type. One of: PNPushType.FCM or PNPushType.APNS2
Channels required#
Type: Array
Channels to enable for push notifications.
DeviceId required#
Type: string
Device ID (push token).
Environment#
Type: PushEnvironment
APNs environment (Development, Production).
Topic#
Type: string
APNs topic (bundle identifier).
QueryParam#
Type: Dictionary<string, object>
Name/value pairs to send as query parameters.
AsyncDeprecated#
Type: PNCallback
Deprecated; prefer ExecuteAsync.
Execute required#
Type: PNCallback
Callback of type PNPushAddChannelResult.
ExecuteAsync#
Type: None
Returns PNResult<PNPushAddChannelResult>.

Sample code​

Add device to channel​

1using PubnubApi;
2
3public class PushNotificationCallback : PNCallback<PNPushAddChannelResult>
4{
5 public override void OnResponse(PNPushAddChannelResult result, PNStatus status)
6 {
7 if (!status.Error && result != null)
8 {
9 Console.WriteLine("Push notifications added to channels successfully.");
10 }
11 else
12 {
13 Console.WriteLine("Failed to add push notifications: " + pubnub.JsonPluggableLibrary.SerializeToJsonString(status));
14 }
15 }
show all 60 lines

Returns​

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

List push notifications channels for a device​

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)​

1pubnub.AuditPushChannelProvisions()
2 .DeviceId(string)
3 .PushType(PNPushType)
4 .Environment(PushEnvironment)
5 .Topic(string)
6 .QueryParam(Dictionary<string,object>)
required
DeviceId required#
Type: string
Device ID (push token).
PushType required#
Type: PNPushType
Push type. One of: PNPushType.FCM or PNPushType.APNS2
Environment#
Type: PushEnvironment
APNs environment (Development, Production).
Topic#
Type: string
APNs topic (bundle identifier).
QueryParam#
Type: Dictionary<string, object>
Name/value pairs to send as query parameters.
AsyncDeprecated#
Type: PNCallback
Deprecated; prefer ExecuteAsync.
Execute required#
Type: PNCallback
Callback of type PNPushListProvisionsResult.
ExecuteAsync#
Type: None
Returns PNResult<PNPushListProvisionsResult>.

Sample code​

List channels for device​

1using PubnubApi;
2
3// Configuration
4PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
5{
6 SubscribeKey = "demo",
7 PublishKey = "demo",
8 Secure = true
9};
10
11// Initialize PubNub
12Pubnub pubnub = new Pubnub(pnConfiguration);
13
14// for FCM/GCM
15pubnub.AuditPushChannelProvisions()
show all 32 lines

Returns​

Returns PNPushListProvisionsResult with:

Property NameTypeDescription
ChannelsList<string>Channels associated with push notifications.

Remove a device from push notifications channels​

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.

Method(s)​

1pubnub.RemovePushNotificationsFromChannels()
2 .DeviceId(string)
3 .Channels(Array)
4 .PushType(PNPushType)
5 .Environment(PushEnvironment)
6 .Topic(string)
7 .QueryParam(Dictionary<string,object>)
required
DeviceId required#
Type: string
Device ID (push token).
Channels required#
Type: Array
Channels to disable for push notifications.
PushType required#
Type: PNPushType
Push type. One of: PNPushType.FCM or PNPushType.APNS2
Environment#
Type: PushEnvironment
APNs environment (Development, Production).
Topic#
Type: string
APNs topic (bundle identifier).
QueryParam#
Type: Dictionary<string, object>
Name/value pairs to send as query parameters.
AsyncDeprecated#
Type: PNCallback
Deprecated; prefer ExecuteAsync.
Execute required#
Type: PNCallback
Callback of type PNPushRemoveChannelResult.
ExecuteAsync#
Type: None
Returns PNResult<PNPushRemoveChannelResult>.

Sample code​

Remove device from channel​

1using PubnubApi;
2
3// Configuration
4PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
5{
6 SubscribeKey = "demo",
7 PublishKey = "demo",
8 Secure = true
9};
10
11// Initialize PubNub
12Pubnub pubnub = new Pubnub(pnConfiguration);
13
14// for FCM/GCM
15pubnub.RemovePushNotificationsFromChannels()
show all 42 lines

Returns​

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

Remove a device from all push notifications channels​

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)​

1pubnub.RemoveAllPushNotificationsFromDeviceWithPushToken()
2 .DeviceId(string)
3 .PushType(PNPushType)
4 .Environment(PushEnvironment)
5 .Topic(string)
6 .QueryParam(Dictionary<string,object>)
required
DeviceId required#
Type: string
Device ID (push token).
PushType required#
Type: PNPushType
Push type. One of: PNPushType.FCM or PNPushType.APNS2
Environment#
Type: PushEnvironment
APNs environment (Development, Production).
Topic#
Type: string
APNs topic (bundle identifier).
QueryParam#
Type: Dictionary<string, object>
Name/value pairs to send as query parameters.
AsyncDeprecated#
Type: PNCallback
Deprecated; prefer ExecuteAsync.
Execute required#
Type: PNCallback
Callback of type PNPushRemoveAllChannelsResult.
ExecuteAsync#
Type: None
Returns PNResult<PNPushRemoveAllChannelsResult>.

Sample code​

Remove all mobile push notifications​

1using PubnubApi;
2
3// Configuration
4PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
5{
6 SubscribeKey = "demo",
7 PublishKey = "demo",
8 Secure = true
9};
10
11// Initialize PubNub
12Pubnub pubnub = new Pubnub(pnConfiguration);
13
14// for FCM/GCM
15pubnub.RemoveAllPushNotificationsFromDeviceWithPushToken()
show all 30 lines

Returns​

Returns PNPushRemoveAllChannelsResult with:

Property NameTypeDescription
PNPushRemoveAllChannelsResultObjectEmpty object on success.
PNStatusObjectRequest status (error or success).

Was this page useful?

Last updated on