android - Firebase Cloud functions to Android application notifications
问题描述
I need some hints for the following system:
- client app: Android
- backend: Firebase Cloud Funtions
- database: realtime database
One of the scenarios is as follows:
- All users are on android devices.
Users are sign-in via OAuth2 (google accounts).
- User1 creates an offer and the offer is 'waiting' in active offers list.
- Other users are notified for the change is active offers list..
- User2 requests the offer and the offer is 'requested'
- Other users are notified for the change is active offers list..
- User 1 is notified for the User2's request
User1 confirms/denies the User1's request:
a) confirms and the offer is 'completed' and removed from the active offers list.
b) denies and the offer is again 'waiting' in the active offers list.
- Other users are notified for the change is active offers list..
- User2 is notified for the User1's confirmation/denial
NOTE: System is specific and limited so the active offers list will be short enough and that's why currently I'm thinking about notifying users about list modifications and not about a single offer modification, but I'm open for advices.
==========================================
Currenty I've implemented the following:
User1 creates an offer and the offer is 'waiting':
- android app calls createOffer(...) firebase cloud function
- createOffer(...) adds a new offer to the offers list in realtime database.
- onWrite(...) realtime database trigger is fired on active offers list level
here I need the hint
User2 requests the offer and the offer is 'requested':
- android app calls requestOffer(...) firebase cloud function
- requestOffer(...) modifies the offer in realtime database.
- onWrite(...) realtime database trigger is fired on active offers list level
here I need the hint
here I need the hint
User1 confirms/denies the User1's request:
a) confirmation:
- android app calls confirnOfferRequest(...) firebase cloud function
- confirmOfferRequest(...) removes the offer from active offers list.
- onWrite(...) realtime database trigger is fired on active offers list level
b) denial:
- android app calls denyOfferRequest(...) firebase cloud function
- denyOfferRequest(...) modifies the offer in active offers list. onWrite(...) realtime database trigger is fired on active offers list level.
.
here I need the hint
here I need the hint
==================================
So I need hints about notifications to android application: steps 2, 4, 5, 7, 8
I guess I should use FCM, but:
Android tutorials I find are about the deprecated FirebaseInstanceId
I'm not sure what is my case:
- topic messages
- device groups
- upstream messages
I'm not sure how to notify android app by onWrite(...) realtime database trigger.
Signed-in users only should be notified about active offers list changes. if a user is singed-out should not be notified and when sign-in should get actual state (active offers list).
=============
@James Poag, Thank you for the hints. I'll try and give feedback.
I have some questions:
When I said "notification" I didn't mean 'real' notification in device's 'status bar'. At least current plan is the application to show the info inside itself - this I called "notification". It seems simpler.
- is there a reason to use onCreate() instead of onWrite() ?
Currently my plan is to perform same action on creating/updating an offer so I thought onWrite() would do the trick for both cases. Or not ?
thanks for transaction hint :)
I guess in my case I have "data" messages, not "real" notifications.
I have to read about the permissions. thanks.
Currenty I check for authorization this way:
if (!context.auth) {
return Promise.reject(Error('User is not authenticated.'));
}
解决方案
以下要点是您要使用Admin SDK 发送消息。
您可以创建主题,让用户订阅主题,然后向主题发送消息(在上面的同一链接中介绍),或者如果有一小部分用户,那么也许您只想向数据库中的每个人发送消息。
此外,不要依赖 Android 中的默认通知处理来处理您的 FCM 消息,请使用步骤 #2 中的链接手动将通知发布到用户的托盘。
User1 创建了一个报价并且报价正在“等待”:
- android 应用调用 createOffer(...) firebase 云函数
- createOffer(...) 在实时数据库中的报价列表中添加一个新报价。
- onWrite(...) 在活动商品列表级别触发实时数据库触发器考虑使用 onCreate() 而不是 onWrite()
使用 admin sdk 向您的应用用户发送通知。这将需要发送到组主题或遍历用户数据库并发送到用户的设备令牌。当您的用户使用设备登录时,从 FirebaseMessaging 获取他们的令牌并将其写入保存其个人资料的实时数据库。
User2 请求报价并且报价被“请求”:
- android 应用调用 requestOffer(...) firebase 云函数
- requestOffer(...) 在实时数据库中修改报价。在这里,您需要查看交易以防止多个用户获得对 Offer 的锁定
- onWrite(...) 实时数据库触发器在活动报价列表级别触发
向主题组发送
data
唯一通知。在您的 Android 应用程序中,处理数据负载,打开NotificationManager并将之前的通知更新为“已请求”。*在数据库中创建报价时,请务必包含所有者的设备令牌。这样,当报价状态更改为“已请求”时,您可以使用 admin sdk 向所有者发出通知。*
User1 确认/拒绝 User1 的请求:
a) 确认:
- android 应用调用 confirnOfferRequest(...) firebase 云函数
- confirmOfferRequest(...) 从活动报价列表中删除报价。
- onWrite(...) 实时数据库触发器在活动报价列表级别触发
b) 否认:
- android应用调用denyOfferRequest(...) firebase云函数
- denyOfferRequest(...) 修改活动报价列表中的报价。onWrite(...) 实时数据库触发器在活动报价列表级别触发。
.
** 与#4 相同,但如果接受,则使用通知管理器完全取消通知 **
当 User2 触发第 3 步时,他们应该将他们的设备令牌作为请求的一部分发送。这样,当请求被接受时,您的云功能将知道该通知谁。
当用户登录设备时,将他们的令牌发送到主题组。当他们注销时,从组中删除令牌(在实际注销之前)。
由于所有内容都通过您的云功能进行路由,因此您可以将数据库上的权限设置为.read : false, .write : false
,然后对于配置文件部分(如果需要),您可以添加一个特殊的.write : auth !== null && auth.user === uid
. 否则,Admin SDK 具有从您的数据库读取/写入的特殊权限。
此外,当有人调用您的 HTTPS 函数时检查授权,以确保他们已登录 Firebase。
推荐阅读
- multithreading - 我如何在 struct 中存储并在整个应用程序的不同函数中使用该值?
- c# - 用于反序列化的自定义转换器未触发或未命中 Web API 中的断点
- android - Jetpack Compose MutableSharedFlow 一次性事件
- mysql - 如何根据以下条件编写mysql
- excel - 如何以行号作为变量遍历特定范围?
- database - 我根据股票代码的Initial使用范围分区策略对数据进行分区,导致分区结果不均匀
- sed - Cloud Foundry:如何获取 VCAP_SERVICES 环境变量的内容?(而且只有这个变量!)
- php - 如何从 2 个查询的结果中创建一个数组?
- ios - Sqlite.swift 迁移版本错误。致命错误:“试试!” 表达式意外引发错误:文件不是数据库(代码:26)
- elasticsearch - 带有 micronaut-elasticsearch 插件的 Grails