Javascript Firebase FCM 错误:“无效注册”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44035518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:18:05  来源:igfitidea点击:

Firebase FCM error: 'InvalidRegistration'

javascriptnode.jsfirebasefirebase-cloud-messaging

提问by anho

I am currently trying to send a PushNotificationto a Device Groupusing FCMwith the help of Firebase Cloud Functionsbut once the notification is sent, it returns with code 200but with failure :

我目前正在尝试在的帮助下将 a 发送PushNotificationDevice Groupusing FCMFirebase Cloud Functions但是一旦发送通知,它就会返回代码200但失败:

SUCCESS response= {
multicast_id: 8834986220110966000,
success: 0,
failure: 1,
canonical_ids: 0,
results: [ { error: 'InvalidRegistration' } ] 
}

Here is the code I am using to send this notification... what am I missing?

这是我用来发送此通知的代码......我错过了什么?

const options = {
    method: 'POST',
    uri: 'https://fcm.googleapis.com/fcm/send',
    headers: {
       'Authorization': 'key=' + serverKey,
    },
    body: {
       to: groupId,
       data: {
        subject: message
       },
       notification: {
         title: title,
         body: body,
         badge: 1,
        },
       content_available: true
    },
    json: true
};

return rqstProm(options)
    .then((parsedBody) => {
        console.log('SUCCESS response=', parsedBody);
    })
    .catch((err) => {
        console.log('FAILED err=', err);
    });

Where JSON values title, body, subject, messageare String

其中 JSON 值title, body, subject,messageString

采纳答案by Bob Snyder

There is an easier way to send a message to a device group from a Cloud Function. Use admin.messaging().sendToDeviceGroup(). Sample code and instructions are in this guide.

有一种更简单的方法可以从 Cloud Function 向设备组发送消息。使用admin.messaging().sendToDeviceGroup()本指南中提供了示例代码和说明。

I think your current method is failing because there is something wrong with the group notification key provided in groupId. It should be the string key value that was returned when you created the device group. The error codes are listed in this table. For 200/InvalidRegistration it says:

我认为您当前的方法失败了,因为groupId. 它应该是创建设备组时返回的字符串键值。此表中列出了错误代码。对于 200/InvalidRegistration,它说:

Check the format of the registration token you pass to the server. Make sure it matches the registration token the client app receives from registering with Firebase Notifications. Do not truncate or add additional characters.

检查您传递给服务器的注册令牌的格式。确保它与客户端应用从注册 Firebase 通知时收到的注册令牌匹配。不要截断或添加其他字符。

回答by Sunil Garg

In my case, I was sending notifications to topic ("topics/my-topic"). I was missing prepending /in the starting of topic so I was getting the same issue. SO topic should be /topics/my-topic.

就我而言,我正在向主题 (" topics/my-topic")发送通知。我/在主题开始时缺少前置,所以我遇到了同样的问题。SO 主题应该是/topics/my-topic.

May be this helps!!

可能这有帮助!!

回答by user3824246

A registration token is tied to a certain group of senders. When a client app registers for FCM, it must specify which senders are allowed to send messages. You should use one of those sender IDs when sending messages to the client app.

注册令牌与特定的发件人组相关联。当客户端应用程序注册 FCM 时,它必须指定允许哪些发件人发送消息。向客户端应用程序发送消息时,您应该使用这些发件人 ID 之一。

Al you need to do is add a http header 'project_id' with your sender id.

您需要做的就是添加一个带有您的发件人 ID 的 http 标头“project_id”。