Skip to content

push ​

go
import "github.com/tangoui/tango/sys/push"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

Package push registers the app for remote (push) notifications and reports notifications that arrive or that the user taps.

push.OnOpen(a, func(m push.Message) { route(m.Data) })   // before Run or in OnReady
token, err := push.Register(a)                          // send token to your server

iOS: APNs. Register asks for alert/sound/badge permission if the user was never asked, then returns the device token as lowercase hex. The app needs the aps-environment entitlement (a provisioning profile with Push Notifications); without it Register fails with the system's error. The simulator issues tokens too (iOS 16+ on Apple silicon) and accepts simulated pushes: xcrun simctl push booted <bundle-id> payload.apns.

Android: Firebase Cloud Messaging needs Google services, which Tango does not bundle. Register returns ErrUnsupported until the app wires FCM in (add the Firebase dependency and a FirebaseMessagingService that calls org.tango.host.TangoPush.token / .received / .opened — see docs/mobile-permissions.md); after that it returns the FCM token and the events below work.

Notifications arriving while the app runs in the foreground are shown as banners on iOS and reported with OnReceive. Taps — also the one that launched the app — are reported with OnOpen; register handlers before Run or in OnReady so the launch tap is not missed. Handlers run on the main thread.

变量 ​

go
var (
	ErrUnsupported      = platform.ErrUnsupported
	ErrPermissionDenied = platform.ErrPermissionDenied
)

函数 ​

func OnOpen ​

go
func OnOpen(a *tango.App, fn func(Message))

OnOpen calls fn when the user taps a notification.

func OnReceive ​

go
func OnReceive(a *tango.App, fn func(Message))

OnReceive calls fn for notifications that arrive while the app runs.

func OnToken ​

go
func OnToken(a *tango.App, fn func(token string))

OnToken calls fn with the token whenever the platform issues a new one (after Register, and when FCM rotates it).

func Register ​

go
func Register(a *tango.App) (string, error)

Register registers with the push service and returns the token.

类型 ​

Message ​

go
type Message struct {
	ID     string // the notification identifier, when the platform gives one
	Title  string
	Body   string
	Action string         // OnOpen: action identifier of a notification action button, "" for a plain tap
	Data   map[string]any // the whole payload: APNs userInfo (with "aps") on iOS, FCM data on Android
}

Message is a remote notification.

以 MIT 许可发布