android - 通过 Http 请求通过 Cloud Functions 进行 Firebase 云消息传递

标签 android firebase google-cloud-platform google-cloud-firestore firebase-cloud-messaging

尝试使用 Firebase 云消息传递为我的 Android 应用程序实现消息传递过程,我遇到了一些限制,因为我没有任何服务器,也不打算拥有任何类型的服务器。

我打算做什么: - 使用 okhttp 创建所有需要的 http 请求表单。 - 将所需的http请求发送到我的云功能 - 这些函数将从 firestore 获取需要的数据并响应请求,同时实际向主题发送云消息。

这是我想出的实现我自己的 REST API 的唯一方法,因为我无法在 Android 客户端应用程序中使用 Admin SDK,并且没有任何处理服务器。

有没有类似的示例、教程?欢迎任何建议。

最佳答案

您确实不需要有服务器来实现您的功能需求。正如您在问题中提到的,您可以很好地使用云功能(一种无服务解决方案)。

当您想要发送 Firebase 云消息通知时,您可以:

  1. 将通知内容写入新的 Firestore 文档(写入特定文档集合)。与实现 HTTP 云函数来发送数据相比,这里的优点是您可以保留请求的历史记录,而且由于您将使用适用于 Firestore 的 Android SDK,因此编码起来会更容易一些。
  2. 设置一个云函数,当在此集合中创建新文档时触发该函数并发送通知。

有一个官方示例云函数可以做到这一点,请参阅 https://github.com/firebase/functions-samples/tree/Node-8/fcm-notifications 。该函数的代码位于 https://github.com/firebase/functions-samples/blob/Node-8/fcm-notifications/functions/index.js .

在您的情况下,您将按如下方式触发云功能:

exports.sendNotification = functions.database.ref('/notificationsRequests/{requestUid}')
.onCreate(async (snap, context) => {})

参见https://firebase.google.com/docs/functions/firestore-events


请注意,您会在 SO 上找到很多涵盖此主题的问题和答案:https://stackoverflow.com/search?q=firestore+send+notifications

关于android - 通过 Http 请求通过 Cloud Functions 进行 Firebase 云消息传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52791174/

相关文章:

rest - 本地依赖的 GCF 部署

kubernetes - 云函数-连接Kubernetes容器

android - Admob child 定向设置

android - Xamarin Android 应用程序图标具有启动器 Activity 的名称

android - 您不能在尚未附加的 View 或 getActivity() 返回 null 的 fragment 上开始加载

python - Google SDK 实用程序中的 SSL 证书错误

android - ExoPlayer 不播放 Kitkat 中原始文件夹中的视频

java - 在 android 中设置时日期不正确

java - 如何增加 Firebase 查询中 "limitToFirst"的限制?

python - 引用 Python 文件本地的文件