RezinaLMS-BackEnd/internal/services/notification/fcm_push.go
Yared Yemane c66a7be28f fix: surface FCM bulk push delivery failures
Return per-token Firebase errors and project ID in bulk push responses, log actionable hints, and only deactivate truly expired tokens.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-16 01:53:52 -07:00

63 lines
2.0 KiB
Go

package notificationservice
import (
"strings"
"firebase.google.com/go/v4/messaging"
)
// BulkPushFailure describes a single FCM delivery failure for one device token.
type BulkPushFailure struct {
UserID int64 `json:"user_id"`
TokenPreview string `json:"token_preview"`
Error string `json:"error"`
Hint string `json:"hint,omitempty"`
}
// BulkPushDeliveryResult is the outcome of a bulk FCM send attempt.
type BulkPushDeliveryResult struct {
Sent int
Failed int
DevicesTargeted int
FCMProjectID string
Failures []BulkPushFailure
}
func tokenPreview(token string) string {
token = strings.TrimSpace(token)
if len(token) <= 12 {
return token
}
return "..." + token[len(token)-12:]
}
func fcmFailureHint(err error) string {
switch {
case messaging.IsSenderIDMismatch(err), messaging.IsMismatchedCredential(err):
return "The device token was issued by a different Firebase project than the server FCM_SERVICE_ACCOUNT_KEY. Ensure the client app uses the same Firebase project."
case messaging.IsUnregistered(err), messaging.IsRegistrationTokenNotRegistered(err):
return "The device token is expired or invalid. Re-register via POST /api/v1/devices/register from the client app."
case messaging.IsInvalidAPNSCredentials(err), messaging.IsThirdPartyAuthError(err):
return "Firebase cannot deliver to Apple devices. Configure APNS credentials in the Firebase console."
case messaging.IsInvalidArgument(err):
return "FCM rejected the message or token format. Verify the client sends a native FCM registration token (not Expo or another push provider)."
default:
return ""
}
}
func shouldDeactivateFCMToken(err error) bool {
return messaging.IsUnregistered(err) || messaging.IsRegistrationTokenNotRegistered(err)
}
func buildFCMNotification(title, body, imageURL string) *messaging.Notification {
n := &messaging.Notification{
Title: title,
Body: body,
}
if strings.TrimSpace(imageURL) != "" {
n.ImageURL = imageURL
}
return n
}