2024-10-24 16:56:22 +00:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
|
2024-11-26 13:46:03 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-10-24 16:56:22 +00:00
|
|
|
"github.com/sideshow/apns2"
|
|
|
|
payload2 "github.com/sideshow/apns2/payload"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AppleNotifyProvider struct {
|
|
|
|
topic string
|
|
|
|
conn *apns2.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *AppleNotifyProvider) Push(in pushkit.Notification, tk string) error {
|
|
|
|
data := payload2.
|
|
|
|
NewPayload().
|
|
|
|
AlertTitle(in.Title).
|
|
|
|
AlertBody(in.Body).
|
|
|
|
Category(in.Topic).
|
|
|
|
Custom("metadata", in.Metadata).
|
|
|
|
Sound("default").
|
|
|
|
MutableContent()
|
|
|
|
if len(in.Subtitle) > 0 {
|
|
|
|
data = data.AlertSubtitle(in.Subtitle)
|
|
|
|
}
|
|
|
|
if avatar, ok := in.Metadata["avatar"]; ok {
|
|
|
|
data = data.Custom("avatar", avatar)
|
|
|
|
}
|
|
|
|
if picture, ok := in.Metadata["picture"]; ok {
|
|
|
|
data = data.Custom("picture", picture)
|
|
|
|
}
|
|
|
|
rawData, err := data.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
payload := &apns2.Notification{
|
|
|
|
DeviceToken: tk,
|
2024-11-26 14:28:37 +00:00
|
|
|
Topic: v.topic,
|
2024-10-24 16:56:22 +00:00
|
|
|
Payload: rawData,
|
|
|
|
}
|
2024-11-26 13:46:03 +00:00
|
|
|
|
|
|
|
resp, err := v.conn.Push(payload)
|
|
|
|
if resp != nil {
|
|
|
|
log.Debug().
|
|
|
|
Str("token", tk).
|
|
|
|
Str("remote_id", resp.ApnsID).
|
|
|
|
Str("remote_uuid", resp.ApnsUniqueID).
|
|
|
|
Int("status", resp.StatusCode).
|
|
|
|
Time("timestamp", resp.Timestamp.Time).
|
|
|
|
Str("reason", resp.Reason).
|
|
|
|
Msg("Pushed once notification to apple")
|
|
|
|
}
|
|
|
|
|
2024-10-24 16:56:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *AppleNotifyProvider) GetName() string {
|
|
|
|
return "apns"
|
|
|
|
}
|