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
ortag2
. Example devices A, B and C.hub.Send(notification, "tag1 || tag2")
-
Send to devices that has
tag1
andtag2
. Device A and C.hub.Send(notification, "tag1 && tag2")
-
Send to devices that has
tag1
andtag2
but nottag3
. Device A.hub.Send(notification, "tag1 && tag2 && !tag3")
-
Send to devices that has not
tag1
. Device B.hub.Send(notification, "!tag1")
- Fix for background notifications on iOS 13
- Pass the current context to the http request instead of using the background context, thanks to NathanBaulch
- Add support for installations, thanks to NathanBaulch
- Add support for batch send, thanks to NathanBaulch
- Add support for unregistering a device, thanks to NathanBaulch
- Add automatic testing for Go 1.13
- Two minor bug fixes
- Bugfix for reading the message id on standard hubs. Headers are always lowercase.
- Bugfix for when device registration were responding an unexpected response.
- Support for templated notifications
- Support for notification telemetry in higher tiers
- Big rewrite
- Added get registrations
- Travis CI
- Renamed the entities to use the same nomenclature as Azure
- Using fixtures for tests
- Support tag expressions
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).