Skip to content

Go package for sending push notifications to Expo (React Native) apps.

License

Notifications You must be signed in to change notification settings

orsinium-labs/expofier

Repository files navigation

expofier

[ 📄 docs ] [ 🐙 github ]

Go package for sending push notifications to Expo (React Native) apps.

It provides both a low-level API client as well as a backgorund service taking care of delivery guarantees.

Installation

go get github.com/orsinium-labs/expofier

Usage

See the official Expo docs for instructions on configuring the client app and obtaining the push token.

Low-level usage:

client := expofier.NewClient()

// Send message:
msg := expofier.Message{
    To:   []expofier.Token{"ExpoPushToken[a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11]"},
    Body: "Hello world!",
}
ctx := context.Background()
ticket, err := client.SendMessage(ctx, msg)
if err != nil {
    return fmt.Errorf("send message: %w", err)
}

// Check message delivery status:
receipt := client.FetchReceipt(ctx, ticket)
if receipt != nil {
    if receipt.Error != nil {
        return fmt.Errorf("deliver message: %w", err)
    }
    fmt.Println("message delivered")
} else {
    fmt.Println("message not delivered yet")
}

For high-level usage, the package provides Service which takes care of grouping messages (to minimize network requests), chunking messages, retries, and checking the delivery status.

service := expofier.NewService()
ctx := context.Background()
go service.Run(ctx)

msg := expofier.Message{
    To:   []expofier.Token{"ExpoPushToken[a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11]"},
    Body: "Hello world!",
}
promise := service.Send(ctx, msg)
promise.Wait(ctx)
err := promise.Err()
if err != nil {
    return err
}

It is recommended to check for ErrDeviceNotRegistered and remove invalid tokens from your database:

if err == expofier.ErrDeviceNotRegistered {
    removeTokenFromDB(token)
}