【发布时间】:2018-03-15 06:41:54
【问题描述】:
我有一个包含两个组件的应用程序:一个使用 FCM Go Adming SDK 发送推送通知的服务器;以及一个使用 FCM Web SDK 接收生成注册令牌和接收推送通知的 Web 客户端。
但是,当我尝试从服务器端发送消息时;尽管两个 SDK 都在同一个 Firebase 项目中注册,但它给了我以下错误:
http error status: 403; reason: sender id does not match regisration token; code: mismatched-credential
服务器组件详情
我将 Firebase Admin SDK 添加到我的服务器并使用服务帐户凭据对其进行了初始化。用于初始化的serviceAccountKey.json 如下所示:
{
"type": "service_account",
"project_id": "myproject",
"private_key_id": "<PRIVATE_KEY_ID>",
"private_key": "<PRIVATE_KEY>",
"client_email": "firebase-adminsdk-yg8ah@myproject.iam.gserviceaccount.com",
"client_id": "<CLIENT_ID>",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/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-yg8ah%40myproject.iam.gserviceaccount.com"
}
用于发送通知的服务器端代码如下所示:
app, err := firebase.NewApp(context.Background(), nil, option.WithCredentialsFile("./resources/serviceAccountKey.json"))
if err != nil {
log.Fatalf("Failed to initialize the notifications server: %v\n", err)
}
// Obtain a messaging.Client from the App.
ctx := context.Background()
client, err := app.Messaging(ctx)
// The message payload.
message := &messaging.Message{
Data: map[string]string{},
Notification: &messaging.Notification{Title: "Title", Body: "Body"},
Token: "<Registration_Token>",
}
// Send a message to the device corresponding to the provided registration token.
response, err := client.Send(ctx, message)
if err != nil {
log.Fatalf("Failed to send the push notification: %v\n", err)
}
客户端组件详情
对于网络客户端,我创建了Web Push certificates VAPID 密钥对,在将messaging.usePublicVapidKey('<YOUR_PUBLIC_VAPID_KEY_HERE>'); 行更改为messaging.usePublicVapidKey('<MY_PUBLIC_VAPID_KEY_HERE>'); 后使用FCM Web SDK sample code for messaging
此外,网络客户端应用托管在 Firebase 托管中。
【问题讨论】:
标签: firebase go push-notification firebase-cloud-messaging firebase-admin