This library provides a Go module for Microsoft Azure Notification Hubs.
Originally a fork from Gozure with patches from Martin Etnestad @ vippsas.
Now maintained and packaged by Daresay AB, @daresaydigital.
Basically a wrapper for this Rest API
Using go get
go get github.com/daresaydigital/azure-notificationhubs-go
No external dependencies
package main
import (
"context"
"strings"
"github.com/daresaydigital/azure-notificationhubs-go"
)
func main() {
var (
hub = notificationhubs.NewNotificationHub("YOUR_DefaultFullSharedAccessConnectionString", "YOUR_HubPath")
template = `{
"aps":{
"alert":{
"title":"$(title)",
"body":"$(body)",
},
"badge":"#(badge)",
"topic":"co.daresay.app",
"content-available": 1
},
"name1":"$(value1)",
"name2":"$(value2)"
}`
)
template = strings.ReplaceAll(template, "\n", "")
template = strings.ReplaceAll(template, "\t", "")
reg := notificationhubs.NewTemplateRegistration(
"ABC123", // The token from Apple or Google
nil, // Expiration time, probably endless
"ZXCVQWE", // Registration id, if you want to update an existing registration
"tag1,tag2", // Tags that matches this device
notificationhubs.ApplePlatform, // or GcmPlatform for Android
template, // The template. Use "$(name)" for strings and "#(name)" for numbers
)
// or hub.NewRegistration( ... ) without template
hub.RegisterWithTemplate(context.TODO(), *reg)
// or if no template:
hub.Register(context.TODO(), *reg)
}
package main
import (
"context"
"fmt"
"github.com/daresaydigital/azure-notificationhubs-go"
)
func main() {
var (
hub = notificationhubs.NewNotificationHub("YOUR_DefaultFullSharedAccessConnectionString", "YOUR_HubPath")
payload = []byte(`{"title": "Hello Hub!"}`)
n, _ = notificationhubs.NewNotification(notificationhubs.Template, payload)
)
// Broadcast push
b, _, err := hub.Send(context.TODO(), n, nil)
if err != nil {
panic(err)
}
fmt.Println("Message successfully created:", string(b))
// Tag category push
tags := "tag1 || tag2"
b, _, err = hub.Send(context.TODO(), n, &tags)
if err != nil {
panic(err)
}
fmt.Println("Message successfully created:", string(b))
}
Read more about how to segment notification receivers in the official documentation.
Example devices:
"devices": {
"A": {
"tags": [
"tag1",
"tag2"
]
},
"B": {
"tags": [
"tag2",
"tag3"
]
},
"C": {
"tags": [
"tag1",
"tag2",
"tag3"
]
},
}
Send to devices that has tag1
or tag2
. Example devices A, B and C.
hub.Send(notification, "tag1 || tag2")
Send to devices that has tag1
and tag2
. Device A and C.
hub.Send(notification, "tag1 && tag2")
Send to devices that has tag1
and tag2
but not tag3
. Device A.
hub.Send(notification, "tag1 && tag2 && !tag3")
Send to devices that has not tag1
. Device B.
hub.Send(notification, "!tag1")
First release by Daresay. Restructured the code and renamed the API according to Go standards.
Implement cancel scheduled notifications using http DELETE. Find inspo from the Java SDK here.
Only Android and iOS is supported today, implement the other supported platforms. Probably limited usecase.
See the LICENSE file for license rights and limitations (MIT).
Version | Tag | Published |
---|---|---|
v0.1.5-0.20200217102931-10149d3de92e | 2yrs ago | |
v0.1.4 | 2yrs ago | |
v0.1.3 | 2yrs ago | |
v0.1.1 | 3yrs ago |