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>)
PushTyperequired#- Push type. One of:
PNPushType.FCMorPNPushType.APNS2 Channelsrequired#- Channels to enable for push notifications.
DeviceIdrequired#- Device ID (push token).
Environment#- APNs environment (
Development,Production). Topic#- APNs topic (bundle identifier).
QueryParam#- Name/value pairs to send as query parameters.
Deprecated#AsyncDeprecated; preferExecuteAsync.Executerequired#- Callback of type
PNPushAddChannelResult. ExecuteAsync#- 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 linesReturns
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>)
DeviceIdrequired#- Device ID (push token).
PushTyperequired#- Push type. One of:
PNPushType.FCMorPNPushType.APNS2 Environment#- APNs environment (
Development,Production). Topic#- APNs topic (bundle identifier).
QueryParam#- Name/value pairs to send as query parameters.
Deprecated#AsyncDeprecated; preferExecuteAsync.Executerequired#- Callback of type
PNPushListProvisionsResult. ExecuteAsync#- 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 linesReturns
Returns PNPushListProvisionsResult with:
| Property Name | Type | Description |
|---|---|---|
Channels | List<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>)
DeviceIdrequired#- Device ID (push token).
Channelsrequired#- Channels to disable for push notifications.
PushTyperequired#- Push type. One of:
PNPushType.FCMorPNPushType.APNS2 Environment#- APNs environment (
Development,Production). Topic#- APNs topic (bundle identifier).
QueryParam#- Name/value pairs to send as query parameters.
Deprecated#AsyncDeprecated; preferExecuteAsync.Executerequired#- Callback of type
PNPushRemoveChannelResult. ExecuteAsync#- 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 linesReturns
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>)
DeviceIdrequired#- Device ID (push token).
PushTyperequired#- Push type. One of:
PNPushType.FCMorPNPushType.APNS2 Environment#- APNs environment (
Development,Production). Topic#- APNs topic (bundle identifier).
QueryParam#- Name/value pairs to send as query parameters.
Deprecated#AsyncDeprecated; preferExecuteAsync.Executerequired#- Callback of type
PNPushRemoveAllChannelsResult. ExecuteAsync#- 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 linesReturns
Returns PNPushRemoveAllChannelsResult with:
| Property Name | Type | Description |
|---|---|---|
PNPushRemoveAllChannelsResult | Object | Empty object on success. |
PNStatus | Object | Request status (error or success). |