|
@@ -2,6 +2,7 @@ package server
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "encoding/json"
|
|
|
"log"
|
|
|
"time"
|
|
|
|
|
@@ -48,19 +49,45 @@ func NewFirebaseMessageServer(repos *repositories.Repositories) *FirebaseMessage
|
|
|
if err != nil {
|
|
|
log.Fatalln(err)
|
|
|
}
|
|
|
+
|
|
|
firebaseMessageServer := &FirebaseMessageServer{app: app, repos: repos}
|
|
|
go firebaseMessageServer.queueRun(ctx)
|
|
|
return firebaseMessageServer
|
|
|
}
|
|
|
|
|
|
func (f *FirebaseMessageServer) Send(ctx context.Context, uid string, message Message) error {
|
|
|
+ // "newMsg": { "title": "News", "body": "click to view" },
|
|
|
+ // "chatting": { "title": "Chat Message", "body": "click to view" },
|
|
|
+ // "location": { "title": "Location Changed", "body": "click to view" },
|
|
|
+ // "fence": { "title": "Electronic Fence", "body": "click to view" },
|
|
|
+ // "notification": { "title": "New Notification","body": "click to view" },
|
|
|
+ // "videoCall": { "title": "Call Notification", "body": "click to view" }
|
|
|
+ var title string
|
|
|
+ var body = "click to view"
|
|
|
+ messageType := message.MessageType()
|
|
|
+ switch messageType {
|
|
|
+ case MessageTypeDownChating, MessageTypeUpChating:
|
|
|
+ title = "Chat Message"
|
|
|
+ case MessageTypeLocation:
|
|
|
+ title = "Location Changed"
|
|
|
+ case MessageTypeNotification:
|
|
|
+ // var notification Notification
|
|
|
+ // _ = json.Unmarshal(message.Data(), ¬ification)
|
|
|
+ // switch notification.Content.ID {
|
|
|
+ // }
|
|
|
+ title = "New Notification"
|
|
|
+ case MessageTypeVideoCall:
|
|
|
+ title = "Call Notification"
|
|
|
+ default:
|
|
|
+ title = "News"
|
|
|
+ }
|
|
|
+
|
|
|
// 加入消息到 fcm 队列
|
|
|
queue := &models.FirebaseMessagingQueue{
|
|
|
- Title: "New message",
|
|
|
- Body: "received a new message",
|
|
|
- Receiver: uid,
|
|
|
- Data: string(serializeMessage(message)),
|
|
|
- // Status: DefaultQueueStatus,
|
|
|
+ Title: title,
|
|
|
+ Body: body,
|
|
|
+ Receiver: uid,
|
|
|
+ Data: string(serializeMessage(message)),
|
|
|
RemainingRetries: DefaultRemainingRetries,
|
|
|
}
|
|
|
if err := f.repos.FirebaseMessageQueueRepository.Create(ctx, queue); err != nil {
|
|
@@ -129,13 +156,23 @@ func (f *FirebaseMessageServer) queueRun(ctx context.Context) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ aps := map[string]interface{}{
|
|
|
+ "alert": map[string]interface{}{
|
|
|
+ "title": queue.Title,
|
|
|
+ "body": queue.Body,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ apsBuf, _ := json.Marshal(aps)
|
|
|
msg := &messaging.Message{
|
|
|
Token: firebaseToken.Token,
|
|
|
- Data: map[string]string{"message": queue.Data},
|
|
|
- Notification: &messaging.Notification{
|
|
|
- Title: queue.Title,
|
|
|
- Body: queue.Body,
|
|
|
+ Data: map[string]string{
|
|
|
+ "aps": string(apsBuf),
|
|
|
+ "message": queue.Data,
|
|
|
},
|
|
|
+ // Notification: &messaging.Notification{
|
|
|
+ // Title: queue.Title,
|
|
|
+ // Body: queue.Body,
|
|
|
+ // },
|
|
|
}
|
|
|
|
|
|
zap.L().Info("[firebase] Send", zap.Any("msg", msg), zap.String("user_id", receiver))
|
|
@@ -144,6 +181,10 @@ func (f *FirebaseMessageServer) queueRun(ctx context.Context) {
|
|
|
zap.L().Error("[firebase] 发送错误", zap.Error(err), zap.String("user_id", receiver))
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+ zap.L().Info("[firebase] Ack", zap.String("user_id", receiver))
|
|
|
+ client.Send(ctx, &messaging.Message{})
|
|
|
+
|
|
|
zap.L().Info("[firebase] Successfully",
|
|
|
zap.Any("msg", msg),
|
|
|
zap.String("user_id", receiver),
|