Files
open-im-server/internal/push/offlinepush/fcm/push.go
T

122 lines
3.3 KiB
Go
Raw Normal View History

2022-07-28 18:58:57 +08:00
package fcm
import (
2023-03-03 19:59:10 +08:00
"OpenIM/internal/push/offlinepush"
2023-02-23 19:15:30 +08:00
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/db/cache"
"context"
firebase "firebase.google.com/go"
"firebase.google.com/go/messaging"
2023-02-20 10:13:29 +08:00
"github.com/go-redis/redis/v8"
"google.golang.org/api/option"
2023-02-20 10:13:29 +08:00
"path/filepath"
)
2022-07-29 16:07:05 +08:00
const SinglePushCountLimit = 400
2023-02-20 10:13:29 +08:00
var Terminal = []int{constant.IOSPlatformID, constant.AndroidPlatformID, constant.WebPlatformID}
type Fcm struct {
2023-02-20 10:13:29 +08:00
fcmMsgCli *messaging.Client
2023-03-03 17:42:26 +08:00
cache cache.Model
}
2023-03-03 17:42:26 +08:00
func NewClient(cache cache.Model) *Fcm {
opt := option.WithCredentialsFile(filepath.Join(config.Root, "config", config.Config.Push.Fcm.ServiceAccount))
fcmApp, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
return nil
}
2023-02-20 10:13:29 +08:00
// auth
// fcmClient, err := fcmApp.Auth(context.Background())
// if err != nil {
// return
// }
ctx := context.Background()
2022-07-28 18:14:58 +08:00
fcmMsgClient, err := fcmApp.Messaging(ctx)
if err != nil {
2022-07-28 18:14:58 +08:00
panic(err.Error())
return nil
}
2023-02-20 10:13:29 +08:00
return &Fcm{fcmMsgCli: fcmMsgClient}
}
2023-03-03 19:59:10 +08:00
func (f *Fcm) Push(ctx context.Context, userIDs []string, title, content string, opts *offlinepush.Opts) error {
// accounts->registrationToken
2022-09-01 21:05:16 +08:00
allTokens := make(map[string][]string, 0)
2023-02-20 10:13:29 +08:00
for _, account := range userIDs {
2022-09-01 21:05:16 +08:00
var personTokens []string
2023-02-20 10:13:29 +08:00
for _, v := range Terminal {
Token, err := f.cache.GetFcmToken(ctx, account, v)
2022-09-01 21:05:16 +08:00
if err == nil {
personTokens = append(personTokens, Token)
}
}
2022-09-01 21:05:16 +08:00
allTokens[account] = personTokens
}
Success := 0
Fail := 0
2022-09-01 21:05:16 +08:00
notification := &messaging.Notification{}
2023-02-20 10:13:29 +08:00
notification.Body = content
2022-09-01 21:05:16 +08:00
notification.Title = title
var messages []*messaging.Message
2023-02-20 10:13:29 +08:00
for userID, personTokens := range allTokens {
2022-09-02 12:03:28 +08:00
apns := &messaging.APNSConfig{Payload: &messaging.APNSPayload{Aps: &messaging.Aps{Sound: opts.IOSPushSound}}}
2022-09-01 21:05:16 +08:00
messageCount := len(messages)
if messageCount >= SinglePushCountLimit {
2023-02-20 10:13:29 +08:00
response, err := f.fcmMsgCli.SendAll(ctx, messages)
2022-09-01 21:05:16 +08:00
if err != nil {
Fail = Fail + messageCount
} else {
Success = Success + response.SuccessCount
Fail = Fail + response.FailureCount
}
messages = messages[0:0]
}
if opts.IOSBadgeCount {
2023-02-20 10:13:29 +08:00
unreadCountSum, err := f.cache.IncrUserBadgeUnreadCountSum(ctx, userID)
2022-09-01 21:05:16 +08:00
if err == nil {
apns.Payload.Aps.Badge = &unreadCountSum
} else {
2023-02-20 10:13:29 +08:00
//log.Error(operationID, "IncrUserBadgeUnreadCountSum redis err", err.Error(), uid)
2022-09-07 16:41:18 +08:00
Fail++
continue
}
} else {
2023-02-20 10:13:29 +08:00
unreadCountSum, err := f.cache.GetUserBadgeUnreadCountSum(ctx, userID)
2022-09-07 16:41:18 +08:00
if err == nil && unreadCountSum != 0 {
apns.Payload.Aps.Badge = &unreadCountSum
2023-02-20 10:13:29 +08:00
} else if err == redis.Nil || unreadCountSum == 0 {
2022-09-07 16:41:18 +08:00
zero := 1
apns.Payload.Aps.Badge = &zero
} else {
2023-02-20 10:13:29 +08:00
//log.Error(operationID, "GetUserBadgeUnreadCountSum redis err", err.Error(), uid)
2022-09-07 16:41:18 +08:00
Fail++
2022-09-01 21:05:16 +08:00
continue
}
}
for _, token := range personTokens {
temp := &messaging.Message{
2023-02-20 10:13:29 +08:00
Data: map[string]string{"ex": opts.Ex},
2022-09-01 21:05:16 +08:00
Token: token,
Notification: notification,
APNS: apns,
}
messages = append(messages, temp)
}
}
messageCount := len(messages)
if messageCount > 0 {
2023-02-20 10:13:29 +08:00
response, err := f.fcmMsgCli.SendAll(ctx, messages)
if err != nil {
2022-09-01 21:05:16 +08:00
Fail = Fail + messageCount
2023-02-20 10:13:29 +08:00
//log.Info(operationID, "some token push err", err.Error(), messageCount)
2022-09-01 21:05:16 +08:00
} else {
Success = Success + response.SuccessCount
Fail = Fail + response.FailureCount
}
}
2023-02-20 10:13:29 +08:00
return nil
}