Browse Source

手表对APP发起视频通话,FCM推送时,如果是安卓,对msg中的Topic赋值video_call

tea.yu 1 tháng trước cách đây
mục cha
commit
6c0f391cda
2 tập tin đã thay đổi với 58 bổ sung7 xóa
  1. 27 0
      server/fcm.go
  2. 31 7
      server/fcm_test.go

+ 27 - 0
server/fcm.go

@@ -2,6 +2,8 @@ package server
 
 import (
 	"context"
+	"encoding/json"
+	"fmt"
 	"log"
 	"time"
 
@@ -208,6 +210,17 @@ func (f *FirebaseMessageServer) queueRun(ctx context.Context) {
 		switch firebaseToken.System {
 		case "android":
 			client = f.androidClient
+			// 将JSON字符串转换为NatsPubMessage对象
+			var message NatsPubMessage[NatsPubVideoCallMessage]
+			err := json.Unmarshal([]byte(queue.Data), &message)
+			if err != nil {
+				fmt.Println("Error unmarshaling JSON:", err)
+			} else {
+				zap.L().Info("[firebase] Unmarshal queue.Data: ", zap.Any("message:", message))
+				if message.Type == MessageTypeVideoCall {
+					msg.Topic = "video_call"
+				}
+			}
 		default:
 			client = f.iosClient
 		}
@@ -235,3 +248,17 @@ func (f *FirebaseMessageServer) queueRun(ctx context.Context) {
 func Now(n time.Time) *time.Time {
 	return &n
 }
+
+type NatsPubVideoCallMessage struct {
+	AccountId string `json:"accountId"`
+	Dial      int8   `json:"dial"` // -1: 挂断 1: 接通
+
+	Receiver string `json:"receiver"`
+	Sender   string `json:"sender"`
+}
+
+type NatsPubMessage[T NatsPubVideoCallMessage] struct {
+	RequestId string `json:"requestId"`
+	Type      int8   `json:"type"`
+	Content   *T     `json:"content"`
+}

+ 31 - 7
server/fcm_test.go

@@ -12,27 +12,51 @@ import (
 
 func TestFirebaseMessageServer_Send(t *testing.T) {
 	ctx := context.Background()
-	conf := &firebase.Config{ServiceAccountID: iosFirebaseServiceAccountID}
+	conf := &firebase.Config{ServiceAccountID: androidFirebaseServiceAccountID}
 	app, err := firebase.NewApp(ctx, conf,
-		option.WithCredentialsJSON([]byte(iosFirebaseCredentials)))
+		option.WithCredentialsJSON([]byte(androidFirebaseCredentials)))
 	if err != nil {
 		log.Fatalln(err)
 	}
-
+	//token := "fDfRE2raSkSfroIw47qSEV:APA91bF4LORpS8ZWo-i8i1DQ8SZ8C-BEdFWBdGQGDW-PwzQUivoNf75_NpTrFNLFlAqdHd4mdauaI9aZWhFs4Pou2iySmuI6Ct0sHs5Bc9LrnGMK9f5YEBA"
 	token := "cl7mDFUw5URmvG9Jio2p7L:APA91bH4GIRM1IJNDn7_tYw7q4ADVdzQKpnaPNqxXw6PZE-H9H82sGkF74-KSz7moydB6ykgHucrXNjsSopmyFCi3M4S-SVWZA_fGJa6Q0WO6V_CY_gtDhi2WHGobM84Pm0aZ0AHb2XB"
 	clt, err := app.Messaging(ctx)
 	if err != nil {
 		panic(err)
 	}
+	jsonStr := "hello word"
+	title := "测试消息"
+	body := "测试消息内容,无需关注"
 
+	//title:= "Call Notification"
+	//body:=  "click to view"
+	//videoCallMessage := &NatsPubVideoCallMessage{
+	//	AccountId: "954d38fc-f4a4-445f-88e3-7be83a51a3c8",
+	//	Dial:      1,
+	//	Receiver:  "c2686cac-8ec8-4b26-8e87-50b7d63a7d36",
+	//	Sender:    "954d38fc-f4a4-445f-88e3-7be83a51a3c8",
+	//}
+	//
+	//message := NatsPubMessage[NatsPubVideoCallMessage]{
+	//	RequestId: "41a83fe2-6281-4f72-bb80-067fb5e19b1c",
+	//	Type:      MessageTypeVideoCall,
+	//	Content:   videoCallMessage,
+	//}
+	//jsonBytes, err := json.Marshal(message)
+	//if err != nil {
+	//	fmt.Println("Error marshaling JSON:", err)
+	//	return
+	//}
+	//jsonStr := string(jsonBytes)
 	msg := &messaging.Message{
 		Token: token,
 		Data: map[string]string{
-			"message": "hello word",
+			"message": jsonStr,
 		},
 		Android: &messaging.AndroidConfig{
 			Notification: &messaging.AndroidNotification{
-				Sound: "default",
+				Sound:    "default",
+				Priority: messaging.PriorityHigh,
 			},
 		},
 		APNS: &messaging.APNSConfig{
@@ -43,8 +67,8 @@ func TestFirebaseMessageServer_Send(t *testing.T) {
 			},
 		},
 		Notification: &messaging.Notification{
-			Title: "测试消息",
-			Body:  "测试消息内容,无需关注",
+			Title: title,
+			Body:  body,
 		},
 	}