Go for Azure Push Notifications

Gozure is a Go(lang) backend client for Microsoft Azure Notification Hub. It is a simple REST API wrapper that we internally use in our Go projects.

Case study

Imagine you have a football match (Brazil 1:7 Germany) and want make all German fans happy whenever their team scores a goal.

Assumptions:
match id: 123
user language: de
event type: goal

Step one

On start the app scans user settings, finds out that our user is a German fan and subscribes to the predefined push notification category: m123_goal_de. At this moment the app says to Azure - I want to get push notifications for all goals in the match with id 123 and the text should be localized in german language.

Step two

App backend processes goal events from match 123 and posts them to the Notification Hub. Azure will take care that respective users get their notifications.

This is how it looks like from the Go side:

import (
    "encoding/json"
    "fmt"

    "github.com/onefootball/gozure/notihub"
)
...
type NotificationMessage struct {
    Title     string `json:"title"`
    HomeScore int    `json:"home_score"` 
    AwayScore int    `json:"away_score"`
}

// create hub
hub, err := notihub.NewNotification("defaultFullSharedAccessSignature", "hubPath")
if err != nil {
    panic(err)
}

m := &NotificationMessage{"TOR!", 7, 1}
b, err := json.Marshal(m)
if err != nil {
    panic(err)
}

// use Template message
n, err := notihub.NewNotification(notihub.Template, b)
if err != nil {
    panic(err)
}

// send notification to the respective category
r, err := hub.Send(n, []string{"m123_goal_de"})
if err != nil {
    panic(err)
}

fmt.Println("message created:", string(r))

We hope that our contribution would help certain Gophers to send their push notifications.

Written on July 23, 2015 by Siamion Makarski