The Notification plugin lets your app post and cancel system notifications on Android.
Setup
The notification plugin is already registered in the default AppTemplate.
Android Manifest
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
API
notification.NewPlugin() *NotificationPlugin
Creates a new notification plugin instance.
(*NotificationPlugin).Post(n Notification) (string, error)
Posts a system notification.
Returns: A result string from the native layer, typically {"status":"posted"} or similar.
(*NotificationPlugin).Cancel(id int) string
Cancels a previously posted notification by its ID.
Types
Notification
type Notification struct {
ID int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
ChannelID string `json:"channel_id"`
ChannelName string `json:"channel_name"`
Importance Importance `json:"importance"`
}
| Field | Description |
|---|---|
ID |
Notification ID. If 0, a unique ID is generated automatically |
Title |
Notification title text |
Body |
Notification body text |
ChannelID |
Android notification channel ID. Defaults to default_channel |
ChannelName |
Human-readable channel name. Defaults to General Notifications |
Importance |
Notification priority level |
Importance
type Importance string
const (
ImportanceDefault Importance = "DEFAULT"
ImportanceHigh Importance = "HIGH"
ImportanceLow Importance = "LOW"
ImportanceMin Importance = "MIN"
)
Example
import "github.com/sweet-juice/sweetjuice/plugins/notification"
plugin := notification.NewPlugin()
_, err := plugin.Post(notification.Notification{
Title: "Sweet Juice",
Body: "Background sync running...",
ChannelID: "default_channel",
ChannelName: "General Notifications",
Importance: notification.ImportanceDefault,
})
// Cancel a notification by ID
plugin.Cancel(12345)
Notes
- On Android 13+ (
API 33), the user must grantPOST_NOTIFICATIONSruntime permission before notifications can be posted. - The notification plugin uses a default channel if none is specified. Custom channels should be created natively if you need different behavior.