UnityUnity V4 Misc API Reference for Realtime Apps


These docs are for PubNub 4.x for Unity which is our latest and greatest! For the docs of the older versions of the SDK, please check PubNub 3.x for Unity.

If you have questions about the PubNub for Unity SDK, please contact us at support@pubnub.com.

The methods on this page are utility methods that don't fit into other categories.
Destroy frees up the threads and allows for clean exit.
  1. CleanUp()
Call the reconnect method to force the SDK to try and reach out PubNub.
To reconnect the data you can use the following method(s) in Unity V4 SDK.
  1. Reconnect()
This function will return a 17 digit precision Unix epoch.
The timetoken is constructed using the following algorithm:
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
To fetch Time you can use the following method(s) in Unity V4 SDK
  1. ParameterTypeRequiredDescription
    AsyncPNCallbackYesPNCallback of type PNTimeResult
	.Async((result, status) => {              
		if (status.Error) {
			Debug.Log(string.Format("Time Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category));
		} else {
			Debug.Log(string.Format("DateTime {0}, In Example, result: {1}", DateTime.UtcNow ,result.TimeToken));
The Time() operation returns a PNTimeResult which contains the following operations:
TimetokenlongReturns a long representation of current time token.

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

  1. ParameterTypeRequiredDescription
    SetAPNSPayloadPNAPSDataOptionalSet APNS Payload. Associated APNS devices will receive only the data within the pn_apns key.
    SetAPNSPayloadList<PNAPNS2Data>OptionalSet APNS2 Payload. Associated APNS devices will receive only the data within the pn_push key.
    SetMPNSPayloadPNMPNSDataOptionalSet MPNS Payload. Associated MPNS devices will receive only the data within the pn_mpns key.
    SetFCMPayloadPNFCMDataOptionalSet FCM Payload. Associated GCM/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, pn_mpns, and common payload.
    BuildPayloadYesBuilds the payload from the values set using the parameters. Returns a Dictionary<string, object>

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"},

PNAPNS2Data apns2One = new PNAPNS2Data();
apns2One.CollapseID = "invitations";
apns2One.Expiration = "2019-12-13T22:06:09Z";
apns2One.Version = "v1";
apns2One.Targets = new List<PNPushTarget>(){
	new PNPushTarget(){
		Environment = PNPushEnvironment.Development,
		Topic = "com.meetings.chat.app",
		ExcludeDevices = new List<string>(){

PNAPNS2Data apns2Two = new PNAPNS2Data();
apns2Two.CollapseID = "invitations";
apns2Two.Expiration = "2019-12-15T22:06:09Z";
apns2Two.Version = "v2";
apns2Two.Targets = new List<PNPushTarget>(){
	new PNPushTarget(){
		Environment = PNPushEnvironment.Production,
		Topic = "com.meetings.chat.app",
		ExcludeDevices = new List<string>(){

List<PNAPNS2Data> apns2 = new List<PNAPNS2Data>(){

PNMPNSData mpns = new PNMPNSData();
mpns.Custom = new Dictionary<string, object>(){
	{"mpns_key1", "mpns_value1"},
	{"mpns_key2", "mpns_value2"},
mpns.Title = "title";
mpns.Type = "type";
mpns.Count = 1;
mpns.BackTitle = "BackTitle";
mpns.BackContent = "BackContent";

PNFCMData fcm = new PNFCMData();
fcm.Custom = new Dictionary<string, object>(){
	{"fcm_key1", "fcm_value1"},
	{"fcm_key2", "fcm_value2"},
fcm.Data = new PNFCMDataFields(){
	Summary = "summary",
	Custom = new Dictionary<string, object>(){
		{"fcm_data_key1", "fcm_data_value1"},
		{"fcm_data_key2", "fcm_data_value2"},

Dictionary<string, object> commonPayload = new Dictionary<string, object>();
commonPayload = new Dictionary<string, object>(){
		{"common_key1", "common_value1"},
		{"common_key2", "common_value2"},

Dictionary<string, object> payload = cpph.SetAPNSPayload(apns, apns2).SetMPNSPayload(mpns).SetFCMPayload(fcm).SetCommonPayload(commonPayload).BuildPayload();
    .Async((result, status) => {
        if (!status.Error) {
            Debug.Log(string.Format("DateTime {0}, In Publish Example, Timetoken: {1}", DateTime.UtcNow , result.Timetoken));
        } else {
The CreatePushPayloadHelper() operation returns a Dictionary<string, object> which can be passed directly to the Publish Method's Message parameter.