Go Server SDK

Installation

You can install this SDK by doing:

bash

1
$ go get github.com/pusher/push-notifications-go

Reference

.New

Constructs a new Beams client instance using your instance id and secret key (you can get these from the dashboard)

Arguments

  • instanceId(string): The unique identifier for your Push notifications instance. This can be found in the dashboard under "Credentials".
  • secretKey(string): The secret key your server will use to access your Beams instance. This can be found in the dashboard under "Credentials".
  • options(Option | vararg): Variadic list of options for configuring the SDK

Returns

  • client(PushNotifications): Beams API client implementating the PushNotifications interface.
  • err(error): Error value if something went wrong, nil otherwise.

Example

Go

1
2
3
4
5
6
7
8
9
const (
  instanceId = "YOUR_INSTANCE_ID_HERE"
  secretKey  = "YOUR_SECRET_KEY_HERE"
)

beamsClient, err := pushnotifications.New(instanceId, secretKey)
if err != nil {
  fmt.Println("Could not create Beams Client:", err.Error())
}

.PublishToInterests

Sends broadcast notifications to groups of subscribed devices using Device Interests

Arguments

  • interests([]string | Min length=1, Max length=100): List of interests to send the push notification to, ranging from 1 to 100 per publish request. See Device Interests
  • publishBody(map[string]interface{}): A map containing the publish request body. See publish API reference

Returns

  • publishID(string): Unique identifier for the publish request.
  • err(error): Error value if something went wrong, nil otherwise.

Example

Go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
publishRequest := map[string]interface{}{
  "apns": map[string]interface{}{
    "aps": map[string]interface{}{
      "alert": map[string]interface{}{
        "title": "Hello",
        "body":  "Hello, world",
      },
    },
  },
  "fcm": map[string]interface{}{
    "notification": map[string]interface{}{
      "title": "Hello",
      "body":  "Hello, world",
    },
  },
}

pubId, err := beamsClient.PublishToInterests([]string{"hello"}, publishRequest)
if err != nil {
  fmt.Println(err)
} else {
  fmt.Println("Publish Id:", pubId)
}

.PublishToUsers

Securely send notifications to individual users of your application using Authenticated Users

Arguments

  • userIds([]string | Min length=1, Max length=1000): List of ids of users to send the push notification to, ranging from 1 to 1000 per publish request. See Authenticated Users
  • publishBody(map[string]interface{}): A map containing the publish request body. See publish API reference

Returns

  • publishID(string): Unique identifier for the publish request.
  • err(error): Error value if something went wrong, nil otherwise.

Example

Go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
publishRequest := map[string]interface{}{
  "apns": map[string]interface{}{
    "aps": map[string]interface{}{
      "alert": map[string]interface{}{
        "title": "Hello",
        "body":  "Hello, world",
      },
    },
  },
  "fcm": map[string]interface{}{
    "notification": map[string]interface{}{
      "title": "Hello",
      "body":  "Hello, world",
    },
  },
}

pubId, err := beamsClient.PublishToUsers([]string{"user-001", "user-002"}, publishRequest)
if err != nil {
  fmt.Println(err)
} else {
  fmt.Println("Publish Id:", pubId)
}

.GenerateToken

Generate a Beams auth token to allow a user to associate their device with their user id. The token is valid for 24 hours.

Arguments

  • userID(string): Id of the user you would like to generate a Beams auth token for.

Returns

  • beamsToken(string): Beams Token for the given user
  • err(error): Error value if something went wrong, nil otherwise.

Example

Go

1
2
3
4
5
userID := "user-001"
beamsToken, err := beamsClient.GenerateToken(userID)
if err != nil {
  fmt.Println("Could not generate Beams token:", err.Error())
}

.DeleteUser

Remove the given user (and all of their devices) from Beams. This user will no longer receive any notifications and all state stored about their devices will be deleted.

Arguments

  • userID(string): Id of the user you would like to delete from Beams.

Returns

  • err(error): Error value if something went wrong, nil otherwise.

Example

Go

1
2
3
4
5
userID := "user-001"
err := beamsClient.DeleteUser(userID)
if err != nil {
  fmt.Println("Could not delete user:", err.Error())
}