简体   繁体   English

使用FCM向不同的用户发送推送通知

[英]Sending push notifications to different users with FCM

I'm working on a doorbell project in which I want to send Push Notification to user when a guest come and ring the doorbell, it should send a Push Notification to the user (ie owner of house). 我正在开发一个门铃项目,我想在客人来访并敲门铃时向用户发送“推送通知”,它应该向用户(即房主)发送“推送通知”。 I want to use Firebase Cloud Messaging for this in my android app. 我想在我的Android应用程序中为此使用Firebase Cloud Messaging

We are using RaspberryPi as the hardware controller. 我们正在使用RaspberryPi作为硬件控制器。 It will trigger notification to user using FCM on their android device when someone presses their doorbell.(which is not a point of concern.) 当有人按下门铃时,它将触发使用FCM在其android设备上向用户发出通知。

My confusion is: 我的困惑是:

According to this post on SO, FCM will send Push Notification to the predefined 'registration_ids' which we have mentioned in the app. 根据SO上的这篇帖子, FCM将把推送通知发送到我们在应用程序中提到的预定义“ registration_ids”。 But in my case, every user will have their different registration ids. 但是在我的情况下,每个用户都有不同的注册ID。 Also the number of users are not fixed. 同样,用户数量不是固定的。

It could be any number of users. 它可以是任意数量的用户。 Whoever uses the doorbell will have their own id. 谁使用门铃将有自己的身份证。

So it will be a possible scenario like: 因此,这将是一个可能的情况,例如:

  1. suppose I have not mentioned any registration id in app. 假设我在应用中未提及任何注册ID。 10 users have installed this doorbell at home and someone on the house no 1 presses doorbell button, the FCM will send push notification to all the 10 users. 10个用户在家中安装了此门铃,而没有房子1的某人按下门铃按钮,FCM将向所有10个用户发送推送通知。 Reason is, the registration id will be same for all the devices who have installed app from PlayStore. 原因是,所有从PlayStore安装了app的设备的注册ID都相同。

  2. suppose I have mentioned 3 registration id in app. 假设我在应用中提到了3个注册ID。 So 3 users will be getting the same Push Notification at the same time. 因此3个用户将同时获得相同的推送通知。

  3. And if i'm not wrong, if i put only 1 registration id, only 1 user will get notification even if 5 different house's doorbell are pressed, the same user will be notified 5 times. 如果我没记错,如果我只输入1个注册ID,即使按下5个不同的房屋的门铃,也只有1个用户会收到通知,同一用户将收到5次通知。

  4. If i declare a specific registration id to be notified in server side (ie RaspberryPi), I have to regularly add new registration ids as the number of users increases in my app and update it frequently. 如果我声明要在服务器端通知的特定注册ID(即RaspberryPi),则必须随着应用程序中用户数量的增加定期添加新的注册ID,并经常对其进行更新。

So how do I resolve this problem ? 那么我该如何解决这个问题呢?

I haven't started developing this app till now because I'm confused with the first step itself. 直到现在我还没有开始开发该应用程序,因为我对第一步本身感到困惑。

If I've gone wrong somewhere, please guide me as I'm new to Push Notifications or FCM. 如果我在某个地方出错了,请指导我,因为我是推送通知或FCM的新手。 I may not have understood concept of FCM, which is possible but I'm not sure. 我可能不了解FCM的概念,这是可能的,但我不确定。 If this solution is not possible using FCM, suggest me some other options to achieve this please. 如果使用FCM无法实现此解决方案,请提出一些其他选择来实现这一目标。

A Registration token corresponds to a single app instance (see my answer here ). 注册令牌对应于单个应用程序实例(请参阅此处的答案)。

In your post, it seems like you were confusing the registration id as something that is permanent in the app, which is not. 在您的帖子中,您似乎将注册ID混淆为应用程序中永久存在的某种东西,而事实并非如此。

Whenever a user installs your app, on the first initialization, it should generate it's own Registration token, where you'll have to save that token to your App Server, making sure that you associate it with the corresponding user details. 每当用户安装您的应用程序时,在第一次初始化时,它都应该生成自己的注册令牌,您必须在其中将其保存到App Server中,并确保将其与相应的用户详细信息相关联。

For your use-case, you could make use of Topic Messaging , where you simply subscribe the user to their corresponding doorbell topic, and every time that specific doorbell is triggered, you send a message to the corresponding topic. 对于您的用例,您可以利用Topic Messaging ,您只需在其中订阅用户相应的门铃主题,并且每次触发特定的门铃,就会向相应的主题发送一条消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM