GoGo V4 Misc API Reference for Realtime Apps

 

These docs are for PubNub 4.0 for Go which is our latest and greatest! For the docs of the older versions of the SDK, please check PubNub 3.0 for Go.

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

This function will return a 17 digit precision Unix epoch.
 
Timetoken
The timetoken is constructed using the following algorithm:
timetoken = (Unix epoch time in seconds) * 10000000
Example of creating a timetoken for a specific time & 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 Go V4 SDK
  1. ParameterTypeRequiredDescription
    QueryParammap[string]stringOptionalQueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API.
res, status, err := pn.Time().Execute()

fmt.Println(res, status, err)
MethodTypeDescription
Timetokenint64Returns a date representation of current time token.
This method creates the push payload for use in the appropriate endpoint calls.
  • ParameterTypeRequiredDescription
    SetAPNSPayloadpubnub.PNAPNSDataOptionalSet APNS Payload. Associated APNS devices will receive only the data within the pn_apns key.
    []pubnub.PNAPNS2DataOptionalSet APNS2 Payload. Associated APNS devices will receive only the data within the pn_push key.
    SetMPNSPayloadpubnub.PNMPNSDataOptionalSet MPNS Payload. Associated MPNS devices will receive only the data within the pn_mpns key.
    SetFCMPayloadpubnub.PNFCMDataOptionalSet FCM Payload. Associated GCM/FCM devices will receive only the data within the pn_gcm key.
    SetCommonPayloadmap[string]interface{}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 map[string]interface{}

    Create Push Payload

    aps := pubnub.PNAPSData{
    	Alert: "apns alert",
    	Badge: 1,
    	Sound: "ding",
    	Custom: map[string]interface{}{
    		"aps_key1": "aps_value1",
    		"aps_key2": "aps_value2",
    	},
    }
    
    apns := pubnub.PNAPNSData{
    	APS: aps,
    	Custom: map[string]interface{}{
    		"apns_key1": "apns_value1",
    		"apns_key2": "apns_value2",
    	},
    }
    
    apns2One := pubnub.PNAPNS2Data{
    	CollapseID: "invitations",
    	Expiration: "2019-12-13T22:06:09Z",
    	Version:    "v1",
    	Targets: []pubnub.PNPushTarget{
    		pubnub.PNPushTarget{
    			Environment: pubnub.PNPushEnvironmentDevelopment,
    			Topic:       "com.meetings.chat.app",
    			ExcludeDevices: []string{
    				"device1",
    				"device2",
    			},
    		},
    	},
    }
    
    apns2Two := pubnub.PNAPNS2Data{
    	CollapseID: "invitations",
    	Expiration: "2019-12-15T22:06:09Z",
    	Version:    "v2",
    	Targets: []pubnub.PNPushTarget{
    		pubnub.PNPushTarget{
    			Environment: pubnub.PNPushEnvironmentProduction,
    			Topic:       "com.meetings.chat.app",
    			ExcludeDevices: []string{
    				"device3",
    				"device4",
    			},
    		},
    	},
    }
    
    apns2 := []pubnub.PNAPNS2Data{apns2One, apns2Two}
    
    mpns := pubnub.PNMPNSData{
    	Title:       "title",
    	Type:        "type",
    	Count:       1,
    	BackTitle:   "BackTitle",
    	BackContent: "BackContent",
    	Custom: map[string]interface{}{
    		"mpns_key1": "mpns_value1",
    		"mpns_key2": "mpns_value2",
    	},
    }
    
    fcm := pubnub.PNFCMData{
    	Data: pubnub.PNFCMDataFields{
    		Summary: "summary",
    		Custom: map[string]interface{}{
    			"fcm_data_key1": "fcm_data_value1",
    			"fcm_data_key2": "fcm_data_value2",
    		},
    	},
    	Custom: map[string]interface{}{
    		"fcm_key1": "fcm_value1",
    		"fcm_key2": "fcm_value2",
    	},
    }
    
    CommonPayload := map[string]interface{}{
    	"a": map[string]interface{}{
    		"common_key1": "common_value1",
    		"common_key2": "common_value2",
    	},
    	"b":        "val",
    }
    
    s := pn.CreatePushPayload().SetAPNSPayload(apns, apns2).SetCommonPayload(CommonPayload).SetFCMPayload(fcm).SetMPNSPayload(mpns).BuildPayload()
    
    res, status, err := pn.Publish().
        Channel("my-channel").
        Message(s).
        Execute()
    
    fmt.Println(res, status, err)
    The CreatePushPayload() operation returns a map[string]interface{} which can be passed directly to the Publish Method's Message parameter.