管理 FCM 设备组

15

我正在尝试使用 REST API 从应用服务器管理 FCM 设备组。

据我所知,以下是最新的文档:https://firebase.google.com/docs/cloud-messaging/android/device-group#managing_device_groups

这是我已经可以做到的:

  • 使用一些设备令牌创建一个新的设备组
  • 将设备令牌添加到现有的设备组中

但这是我无法找出如何做到的地方,因为文档中没有提到:

  • 查询是否已经存在一个设备组,基于它的notification_key_name

    解决方法1:如果我尝试使用已经存在的notification_key_name创建一个新组,则会收到告诉我该组已存在的错误信息,但这似乎是一种非常不可靠的方法。

    解决方法2:在其他地方自行存储这些信息。

  • 找出哪些设备令牌(registration_id)属于一个设备组。

    解决方法:与上述相同,自己在其他地方存储这些信息。

  • 从设备组中删除设备令牌(registration_id)。

    解决方法:无。

  • 删除设备组。

    解决方法:无。

谢谢!

2个回答

15
查询设备组是否已存在,基于其notification_key_name。你的第二个解决方案是可行的。你应该将它存储在你的应用服务器上,与你存储注册令牌的地方相同。
查找哪些设备令牌(registration_id)属于设备组。
与上述解决方法相同。您需要在应用服务器上管理这些详细信息。开发人员有责任管理这些详细信息。如果注册设备被删除,匹配操作,您也必须从应用服务器中删除它。
  • 从设备组中移除设备令牌(registration_id)。

我不确定您需要什么。文档详细介绍了如何从设备组中删除注册令牌的信息:

Adding and removing devices from a device group

To add or remove devices from an existing group, send a POST request with the operation parameter set to add or remove, and provide the registration tokens for addition or removal.

Note: If you remove all existing registration tokens from a device group, FCM deletes the device group.

HTTP POST request

For example, to add a device with the registration ID 51 to appUser-Chris, you would send this request:

{
   "operation": "add",
   "notification_key_name": "appUser-Chris",
   "notification_key": "APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ",
   "registration_ids": ["51"]
}

Response format

A successful request to either add or remove a device returns a notification_key like the following:

{
   "notification_key": "APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ"
}

Note: notification_key_name is not required for adding/removing registration tokens, but including it protects you against accidentally using the incorrect notification_key.


  • 删除设备组。

从上面文档的注释中可以看到:

注意: 如果您从设备组中删除所有现有的注册令牌,FCM 将删除该设备组。


1
类似(可能有帮助的)帖子在这里:[https://dev59.com/AlgQ5IYBdhLWcg3wxGzs]。 - AL.
谢谢AL!非常有帮助。我简直不敢相信我错过了文档中关于删除设备和设备组的部分。这就是我只是匆匆浏览而没有仔细阅读的后果,很抱歉! - Josep Sayol
在这部分中,“发送一个操作参数设置为添加或删除的POST请求”,您是否介意包括添加操作的结果端点? - moondaisy
1
@moondaisy 你好。管理设备组所使用的端点在任何操作中基本相同:https://android.googleapis.com/gcm/notification - AL.

1
要在第一个问题中添加另一个选项,不确定这是否是在接受的答案之后由FCM添加的,但在Android(和IOS)文档的管理设备组部分中,有一部分关于使用GET请求检索通知密钥。保留html标签。

https://fcm.googleapis.com/fcm/notification?notification_key_name=appUser-Chris

如果设备组不存在,则返回以下内容:{ "error": "notification_key not found" },否则返回通知密钥。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接