silvermobi/services/push.go

75 lines
1.6 KiB
Go
Raw Normal View History

2023-08-08 10:28:43 +00:00
package services
import (
"context"
"github.com/appleboy/gorush/rpc/proto"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
)
const (
// PlatFormIos constant is 1 for iOS
PushToIos = iota + 1
// PlatFormAndroid constant is 2 for Android
PushToAndroid
// PlatFormHuawei constant is 3 for Huawei
PushToHuawei
// PlatformUnifiedPush constant is 4 for UnifiedPush
PushToUnifiedPush
// PlatformSMSFactor constant is 5 for SMSFactor
PushToSMSFactor
)
type Notification struct {
Platform int32
Recipients []string
Message string
Title string
}
type PushService struct {
Client proto.GorushClient
}
func NewPushService(address string) (*PushService, error) {
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
return nil, err
}
return &PushService{
Client: proto.NewGorushClient(conn),
}, nil
}
func (s *PushService) Send(notification Notification) error {
log.Debug().
Int32("Platform", notification.Platform).
Strs("recipients", notification.Recipients).
Str("notification_message", notification.Message).
Str("notification_title", notification.Title).
Msg("Send notification")
resp, err := s.Client.Send(context.Background(), &proto.NotificationRequest{
Platform: notification.Platform,
Tokens: notification.Recipients,
Message: notification.Message,
Title: notification.Title,
Priority: proto.NotificationRequest_HIGH,
Alert: &proto.Alert{
Title: notification.Title,
Body: notification.Message,
},
})
if err != nil {
return err
}
log.Debug().Str("response", resp.String()).Msg("notification sent")
return nil
}