简体   繁体   English

Android推送通知教程:如何将通知发送给“关注者”?

[英]Android push notifications tutorial: how do I send notifications to “followers”?

I have written a test app where users can publish some data (location, photos, tags etc.) to their "feed" (ie write some data to an online database which is currently Firebase). 我编写了一个测试应用程序,用户可以在其中将一些数据(位置,照片,标签等)发布到他们的“提要”(即,将一些数据写到当前为Firebase的在线数据库中)。 If other users are "following" this user (or a relevant tag), how do I send notifications to the followers that new data is available? 如果其他用户在“关注”该用户(或相关标签),如何向关注者发送新数据可用的通知?

I have looked at Google Cloud Messaging and the replacement Firebase Cloud Messaging, but I find their documentation too technical for me (seems aimed towards IT professionals). 我看过Google Cloud Messaging和替代的Firebase Cloud Messaging,但是我发现他们的文档对我来说太技术性了(似乎针对IT专业人员)。 I can program but don't really understand "servers", "protocols" etc. It seems from GCM/FCM docs I need a "server" before I can use their services. 我可以编程,但不太了解“服务器”,“协议”等。从GCM / FCM文档看来,在使用它们的服务之前,我需要一个“服务器”。 This server will 该服务器将

  • Recognise when a database event happens. 识别何时发生数据库事件。

  • Run some logic to ascertain whether notifications need to be sent out (and to whom) 运行一些逻辑以确定是否需要发送通知(以及向谁发送)

  • Inform GCM or FCM to do so. 通知GCM或FCM这样做。

Is this correct? 这个对吗? If so I don't know how to get a server, write one, plug one in or whatever it is I'm supposed to do with one. 如果是这样,我不知道如何获得服务器,请编写服务器,插入服务器或执行我应该使用的任何服务器。 Any advise or simple tutorials that anyone can give a neophyte developer is greatly appreciated! 任何人都可以给新手开发人员的建议或简单教程,将不胜感激!

Thanks, Riz 谢谢,里兹

It seems you're trying to implement GCM Topic Messaging where your app server sends messages to mulitple devices that are subscribed to a particular "topic". 似乎您正在尝试实现GCM主题消息传递 ,其中您的应用服务器将消息发送到订阅了特定“主题”的多种设备。 Though this particular feature doesn't seem to be that feasible, it is doable. 尽管此特定功能似乎并不可行,但它是可行的。

Before your followers can get notified, they must first be subscribed to a topic. 必须先订阅他们的主题,然后您的关注者才能得到通知。

Here's how to subscirbe in Android: 在Android中订阅的方法如下:

private void subscribeTopics(String token) throws IOException {
    GcmPubSub pubSub = GcmPubSub.getInstance(this);
    for (String topic : TOPICS) {
        pubSub.subscribe(token, "/topics/" + topic, null);
    }
}

Try this actual demos to get a grip on this: 试试这个实际的演示,以了解这一点:

GCM Playground - A reference implementation of a GCM Application Server in the form of a playground that developers can use to test the Google Cloud Messaging Service. GCM游乐场 -GCM应用服务器的参考实施,以游乐场的形式提供,开发人员可以用来测试Google Cloud Messaging Service。

Since Google now encourages users to use FCM, try the FCM Android Quickstart . 由于Google现在鼓励用户使用FCM,因此请尝试FCM Android快速入门 You can also read through this SO post for additional insight. 您也可以通读这篇SO帖子以获得更多见解。

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

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