如何在google-services.json中放置多个project_number / sender id [英] How to put multiple project_number/sender id in google-services.json

查看:565
本文介绍了如何在google-services.json中放置多个project_number / sender id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的android应用程序中添加多个发件人ID。

https://开发者.google.com / cloud-messaging / concept-options


GCM允许多方发送消息到相同的客户端应用程序。例如,假设客户端应用程序是一个包含多个贡献者的文章聚合器,并且他们中的每个应该能够在发布新文章时发送消息。此消息可能包含一个URL,以便客户端应用程序可以下载文章。 GCM让您不必将所有发送活动集中到一个位置,而是让每个发送者都能发送自己的信息。


使用 google-services.json 配置文件是如何实现的?

解决方案

<更新:指的是官方和推荐的方式,而不是和非官方的方法来防止/避免未知的问题。从我的答案 here

实际上有一部分关于此的文档主题:
$ b


接收来自多个发件人的邮件

FCM允许多方发送消息到相同的客户端应用程序。例如,假设客户端应用程序是一个包含多个贡献者的文章聚合器,并且他们每个应该能够在发布新文章时发送消息。此消息可能包含一个URL,以便客户端应用程序可以下载文章。 FCM让您不必将所有发送活动集中在一个位置,而是让每个发送者都能发送自己的信息。为了实现这一点,请确保每个发件人都会生成自己的发件人ID 。有关如何获取FCM发件人ID的信息,请参阅您的平台的客户端文档。当请求注册时,客户端应用程序多次获取令牌,每次在受众字段中使用不同的发件人ID。

最后,将注册令牌与相应的应用程序服务器(完成FCM注册客户端/服务器握手),他们将能够使用他们自己的认证密钥发送消息到客户端应用程序。



请注意限制100个发件人。

我认为这里令人困惑但重要的部分是:


请求注册时,客户端应用程序多次获取令牌,每次在受众字段中使用不同的发件人ID。


换句话说,您必须致电 getToken() 传递发件人ID并简单地FCM(例如, getToken(2xxxxx3344,FCM))作为参数。您必须确保您为每个发件人(项目)调用您需要的。



另外,请注意 getToken() docs:

lockquote

这是一个阻塞函数,所以不要在

一些额外的知识:


  • 如果它像默认的那样失败,它不会自动重试。$​​ b $ b
  • 它在失败时返回一个IOException。

I want to be able to add more than one sender id in my android app.

From https://developers.google.com/cloud-messaging/concept-options

GCM allows multiple parties to send messages to the same client app. For example, suppose the client app is an articles aggregator with multiple contributors, and each of them should be able to send a message when they publish a new article. This message might contain a URL so that the client app can download the article. Instead of having to centralize all sending activity in one location, GCM gives you the ability to let each of these contributors send its own messages.

How is this achieved using google-services.json configuration file?

解决方案

UPDATE: Going to refer to the official and recommended way in doing this instead of the hacky and unofficial approach to prevent/avoid unknown problems. From my answer here.

There is actually a part in the documentation about this topic:

Receiving messages from multiple senders

FCM allows multiple parties to send messages to the same client app. For example, suppose the client app is an article aggregator with multiple contributors, and each of them should be able to send a message when they publish a new article. This message might contain a URL so that the client app can download the article. Instead of having to centralize all sending activity in one location, FCM gives you the ability to let each of these contributors send its own messages.

To make this possible, make sure each sender generates its own sender ID. See the client documentation for your platform for information on on how to obtain the FCM sender ID. When requesting registration, the client app fetches the token multiple times, each time with a different sender ID in audience field.

Finally, share the registration token with the corresponding app servers (to complete the FCM registration client/server handshake), and they'll be able to send messages to the client app using their own authentication keys.

Note that there is limit of 100 multiple senders.

I think the confusing but important part here is:

When requesting registration, the client app fetches the token multiple times, each time with a different sender ID in audience field.

In other terms, you'll have to call getToken() passing the Sender ID and simply "FCM" (e.g. getToken("2xxxxx3344", "FCM")) as the parameters. You'll have to make sure that you call this for each sender (project) that you need.

Also, note from the getToken() docs:

This is a blocking function so do not call it on the main thread.

Some additional good-to-knows:

  • It does not auto retry if it fails like the default one.
  • It returns an IOException when it fails.

这篇关于如何在google-services.json中放置多个project_number / sender id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆