Browse Source

修改 fcm 无通知声音的问题

luoyangwei 1 year ago
parent
commit
4121fd2659
2 changed files with 28 additions and 31 deletions
  1. 12 0
      server/fcm.go
  2. 16 31
      server/fcm_test.go

+ 12 - 0
server/fcm.go

@@ -146,6 +146,18 @@ func (f *FirebaseMessageServer) queueRun(ctx context.Context) {
 			Data: map[string]string{
 				"message": queue.Data,
 			},
+			Android: &messaging.AndroidConfig{
+				Notification: &messaging.AndroidNotification{
+					Sound: "default",
+				},
+			},
+			APNS: &messaging.APNSConfig{
+				Payload: &messaging.APNSPayload{
+					Aps: &messaging.Aps{
+						Sound: "default",
+					},
+				},
+			},
 			Notification: &messaging.Notification{
 				Title: queue.Title,
 				Body:  queue.Body,

+ 16 - 31
server/fcm_test.go

@@ -14,52 +14,37 @@ func TestFirebaseMessageServer_Send(t *testing.T) {
 	ctx := context.Background()
 	conf := &firebase.Config{ServiceAccountID: firebaseServiceAccountID}
 	app, err := firebase.NewApp(ctx, conf,
-		// option.WithHTTPClient(&http.Client{Timeout: 5 * time.Second}),
 		option.WithCredentialsJSON([]byte(firebaseCredentials)))
 	if err != nil {
 		log.Fatalln(err)
 	}
 
-	token := "foU7QZ1toU9zkP-QN_6Qlz:APA91bGNJ9FVZQAVXzrWAyVHdRqgOO9B15dNJqGY7xuhzefASlQp8itrpa4qfbEX4aYRLiRVx8YRu35Mj0CFetkdSBrZk2qz8gv6MHeoMKI0ivFdGGjS76wKFy4m24CDNS9bswUrsoyH"
+	token := "cl7mDFUw5URmvG9Jio2p7L:APA91bH4GIRM1IJNDn7_tYw7q4ADVdzQKpnaPNqxXw6PZE-H9H82sGkF74-KSz7moydB6ykgHucrXNjsSopmyFCi3M4S-SVWZA_fGJa6Q0WO6V_CY_gtDhi2WHGobM84Pm0aZ0AHb2XB"
 	clt, err := app.Messaging(ctx)
 	if err != nil {
 		panic(err)
 	}
-	// 	data := fmt.Sprintf(`
-	// 	message: {
-	// 		notification: {
-	// 			"title": "XXX",
-	// 			"body": "XXX"
-	// 		},
-	// 		data: { "key": "value" },
-	// 		token: %s,
-	// 		android: {
-	// 			"priority": "high"
-	// 		},
-	// 		apns: {
-	// 			"payload": {
-	// 				"aps": {
-	// 					"content-available": 1
-	// 				}
-	// 			},
-	// 		}
-	// 	}
-	//  `, token)
-	// aps := map[string]interface{}{
-	// 	"alert": map[string]interface{}{
-	// 		"title": "title",
-	// 		"body":  "body",
-	// 	},
-	// }
-	// apsBuf, _ := json.Marshal(aps)
+
 	msg := &messaging.Message{
 		Token: token,
 		Data: map[string]string{
 			"message": "hello word",
 		},
+		Android: &messaging.AndroidConfig{
+			Notification: &messaging.AndroidNotification{
+				Sound: "default",
+			},
+		},
+		APNS: &messaging.APNSConfig{
+			Payload: &messaging.APNSPayload{
+				Aps: &messaging.Aps{
+					Sound: "default",
+				},
+			},
+		},
 		Notification: &messaging.Notification{
-			// Title: queue.Title,
-			// Body:  queue.Body,
+			Title: "测试消息",
+			Body:  "测试消息内容,无需关注",
 		},
 	}