diff --git a/config/openim-5c6c0-firebase-adminsdk-ppwol-8765884a78.json b/config/openim-5c6c0-firebase-adminsdk-ppwol-8765884a78.json deleted file mode 100644 index e6870bbc1..000000000 --- a/config/openim-5c6c0-firebase-adminsdk-ppwol-8765884a78.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "type": "service_account", - "project_id": "openim-5c6c0", - "private_key_id": "8765884a7875fd3c0a73bbc1718bdaeca39fd609", - "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC90b1YDn14JsY8\nu5a0RIx7yAWmsn8JuoceVPvFzAv45iuU3MfsD51TKY+gjmydFAMpUuEQKYF2hXie\nJt5+EaBf/RWch2zmHK+husQVJpRvKmTNPjT/fT2FqneozriQZ7QedRiKhkv2lb9A\n3QFO3kmeAI7W4DR+22wadNKBX/REv+nZECz6uaPZ9cRvUYb8GCYkcteau62P0z4E\n3sOHe52Pix0OV06vI47PTNmbhZOozF0wdVfxOe5eE5kQ8VZq2oQ+6SHZwNhl+7py\nHAN8Cgq4JXGrjNdafku9UNuLpYLv3a7DejNe1R5tkITE2P01Us43T7HRuZ8W8mV0\nCl9DU+HfAgMBAAECggEAVxu4M3+6znekw1wmpaVLfsZk6YHCULmbpizDuZqQ5/Dg\nkRjcWhU0UmShN8IET/VOGhmhlOQnODe6CYG22s3F9ibTUxjGvbbuVl65+ybQOU+Q\nIKvqcFCN+hgnf30WL5aXjt6Xm4JTu94ufVqubf4OdIa3Bh6vmIOjUWTI8XwSQbWx\n8llfOL4ElMdmtxg0N5iDmkSXfTgZWfty861s76bqazD1cQhMOYPXPnGP78DKyOjH\nN6XchHDXm5I0eEIa+jACW0p6DMArdwREhVgfQotxzRfExXLUR5XxJnMNuA1IwnWF\nJRAg9cEUeIQoJY/kDuNQlxsS2Manb+3exNk52BlFAQKBgQDjo+tENZ3Ozt/JPtev\nOHolhkGCJNWMJPV6SVuBta1/u0/T8HKxkXdNOrooM+dsK5xk+V6Dp8wwrQiUSy/f\n6RVXUcTKTahlTNhwrxmRisJbeyEoX8tJGDVjwJba4sKdkfsWeh4IduyvwQnRCNn8\nmx9Pfp5EyCnWkpyJLvM7OykqXwKBgQDVd5qD88UOvV73xZUPzPlnNQHzBS+TgPlI\n3CLesi8i5I3zBHYS8ynCi0eNyaLqmtatVL6XhnUAWI/U9NKNmP3AH2pigBVIAjM8\nMmSO8iaYYQA3/TOlIsNE6yFjd3t54yFkQ7J6yLPRQWWKwE+3hQ3VZ5WmyM5jOh/s\nD+g5lOd4gQKBgQCH4qSg7eSKvCEID5ROi0cWuULHfldfNfy3B60xC5NK6TRozmKY\nrr3pgwH216zwfEP6XoVVz8dq0w+I/izQ22Ea47u2C49XEP0unseDgrIsS57qa+x8\ncJAGQMOMW8pSpv6cVz8wTFVTQMcsWb1ONgcFvA6b/mRDKvd4SGd3VwRmvwKBgD48\nP5VG5eXVOjHcrgfMR85aOvZCRcuoZ4VhgN0ScAGpRuVaIJ6HvF4Ww0bISbJCcGJa\nebKtpcDzEQQSWKyltR1lS1vqYcn7sjpTqtOuL6hvZ2Prczoq92lJcBOSyzIPETYt\nMoTvrNVOTq90QAmORik4qP3WP63YTWRCgv7+tbmBAoGALVYhJB3KDuOfn4gowiBL\neCCsW93VdLBmrR4PIZlaLLNZSUu3hmM+XNhKCLzMHMoTtptHKq5aQusDlefxhioS\nuWuUXqFyZRdjP81lLBMZQmh6j7kPLC/fWZdN5iBlZX2JTANaOQZHDy8KEHMYq6zf\nYzLbTvrNYxxKyb5q6IXrErk=\n-----END PRIVATE KEY-----\n", - "client_email": "firebase-adminsdk-ppwol@openim-5c6c0.iam.gserviceaccount.com", - "client_id": "117878683878601533433", - "auth_uri": "https://accounts.google.com/o/oauth2/auth", - "token_uri": "https://oauth2.googleapis.com/token", - "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", - "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-ppwol%40openim-5c6c0.iam.gserviceaccount.com" -} diff --git a/internal/push/fcm/push.go b/internal/push/fcm/push.go index a7a928978..7c1f55d00 100644 --- a/internal/push/fcm/push.go +++ b/internal/push/fcm/push.go @@ -5,10 +5,10 @@ import ( "Open_IM/pkg/common/config" "Open_IM/pkg/common/db" "Open_IM/pkg/common/log" + "Open_IM/pkg/tools" "context" firebase "firebase.google.com/go" "firebase.google.com/go/messaging" - "fmt" "google.golang.org/api/option" "path/filepath" "strconv" @@ -47,7 +47,6 @@ func newFcmClient() *Fcm { } func (f *Fcm) Push(accounts []string, alert, detailContent, operationID string, opts push.PushOpts) (string, error) { - //需要一个客户端的Token // accounts->registrationToken Tokens := make([]string, 0) for _, account := range accounts { @@ -61,23 +60,16 @@ func (f *Fcm) Push(accounts []string, alert, detailContent, operationID string, Tokens = append(Tokens, AndroidfcmToken) } } - tokenlen := len(Tokens) - // 500组为一个推送,我们用400好了 - pages := int((tokenlen-1)/SinglePushCountLimit + 1) Success := 0 Fail := 0 - fmt.Println(operationID, "fcm args", tokenlen, pages) - for i := 0; i < pages; i++ { - Msg := new(messaging.MulticastMessage) - Msg.Notification = &messaging.Notification{} - Msg.Notification.Body = detailContent - Msg.Notification.Title = alert - ctx := context.Background() - max := (i+1)*SinglePushCountLimit - 1 - if max >= tokenlen { - max = tokenlen - 1 - } - Msg.Tokens = Tokens[i*SinglePushCountLimit : max] + result := tools.NewSplitter(SinglePushCountLimit, Tokens).GetSplitResult() + Msg := new(messaging.MulticastMessage) + Msg.Notification = &messaging.Notification{} + Msg.Notification.Body = detailContent + Msg.Notification.Title = alert + 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. @@ -87,7 +79,9 @@ func (f *Fcm) Push(accounts []string, alert, detailContent, operationID string, //Partial failures are indicated by a `BatchResponse` return value. response, err := f.FcmMsgCli.SendMulticast(ctx, Msg) if err != nil { - return "", err + Fail = Fail + len(v.Item) + log.Info(operationID, "some token push err", err.Error(), len(v.Item)) + continue } Success = Success + response.SuccessCount Fail = Fail + response.FailureCount diff --git a/internal/push/logic/callback.go b/internal/push/logic/callback.go index 9cd5158ba..cc897322d 100644 --- a/internal/push/logic/callback.go +++ b/internal/push/logic/callback.go @@ -11,7 +11,7 @@ import ( http2 "net/http" ) -func callbackOfflinePush(operationID string, userIDList []string, msg *commonPb.MsgData) cbApi.CommonCallbackResp { +func callbackOfflinePush(operationID string, userIDList []string, msg *commonPb.MsgData, offlinePushUserIDList *[]string) cbApi.CommonCallbackResp { callbackResp := cbApi.CommonCallbackResp{OperationID: operationID} if !config.Config.Callback.CallbackOfflinePush.Enable { return callbackResp @@ -46,6 +46,10 @@ func callbackOfflinePush(operationID string, userIDList []string, msg *commonPb. return callbackResp } } + if resp.ErrCode == constant.CallbackHandleSuccess && resp.ActionCode == constant.ActionAllow && len(resp.UserIDList) != 0 { + *offlinePushUserIDList = resp.UserIDList + } + log.NewDebug(operationID, utils.GetSelfFuncName(), offlinePushUserIDList, resp.UserIDList) return callbackResp } diff --git a/internal/push/logic/push_to_client.go b/internal/push/logic/push_to_client.go index 11f304433..1ab1ee989 100644 --- a/internal/push/logic/push_to_client.go +++ b/internal/push/logic/push_to_client.go @@ -127,7 +127,7 @@ func MsgToUser(pushMsg *pbPush.PushMsgReq) { } } - callbackResp := callbackOfflinePush(pushMsg.OperationID, UIDList, pushMsg.MsgData) + callbackResp := callbackOfflinePush(pushMsg.OperationID, UIDList, pushMsg.MsgData, &[]string{}) log.NewDebug(pushMsg.OperationID, utils.GetSelfFuncName(), "offline callback Resp") if callbackResp.ErrCode != 0 { log.NewError(pushMsg.OperationID, utils.GetSelfFuncName(), "callbackOfflinePush result: ", callbackResp) @@ -261,7 +261,9 @@ func MsgToSuperGroupUser(pushMsg *pbPush.PushMsgReq) { } } if len(onlineFailedUserIDList) > 0 { - callbackResp := callbackOfflinePush(pushMsg.OperationID, onlineFailedUserIDList, pushMsg.MsgData) + var offlinePushUserIDList []string + var needOfflinePushUserIDList []string + callbackResp := callbackOfflinePush(pushMsg.OperationID, onlineFailedUserIDList, pushMsg.MsgData, &offlinePushUserIDList) log.NewDebug(pushMsg.OperationID, utils.GetSelfFuncName(), "offline callback Resp") if callbackResp.ErrCode != 0 { log.NewError(pushMsg.OperationID, utils.GetSelfFuncName(), "callbackOfflinePush result: ", callbackResp) @@ -270,6 +272,11 @@ func MsgToSuperGroupUser(pushMsg *pbPush.PushMsgReq) { log.NewDebug(pushMsg.OperationID, utils.GetSelfFuncName(), "offlinePush stop") return } + if len(offlinePushUserIDList) > 0 { + needOfflinePushUserIDList = offlinePushUserIDList + } else { + needOfflinePushUserIDList = onlineFailedUserIDList + } if offlinePusher == nil { return } @@ -278,7 +285,7 @@ func MsgToSuperGroupUser(pushMsg *pbPush.PushMsgReq) { log.NewError(pushMsg.OperationID, utils.GetSelfFuncName(), "GetOfflinePushOpts failed", pushMsg, err.Error()) } log.NewInfo(pushMsg.OperationID, utils.GetSelfFuncName(), onlineFailedUserIDList, content, jsonCustomContent, "opts:", opts) - pushResult, err := offlinePusher.Push(onlineFailedUserIDList, content, jsonCustomContent, pushMsg.OperationID, opts) + pushResult, err := offlinePusher.Push(needOfflinePushUserIDList, content, jsonCustomContent, pushMsg.OperationID, opts) if err != nil { log.NewError(pushMsg.OperationID, "offline push error", pushMsg.String(), err.Error()) } else { diff --git a/pkg/call_back_struct/push.go b/pkg/call_back_struct/push.go index e0b46bd0c..e3803a0e2 100644 --- a/pkg/call_back_struct/push.go +++ b/pkg/call_back_struct/push.go @@ -15,6 +15,7 @@ type CallbackBeforePushReq struct { type CallbackBeforePushResp struct { *CommonCallbackResp + UserIDList []string `json:"userIDList"` } type CallbackBeforeSuperGroupOnlinePushReq struct { diff --git a/pkg/tools/tools.go b/pkg/tools/tools.go new file mode 100644 index 000000000..a3baa5f3e --- /dev/null +++ b/pkg/tools/tools.go @@ -0,0 +1,28 @@ +package tools + +type SplitResult struct { + Item []string +} +type Splitter struct { + splitCount int + data []string +} + +func NewSplitter(splitCount int, data []string) *Splitter { + return &Splitter{splitCount: splitCount, data: data} +} +func (s *Splitter) GetSplitResult() (result []*SplitResult) { + remain := len(s.data) % s.splitCount + integer := len(s.data) / s.splitCount + for i := 0; i < integer; i++ { + r := new(SplitResult) + r.Item = s.data[i*s.splitCount : (i+1)*s.splitCount] + result = append(result, r) + } + if remain > 0 { + r := new(SplitResult) + r.Item = s.data[integer*s.splitCount:] + result = append(result, r) + } + return result +}