Skip to content

Commit

Permalink
chore(fcm): notification handling logic for sound field (#797)
Browse files Browse the repository at this point in the history
* chore(fcm): notification handling logic for sound field

- Remove `IsGRPC` field from `PushNotification` struct
- Simplify condition by removing `IsGRPC` check in `GetAndroidNotification`
- Add checks for `APNS` and `Android` fields in `GetAndroidNotification`
- Remove `IsGRPC` initialization in `Send` method of `Server`

Signed-off-by: Bo-Yi Wu <[email protected]>

* refactor: refactor sound request handling and validation

- Change `req.Sound` type check from string to interface
- Add type assertion for `req.Sound` to string
- Ensure `req.APNS` and `req.Android` are only set if `req.Sound` is a string

Signed-off-by: Bo-Yi Wu <[email protected]>

---------

Signed-off-by: Bo-Yi Wu <[email protected]>
  • Loading branch information
appleboy authored Jul 17, 2024
1 parent 0199ac4 commit a82bebe
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
1 change: 0 additions & 1 deletion notify/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ type PushNotification struct {

// ref: https://github.com/sideshow/apns2/blob/54928d6193dfe300b6b88dad72b7e2ae138d4f0a/payload/builder.go#L7-L24
InterruptionLevel string `json:"interruption_level,omitempty"`
IsGRPC bool `json:"is_grpc,omitempty"`
}

// Bytes for queue message
Expand Down
27 changes: 17 additions & 10 deletions notify/notification_fcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,25 @@ func GetAndroidNotification(req *PushNotification) []*messaging.Message {
}

// Check if the notification has a sound
if req.Sound != "" && req.IsGRPC {
req.APNS = &messaging.APNSConfig{
Payload: &messaging.APNSPayload{
Aps: &messaging.Aps{
Sound: req.Sound.(string),
if req.Sound != nil {
sound, ok := req.Sound.(string)

if req.APNS == nil && ok {
req.APNS = &messaging.APNSConfig{
Payload: &messaging.APNSPayload{
Aps: &messaging.Aps{
Sound: sound,
},
},
},
}
}
req.Android = &messaging.AndroidConfig{
Notification: &messaging.AndroidNotification{
Sound: req.Sound.(string),
},

if req.Android == nil && ok {
req.Android = &messaging.AndroidConfig{
Notification: &messaging.AndroidNotification{
Sound: sound,
},
}
}
}

Expand Down
1 change: 0 additions & 1 deletion rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func (s *Server) Send(ctx context.Context, in *proto.NotificationRequest) (*prot
Priority: strings.ToLower(in.GetPriority().String()),
PushType: in.PushType,
Development: in.Development,
IsGRPC: true,
}

if badge > 0 {
Expand Down

0 comments on commit a82bebe

Please sign in to comment.