Push Notifications API for PubNub Dart SDK

PubNub's Mobile Push Gateway feature enables developers to bridge native PubNub publishing with 3rd-party push notification services including Google Android FCM (Firebase Cloud Messaging) and Apple iOS APNs (Apple Push Notification service).

By using the Mobile Push Gateway, developers can eliminate the need for developing, configuring, and maintaining additional server-side components for third-party push notification providers.

To learn more, read about Push Notifications.

Adding Device to Channel

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. Read the support page on enabling add-on features on your keys.

Description

Enable push notifications on provided set of channels.

Method(s)

To run Adding Device to Channel you can use the following method(s) in the Dart SDK:

pubnub.addPushChannels(
String deviceId,
PushGateway gateway,
Set<String> channels,
{String? topic,
Environment? environment,
Keyset? keyset,
String? using}
)
ParameterTypeRequiredDescription
deviceIdStringYesID of the device to add push notifications on.
gatewayPushGatewayYesEnums for the back-end to use for push. Available values: apns2 for APNs, gcm for FCM, and mpns for MPNS.
channelsSet<String>YesChannels to add push notifications for.
topicStringOptionalNotifications topic name (usually it is bundle identifier of application for Apple platform). Required only if gateway set to apns2.
environmentEnvironmentOptionalEnvironment within which device should manage list of channels with enabled notifications (works only if gateway set to apns2).
keysetKeysetOptionalOverride for the PubNub default keyset configuration.
usingStringOptionalKeyset name from the keysetStore to be used for this method call.

Basic Usage

Adding Device to Channel

// for non apns2
var result =
await pubnub.addPushChannels('A332C23D', PushGateway.gcm, {'my_channel'});

// for apns2
var result = await pubnub.addPushChannels(
'device-token', PushGateway.apns2, {'my_channel'},
topic: 'MyAppTopic', environment: Environment.development);

Returns

The addPushChannels() does not return actionable data. In case of an error, an exception with error details is thrown.

Listing Channels For Device

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. Read the support page on enabling add-on features on your keys.

Description

Request for all channels on which push notification has been enabled using specified pushToken.

Method(s)

To run Listing Channels For Device you can use the following method(s) in the Dart SDK:

pubnub.listPushChannels(
String deviceId,
PushGateway gateway,
{String? topic,
Environment? environment,
Keyset? keyset,
String? using}
)
ParameterTypeRequiredDescription
deviceIdStringYesID of the device to add push notifications on.
gatewayPushGatewayYesThe back-end to use for push. Available values: apns2 for APNs, gcm for FCM, and mpns for MPNS.
topicStringOptionalNotifications topic name (usually it is bundle identifier of application for Apple platform). Required only if pushType set to apns2.
environmentEnvironmentOptionalEnvironment within which device should manage list of channels with enabled notifications (works only if gateway set to apns2).
keysetKeysetOptionalOverride for the PubNub default keyset configuration.
usingStringOptionalKeyset name from the keysetStore to be used for this method call.

Basic Usage

Listing Channels For Device

// for non apns2
var result = await pubnub.listPushChannels('A332C23D', PushGateway.gcm);

// for apns2
var result = await pubnub.listPushChannels('device-token', PushGateway.apns2,
topic: 'MyAppTopic', environment: Environment.development);

Returns

The listPushChannels() operation returns a ListPushChannelsResult which contains the following operations:

MethodTypeDescription
channelsListList of channels associated for push notifications.

Removing Device From Channel

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. Read the support page on enabling add-on features on your keys.

Description

Disable push notifications on provided set of channels.

Method(s)

To Removing Device From Channel you can use the following method(s) in the Dart SDK:

pubnub.removePushChannels(
String deviceId,
PushGateway gateway,
Set<String> channels,
{String? topic,
Environment? environment,
Keyset? keyset,
String? using}
)
ParameterTypeRequiredDescription
deviceIdStringYesID of the device to remove push notifications from.
gatewayPushGatewayYesEnums for the back-end to use for push. Available values: apns2 for APNs, gcm for FCM, and mpns for MPNS.
channelsSet<String>YesThe channels to remove.
topicStringOptionalNotifications topic name (usually it is bundle identifier of application for Apple platform). Required only if gateway set to apns2.
environmentEnvironmentOptionalEnvironment within which device should manage list of channels with enabled notifications (works only if gateway set to apns2).
keysetKeysetOptionalOverride for the PubNub default keyset configuration.
usingStringOptionalKeyset name from the keysetStore to be used for this method call.

Basic Usage

Removing Device From Channel

// for non apns2
var result2 = await pubnub
.removePushChannels('A332C23D', PushGateway.gcm, {'my_channel'});

// for apns2
var result = await pubnub.removePushChannels(
'device-token', PushGateway.apns2, {'my_channel'},
topic: 'MyAppTopic', environment: Environment.development);

Returns

The removePushChannels() doesn't return actionable data. In case of an error, an exception with error details is thrown.

Remove all push notifications

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. Read the support page on enabling add-on features on your keys.

Description

Disable push notifications from all channels registered with the specified pushToken.

Method(s)

To Remove all push notifications you can use the following method(s) in the Dart SDK:

pubnub.removeDevice(
String deviceId,
PushGateway gateway,
{String? topic,
Environment? environment,
Keyset? keyset,
String? using}
)
ParameterTypeRequiredDescription
deviceIdStringYesID of the device to remove push notifications from.
gatewayPushGatewayYesEnums for the back-end to use for push. Available values: apns2 for APNs, gcm for FCM, and mpns for MPNS.
topicStringOptionalNotifications topic name (usually it is bundle identifier of application for Apple platform). Required only if gateway set to apns2.
environmentEnvironmentOptionalEnvironment within which device should manage list of channels with enabled notifications (works only if gateway set to apns2).
keysetKeysetOptionalOverride for the PubNub default keyset configuration.
usingStringOptionalKeyset name from the keysetStore to be used for this method call.

Basic Usage

Remove all push notifications

// for non apns2
var result = await pubnub.removeDevice('deviceId', PushGateway.gcm);

// for apns2
var result = await pubnub.removeDevice('device-token', PushGateway.apns2,
topic: 'MyAppTopic', environment: Environment.development);

Returns

The removeDevice() does not return actionable data. In case of an error, an exception with error details is thrown.

Other Examples

Short Syntax

var device = pubnub.device('A332C23D');

// to register device for channels
var result = await device.registerToChannels({'my_channel'}, PushGateway.gcm);

// to remove device from channels
var result =
await device.deregisterFromChannels({'my_channel'}, PushGateway.gcm);

// to remove all registrations for a device
var result = await device.remove(PushGateway.apns2,
topic: 'MyAppTopic', environment: Environment.production);
Last updated on