mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-21 09:19:01 +08:00
fcm ios push badge
This commit is contained in:
+54
-40
@@ -5,7 +5,6 @@ import (
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/db"
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/tools/splitter"
|
||||
"context"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
@@ -47,53 +46,68 @@ func newFcmClient() *Fcm {
|
||||
return &Fcm{FcmMsgCli: fcmMsgClient}
|
||||
}
|
||||
|
||||
func (f *Fcm) Push(accounts []string, alert, detailContent, operationID string, opts push.PushOpts) (string, error) {
|
||||
func (f *Fcm) Push(accounts []string, title, detailContent, operationID string, opts push.PushOpts) (string, error) {
|
||||
// accounts->registrationToken
|
||||
Tokens := make([]string, 0)
|
||||
allTokens := make(map[string][]string, 0)
|
||||
for _, account := range accounts {
|
||||
IosfcmToken, IosErr := db.DB.GetFcmToken(account, 1)
|
||||
AndroidfcmToken, AndroidErr := db.DB.GetFcmToken(account, 2)
|
||||
|
||||
if IosErr == nil {
|
||||
Tokens = append(Tokens, IosfcmToken)
|
||||
}
|
||||
if AndroidErr == nil {
|
||||
Tokens = append(Tokens, AndroidfcmToken)
|
||||
var personTokens []string
|
||||
for _, v := range push.PushTerminal {
|
||||
Token, err := db.DB.GetFcmToken(account, v)
|
||||
if err == nil {
|
||||
personTokens = append(personTokens, Token)
|
||||
}
|
||||
}
|
||||
allTokens[account] = personTokens
|
||||
}
|
||||
Success := 0
|
||||
Fail := 0
|
||||
result := splitter.NewSplitter(SinglePushCountLimit, Tokens).GetSplitResult()
|
||||
Msg := new(messaging.MulticastMessage)
|
||||
Msg.Notification = &messaging.Notification{}
|
||||
Msg.Notification.Body = detailContent
|
||||
Msg.Notification.Title = alert
|
||||
Msg.APNS = &messaging.APNSConfig{Payload: &messaging.APNSPayload{Aps: &messaging.Aps{}}}
|
||||
if opts.IOSBadgeCount {
|
||||
i := 1
|
||||
Msg.APNS.Payload.Aps.Badge = &i
|
||||
}
|
||||
if opts.IOSPushSound != "" {
|
||||
Msg.APNS.Payload.Aps.Sound = opts.IOSPushSound
|
||||
}
|
||||
notification := &messaging.Notification{}
|
||||
notification.Body = detailContent
|
||||
notification.Title = title
|
||||
var messages []*messaging.Message
|
||||
ctx := context.Background()
|
||||
for _, v := range result {
|
||||
Msg.Tokens = v.Item
|
||||
//SendMulticast sends the given multicast message to all the FCM registration tokens specified.
|
||||
//The tokens array in MulticastMessage may contain up to 500 tokens.
|
||||
//SendMulticast uses the `SendAll()` function to send the given message to all the target recipients.
|
||||
//The responses list obtained from the return value corresponds to the order of the input tokens.
|
||||
//An error from SendMulticast indicates a total failure -- i.e.
|
||||
//the message could not be sent to any of the recipients.
|
||||
//Partial failures are indicated by a `BatchResponse` return value.
|
||||
response, err := f.FcmMsgCli.SendMulticast(ctx, Msg)
|
||||
if err != nil {
|
||||
Fail = Fail + len(v.Item)
|
||||
log.Info(operationID, "some token push err", err.Error(), len(v.Item))
|
||||
continue
|
||||
apns := &messaging.APNSConfig{Payload: &messaging.APNSPayload{Aps: &messaging.Aps{Sound: opts.IOSPushSound}}}
|
||||
for uid, personTokens := range allTokens {
|
||||
messageCount := len(messages)
|
||||
if messageCount >= SinglePushCountLimit {
|
||||
response, err := f.FcmMsgCli.SendAll(ctx, messages)
|
||||
if err != nil {
|
||||
Fail = Fail + messageCount
|
||||
log.Info(operationID, "some token push err", err.Error(), messageCount)
|
||||
} else {
|
||||
Success = Success + response.SuccessCount
|
||||
Fail = Fail + response.FailureCount
|
||||
}
|
||||
messages = messages[0:0]
|
||||
}
|
||||
if opts.IOSBadgeCount {
|
||||
unreadCountSum, err := db.DB.IncrUserBadgeUnreadCountSum(uid)
|
||||
if err == nil {
|
||||
apns.Payload.Aps.Badge = &unreadCountSum
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
}
|
||||
for _, token := range personTokens {
|
||||
temp := &messaging.Message{
|
||||
Token: token,
|
||||
Notification: notification,
|
||||
APNS: apns,
|
||||
}
|
||||
messages = append(messages, temp)
|
||||
}
|
||||
|
||||
}
|
||||
messageCount := len(messages)
|
||||
if messageCount > 0 {
|
||||
response, err := f.FcmMsgCli.SendAll(ctx, messages)
|
||||
if err != nil {
|
||||
Fail = Fail + messageCount
|
||||
log.Info(operationID, "some token push err", err.Error(), messageCount)
|
||||
} else {
|
||||
Success = Success + response.SuccessCount
|
||||
Fail = Fail + response.FailureCount
|
||||
}
|
||||
Success = Success + response.SuccessCount
|
||||
Fail = Fail + response.FailureCount
|
||||
}
|
||||
return strconv.Itoa(Success) + " Success," + strconv.Itoa(Fail) + " Fail", nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user