Documentation
¶
Index ¶
- Constants
- Variables
- func GenerateSASToken(resourceUri, keyName, key string, duration time.Duration) (string, error)
- func ValidateSASToken(ctx context.Context, httpClient *http.Client, namespace, hubName, token string) error
- type Client
- func (c *Client) DeleteDevice(ctx context.Context, installationID string) error
- func (c *Client) DeviceExists(ctx context.Context, installationID string) (bool, error)
- func (c *Client) RegisterDevice(ctx context.Context, installation Installation) (string, error)
- func (c *Client) SendNotification(ctx context.Context, notification Notification, tags ...string) error
- func (c *Client) ValidateToken(ctx context.Context) error
- type Configuration
- type Installation
- type Notification
- type Template
- type TokenManager
Constants ¶
const ( // InstallationApple is the platform type for Apple devices (iOS). InstallationApple = "apns" // InstallationGCM is the platform type for Google Cloud Messaging (deprecated). InstallationAndroid = "gcm" // InstallationGCM is the platform type for Google Cloud Messaging (deprecated). InstallationFCM = "fcm" // InstallationFCMV1 is the platform type for Firebase Cloud Messaging (V1). InstallationFCMV1 = "FCMV1" // InstallationBaidu is the platform type for Baidu Push. InstallationBaidu = "baidu" // InstallationWNS is the platform type for Windows Notification Service. InstallationWNS = "wns" // InstallationMPNS is the platform type for Microsoft Push Notification Service. InstallationMPNS = "mpns" )
Installation types for Azure Notification Hubs.
Variables ¶
var DefaultTokenValidity = time.Hour * 24 * 7
1 week.
Functions ¶
func GenerateSASToken ¶
GenerateSASToken creates a Shared Access Signature (SAS) token for Azure Notification Hub.
Ported from: https://learn.microsoft.com/en-us/rest/api/eventhub/generate-sas-token#nodejs.
Types ¶
type Client ¶
type Client struct { Config Configuration TokenManager *TokenManager // HTTPClient is the client used for HTTP requests. // It can be overridden for testing. HTTPClient *http.Client }
Client provides a high-level interface for interacting with Azure Notification Hubs. It encapsulates configuration and token management, and offers methods for device registration, existence checking, and push notification sending without requiring the caller to manually handle SAS tokens or repeated hub information.
Example usage:
client := azurepush.NewClient(cfg) id, err := client.RegisterDevice(context.Background(), installation) err = client.SendNotification(context.Background(), azurepush.Notification{...}, "user:123")
func NewClient ¶
func NewClient(cfg Configuration) *Client
NewClient creates and validates a new push notification client. NewClient creates a new Azure Notification Hub client with the given configuration. It automatically initializes a TokenManager for SAS token generation and reuse.
This method also validates and tests connectivity to the Azure Notification Hub.
Example:
client := azurepush.NewClient(azureCfg) err := client.SendNotification(context.Background(), notification, "user:42")
func (*Client) DeleteDevice ¶
DeleteDevice deletes a registered device installation from Azure Notification Hubs using its installation ID.
This operation is idempotent — if the installation does not exist, Azure will return 404, and this function will still return nil.
Example:
err := client.DeleteDevice(context.Background(), "device-uuid-123")
func (*Client) DeviceExists ¶
DeviceExists checks if a device installation with the given ID exists in Azure Notification Hub. Returns true if the device is found (HTTP 200), false if not found (HTTP 404).
func (*Client) RegisterDevice ¶
RegisterDevice registers a device installation with Azure Notification Hubs. Read more at: https://learn.microsoft.com/en-us/answers/questions/1324518/sending-notification-registering-device-in-notific.
You use the tags you assign during registration to send notifications, as this is how you target specific devices. For example, if you register a device with the tag "user:123", you can send a notification to that device by targeting the "user:123" tag.
func (*Client) SendNotification ¶
func (c *Client) SendNotification(ctx context.Context, notification Notification, tags ...string) error
SendNotification sends a cross-platform push notification to all devices for a given user (e.g. tag with "user:42").
func (*Client) ValidateToken ¶
ValidateToken performs a simple GET request to a dummy installation ID to verify if the SAS token is valid and authorized. Returns nil if authorized (even if installation doesn't exist), or an error if unauthorized.
type Configuration ¶
type Configuration struct { // HubName is the name of your Azure Notification Hub instance. // You can find this in the Azure Portal under your Notification Hub resource. // Example: "myhubname" HubName string `yaml:"HubName"` // ConnectionString is the full connection string for the Azure Notification Hub. // This is used to extract the Namespace, KeyName, and KeyValue. // Example: "Endpoint=sb://<namespace>.servicebus.windows.net/;SharedAccessKeyName=<name>;SharedAccess // Key=<key>" // ConnectionString is the full connection string for the Azure Notification Hub. // // If this field is present, the individual fields (Namespace, KeyName, KeyValue) are ignored. ConnectionString string `yaml:"ConnectionString"` // Namespace is the Azure Service Bus namespace where the Notification Hub lives. // This should be the prefix (without .servicebus.windows.net). // Example: "my-namespace" Namespace string `yaml:"Namespace"` // KeyName is the name of the Shared Access Policy with send or full access. // You can find this under "Access Policies" in your Notification Hub (left menu > Settings > Access Policies). // Example: "DefaultFullSharedAccessSignature" KeyName string `yaml:"KeyName"` // KeyValue is the primary or secondary key associated with the KeyName. // To get this, go to the Notification Hub > Access Policies > select your policy > copy "Primary Key". // You may also use the connection string to parse out these values, but we use manual fields here. // Example: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX==" // KeyValue is the value of the SharedAccessKey. // In the Azure Portal, go to Notification Hub → Access Policies → select a policy (e.g. DefaultFullSharedAccessSignature) // → copy the **Connection String**. From it, extract: // - KeyName from `SharedAccessKeyName` (e.g. DefaultFullSharedAccessSignature) // - KeyValue from `SharedAccessKey` // Example Connection String: // Endpoint=sb://<namespace>.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=YOUR_SECRET_KEY // Use the value of `SharedAccessKey` as KeyValue. KeyValue string `yaml:"KeyValue"` // TokenValidity is how long each generated SAS token should remain valid. // It must be a valid Go duration string (e.g., "1h", "30m"). // Example: 2 * time.Hour // // Defaults to 1 week. TokenValidity time.Duration `yaml:"TokenValidity"` // ConnectivityCheck enables the connectivity check. // If enabled, the NewClient will check the connection to the Azure Notification Hub before sending messages. // // Defaults to false. ConnectivityCheck bool `yaml:"ConnectivityCheck"` }
Configuration holds Azure Notification Hub credentials and settings.
The Validate method MUST be called after loading the configuration to ensure all required fields are present.
func LoadConfiguration ¶
func LoadConfiguration(path string) (*Configuration, error)
LoadConfiguration loads a YAML config from the given path.
func (*Configuration) Validate ¶
func (cfg *Configuration) Validate() error
Validate checks the AzureConfig for required fields. It also parses the connection string if available. If the connection string is present, it will override the individual fields.
type Installation ¶
type Installation struct { // InstallationID is a unique identifier for the installation (usually a device ID or UUID). // This is used to update or delete installations. InstallationID string `json:"installationId"` // Platform is the platform type for the device. // Valid values: "apns" for iOS and "gcm", "FCMV1" for Android. // Platform | Value // Apple | "apns" // FCM | "gcm" or "fcm" and "FCMV1" (see https://learn.microsoft.com/en-us/azure/notification-hubs/firebase-migration-rest) // Baidu | "baidu" // WNS | "wns" // MPNS | "mpns" Platform string `json:"platform"` // PushChannel is the device-specific token to receive notifications. // For APNs: the device token from Apple. // For FCM: the registration token from Firebase. // Ref: https://learn.microsoft.com/en-us/rest/api/notificationhubs/installation#pushchannel PushChannel string `json:"pushChannel"` // Tags is an optional list of tags to categorize this device. // These are used for targeting groups of installations (e.g., "user:123"). Tags []string `json:"tags,omitempty"` // Templates defines push notification templates for the device. // This is optional and only needed for advanced templated notifications. Templates map[string]Template `json:"templates,omitempty"` }
Installation represents a device installation for Azure Notification Hubs.
func (Installation) Validate ¶
func (i Installation) Validate() error
Validate checks if the installation has all required fields set.
type Notification ¶
Notification holds the title, body and custom data for a notification sent to both iOS and Android.
type TokenManager ¶
type TokenManager struct {
// contains filtered or unexported fields
}
TokenManager manages the lifecycle of SAS tokens.
func NewTokenManager ¶
func NewTokenManager(cfg Configuration) *TokenManager
NewTokenManager creates a new TokenManager.
func (*TokenManager) GetToken ¶
func (tm *TokenManager) GetToken() (string, error)
GetToken returns a valid SAS token, refreshing it if necessary.