silvermobi/services/push.go

80 lines
1.7 KiB
Go
Raw Normal View History

2023-08-08 10:28:43 +00:00
package services
import (
"context"
2023-12-08 06:35:04 +00:00
"google.golang.org/protobuf/types/known/structpb"
2023-08-08 10:28:43 +00:00
"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
2023-12-08 06:35:04 +00:00
Data *structpb.Struct
ID string
2023-08-08 10:28:43 +00:00
}
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{
2023-12-08 06:35:04 +00:00
Data: notification.Data,
ID: notification.ID,
2023-08-08 10:28:43 +00:00
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
}