package services import ( "context" "github.com/appleboy/gorush/rpc/proto" "github.com/rs/zerolog/log" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" "google.golang.org/protobuf/types/known/structpb" ) 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 Data *structpb.Struct ID string } type PushService struct { Client proto.GorushClient } func NewPushService(address string) (*PushService, error) { conn, err := grpc.NewClient(address, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { return nil, err } return &PushService{ Client: proto.NewGorushClient(conn), }, nil } func (s *PushService) Send(notification Notification) error { var ( resp *proto.NotificationReply err error ) log.Debug(). Int32("Platform", notification.Platform). Strs("recipients", notification.Recipients). Str("notification_message", notification.Message). Str("notification_title", notification.Title). Msg("Send notification") if resp, err = s.Client.Send(context.Background(), &proto.NotificationRequest{ Data: notification.Data, ID: notification.ID, 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, }, }); err != nil { return err } log.Debug().Str("response", resp.String()).Msg("notification sent") return nil }