Utility Methods API for PubNub Unity SDK

The methods on this page are utility methods that don't fit into other categories.

Cleanup

Cleanup frees up the threads and allows for clean exit.

Method(s)

Pubnub.CleanUp()

Basic Usage

Pubnub.CleanUp();

Returns

None

Encrypt

This function allows to encrypt the data.

Deprecated

The cipherKey parameter in this method is deprecated. We recommend that you configure a seprate instance of the crypto module and use it for partial encryption.

If you pass cipherKey as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.

Method(s)

To encrypt the data you can use the following method(s) in Unity SDK.

pubnub.Encrypt(inputString, cipherKey)
ParameterTypeRequiredDescription
inputStringStringYesThe data to encrypt.
cipherKeyStringOptionalCipher key to use for encryption.

Basic Usage

Encrypt part of message

string stringToEncrypt = "hello world";

var crypto = PubnubApi.Security.Crypto.CryptoModule.CreateAesCbcCryptor("test");

crypto.Encrypt(stringToEncrypt);

Encrypt File

This function allow to encrypt the file content/data.

Deprecated

The cipherKey parameter in this method is deprecated. We recommend that you configure a seprate instance of the crypto module and use it for partial encryption.

If you pass cipherKey as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.

Method(s)

To encrypt the file you can use the following method(s) in Unity SDK.

pubnub.EncryptFile(sourceFile, destinationFile, cipherKey)
ParameterTypeRequiredDescription
sourceFileStringYesFile to be encrypted.
destinationFileStringYesPath of the encrypted file to be saved.
cipherKeyStringOptionalCipher Key to use for encryption. If provided, the legacy encryption with 128-bit cipher key entropy is used. If not provided, the CryptoModule from PubNub config will be used.

For more information, refer to Crypto module configuration.
byte[] outputBytes = pubnub.EncryptFile(sourceBytes) byte[] outputBytes = pubnub.EncryptFile(sourceBytes, cipherKey)
ParameterTypeRequiredDescription
sourceBytesbyte[]Yesbyte array of the file.
cipherKeyStringOptionalCipher Key to use for encryption. If provided, the legacy encryption with 128-bit cipher key entropy is used. If not provided, the CryptoModule from PubNub config will be used.

For more information, refer to Crypto module configuration.

Basic Usage

string source_file = "cat_picture.jpg"; // checks bin folder if no path is provided
string destination_file = "destination_cat_pic.jpg"; // checks bin folder if no path is provided
var crypto = PubnubApi.Security.Crypto.CryptoModule.CreateAesCbcCryptor("test");

crypto.EncryptFile(source_file, destination_file);
byte[] sourceBytes = System.IO.File.ReadAllBytes("cat_picture.jpg"); // checks bin folder if no path is provided
var crypto = PubnubApi.Security.Crypto.CryptoModule.CreateAesCbcCryptor("test");

byte[] outputBytes = crypto.EncryptFile(sourceBytes);
System.IO.File.WriteAllBytes("destination_cat_pic.jpg", outputBytes); // checks bin folder if no path is provided

Decrypt

This function allows to decrypt the data.

Deprecated

The cipherKey parameter in this method is deprecated. We recommend that you configure a seprate instance of the crypto module and use it for partial encryption.

If you pass cipherKey as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.

Method(s)

To decrypt the data you can use the following method(s) in Unity SDK.

pubnub.Decrypt(inputString, cipherKey)
ParameterTypeRequiredDescription
inputStringStringYesThe data to decrypt.
cipherKeyStringOptionalCipher key used for decryption.

Basic Usage

Decrypt part of message

string encryptedString = "9qR0Q4TuDUwiLTcxtIY3mA==";
string cipherKey = "testCipher";

string decryptedMessage = pubnub.Decrypt(encryptedString, cipherKey);

Decrypt File

This function allow to decrypt the file content/data.

Deprecated

The cipherKey parameter in this method is deprecated. We recommend that you configure a seprate instance of the crypto module and use it for partial encryption.

If you pass cipherKey as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.

Method(s)

To decrypt the file you can use the following method(s) in Unity SDK.

pubnub.DecryptFile(sourceFile, destinationFile, cipherKey);
ParameterTypeRequiredDescription
sourceFileStringYesFile to be decrypted.
destinationFileStringYesPath of the decrypted file to be saved.
cipherKeyStringOptionalCipher Key to use for decryption. If provided, the legacy encryption with 128-bit cipher key entropy is used. If not provided, the CryptoModule from PubNub config will be used.

For more information, refer to Crypto module configuration.
byte[] outputBytes = pubnub.DecryptFile(sourceBytes) byte[] outputBytes = pubnub.DecryptFile(sourceBytes, cipherKey)
ParameterTypeRequiredDescription
sourceBytesbyte[]Yesbyte array of the file.
cipherKeyStringOptionalCipher Key to use for decryption. If provided, the legacy encryption with 128-bit cipher key entropy is used. If not provided, the CryptoModule from PubNub config will be used.

For more information, refer to Crypto module configuration.

Basic Usage

string source_file = "encrypted_cat_pic.jpg"; // checks bin folder if no path is provided
string destination_file = "cat_pic_original.jpg"; // checks bin folder if no path is provided
var crypto = PubnubApi.Security.Crypto.CryptoModule.CreateAesCbcCryptor("test");

crypto.DecryptFile(source_file, destination_file);
byte[] sourceBytes = System.IO.File.ReadAllBytes("encrypted_cat_pic.jpg"); // checks bin folder if no path is provided
var crypto = PubnubApi.Security.Crypto.CryptoModule.CreateAesCbcCryptor("test");

byte[] outputBytes = crypto.DecryptFile(sourceBytes);
System.IO.File.WriteAllBytes("cat_pic_original.jpg", outputBytes); // checks bin folder if no path is provided

Disconnect

Call the Disconnect method to force the SDK to stop all requests to PubNub server when there are active subscribe channels.

Method(s)

To disconnect the data transmission you can use the following method(s) in Unity SDK.

Disconnect<T>()

This method doesn't take any arguments.

Basic Usage

pubnub.Disconnect<string>();

Get Subscribed Channel Groups

Returns all the subscribed channel groups in a List of type String.

Method(s)

To Get Subscribe Channel Groups you can use the following method(s) in the Unity SDK:

List<string> GetSubscribedChannelGroups()

Basic Usage

Get Subscribed Channel Groups

List<string> groups = pubnub.GetSubscribedChannelGroups();

Response

List<String>

["channelGroup1", "channelGroup2"]

Get Subscribed Channels

Returns all the subscribed channels in a List of type String.

Method(s)

To Get Subscribed Channels you can use the following method(s) in the Unity SDK:

List<string> GetSubscribedChannels()

Basic Usage

Get Subscribed Channels

List<string> channels = pubnub.GetSubscribedChannels();

Response

List<String>

["channel1", "channel2"]

Reconnect

Call the reconnect method to force the SDK to try and reach out PubNub.

Method(s)

To reconnect the data you can use the following method(s) in Unity SDK.

Reconnect<T>(bool resetSubscribeToken)
ParameterTypeRequiredDescription
resetSubscribeTokenboolOptionalPassing true will send zero timetoken upon reconnect.

Basic Usage

pubnub.Reconnect<string>();

Time

This function will return a 17 digit precision Unix epoch.

Algorithm constructing the timetoken
timetoken = (Unix epoch time in seconds) * 10000000

Example of creating a timetoken for a specific time and date:

08/19/2013 @ 9:20pm in UTC = 1376961606
timetoken = 1376961606 * 10000000
timetoken = 13769616060000000

Method(s)

To fetch Time you can use the following method(s) in Unity SDK:

pubnub.QueryParam(Dictionary<string,object>).Time()
ParameterTypeRequiredDescription
QueryParamDictionary<string, object>OptionalDictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose.
AsyncPNCallbackDeprecatedPNCallback of type PNTimeResult.
ExecuteSystem.ActionYesSystem.Action of type PNTimeResult.
ExecuteAsyncNoneOptional

Basic Usage

Get PubNub Timetoken

pubnub.Time()
.Execute(new PNTimeResultExt(
(result, status) => {
// handle time result.
}
));

Returns

The Time operation returns a PNTimeResult which contains the following property:

Property NameTypeDescription
TimetokenlongReturns a long representation of current timetoken.

Create Push Payload

This method creates the push payload for use in the appropriate endpoint calls.

Method(s)

CreatePushPayloadHelper()
.SetAPNSPayload(PNAPSData, List<PNAPNS2Data>)
.SetFCMPayload(PNFCMData)
.SetCommonPayload(Dictionary<string, object>)
.BuildPayload()
ParameterTypeRequiredDescription
SetAPNSPayloadPNAPSDataOptionalSet APNS Payload. Associated APNS devices will receive only the data within the pn_apns key.
List<PNAPNS2Data>OptionalSet APNS2 Payload. Associated APNS devices will receive only the data within the pn_push key.
SetFCMPayloadPNFCMDataOptionalSet FCM Payload. Associated FCM devices will receive only the data within the pn_gcm key.
SetCommonPayloadDictionary<string, object>OptionalSet Common Payload. Native PubNub subscribers will receive the entire object literal, including the pn_apns, pn_gcm, and common payload.
BuildPayloadYesBuilds the payload from the values set using the parameters. Returns a Dictionary<string, object>

Basic Usage

Create Push Payload

CreatePushPayloadHelper cpph = new CreatePushPayloadHelper();
PNAPSData aps = new PNAPSData();
aps.Alert = "alert";
aps.Badge = 1;
aps.Sound = "ding";
aps.Custom = new Dictionary<string, object>(){
{"aps_key1", "aps_value1"},
{"aps_key2", "aps_value2"},
};

PNAPNSData apns = new PNAPNSData();
apns.APS = aps;
apns.Custom = new Dictionary<string, object>(){
{"apns_key1", "apns_value1"},
{"apns_key2", "apns_value2"},
show all 84 lines

Response

The CreatePushPayloadHelper() operation returns a Dictionary<string, object> which can be passed directly to the Publish Method's Message parameter.

Last updated on