Firebase Cloud可用于Android应用程序通知 [英] Firebase Cloud functions to Android application notifications
问题描述
对于以下系统,我需要一些提示:
I need some hints for the following system:
- 客户端应用:Android
- 后端:Firebase云功能
- 数据库:实时数据库
其中一种情况如下:
- 所有用户都在android设备上.
-
用户通过OAuth2(Google帐户)登录.
- All users are on android devices.
Users are sign-in via OAuth2 (google accounts).
- User1创建一个要约,并且要约在活动要约列表中处于等待中".
- 通知其他用户该更改为活动商品列表.
- User2请求要约,并且要约被请求"
- 通知其他用户该更改为活动商品列表.
- 通知用户1用户2的请求
-
User1确认/拒绝User1的请求:
- 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)确认并且要约已完成"并从有效要约列表中删除.
a) confirms and the offer is 'completed' and removed from the active offers list.
b)拒绝,并且要约在活动要约列表中再次处于等待"状态.
b) denies and the offer is again 'waiting' in the active offers list.
注意:系统是特定的且受限制的,因此有效的要约列表将很短,这就是为什么我目前正在考虑通知用户有关列表修改的信息,而不是关于单个要约修改的通知,但我愿意接受建议.
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创建一个要约,该要约在等待中":
User1 creates an offer and the offer is 'waiting':
- android应用调用createOffer(...)Firebase云功能
- createOffer(...)在实时数据库的要约列表中添加新要约. 在活动商品列表级别上触发
- onWrite(...)实时数据库触发器
- 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
这里我需要提示
User2请求要约,并且要约被请求":
User2 requests the offer and the offer is 'requested':
- android应用调用requestOffer(...)Firebase云功能
- requestOffer(...)修改实时数据库中的报价. 在活动商品列表级别上触发
- onWrite(...)实时数据库触发器
- 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
这里我需要提示
这里我需要提示
User1确认/拒绝User1的请求:
User1 confirms/denies the User1's request:
a)确认:
- android应用调用confirnOfferRequest(...)firebase云功能
- confirmOfferRequest(...)从活动要约列表中删除要约. 在活动商品列表级别上触发
- onWrite(...)实时数据库触发器
- 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)拒绝:
- android应用调用denyOfferRequest(...)Firebase云功能
- denyOfferRequest(...)修改活动商品列表中的商品.在活动商品列表级别上触发onWrite(...)实时数据库触发器.
.
-
这里我需要提示
这里我需要提示
=================================
==================================
所以我需要有关到android应用程序的通知的提示:步骤2、4、5、7、8
我想我应该使用FCM,但是:
I guess I should use FCM, but:
- 我找到的
-
Android教程是关于已弃用的FirebaseInstanceId
Android tutorials I find are about the deprecated FirebaseInstanceId
我不确定我的情况是什么
I'm not sure what is my case:
- 主题消息
- 设备组
- 上游消息
我不确定如何通过onWrite(...)实时数据库触发器来通知android应用.
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 ,谢谢您的提示.我会尽力提供反馈.
@James Poag, Thank you for the hints. I'll try and give feedback.
我有一些问题:
当我说通知"时,并不是在设备的状态栏"中表示真实"通知.至少当前的计划是在其内部显示信息的应用程序-我称之为通知".似乎更简单.
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.
- 是否有理由使用onCreate()而不是onWrite()?
当前,我的计划是在创建/更新商品时执行相同的操作,因此我认为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.'));
}
推荐答案
The gist of the following is that you want to use the Admin SDK to send messages.
您可以创建主题,让用户订阅主题,然后向主题发送消息(在上面的同一链接中找到),或者,如果用户群很小,那么您可能只想向您的所有人发送消息数据库.
You could create Topics, let users subscribe to Topics and then fire messages to topics (covered in the same link above), or if there is a small group of users, then perhaps you just want to send messages to everyone in your DB.
此外,不要依靠Android中默认的FCM消息处理通知,而是使用步骤2中的链接将通知手动发布到用户的任务栏中.
Also, don't rely on the default Notification Processing in Android for your FCM messages, manually Post the notification to the user's tray using the link in step #2.
-
User1创建一个要约,该要约在等待中":
User1 creates an offer and the offer is 'waiting':
- android应用调用createOffer(...)Firebase云功能
- createOffer(...)在实时数据库的要约列表中添加新要约. 在活动商品列表级别上触发
- onWrite(...)实时数据库触发器 考虑使用onCreate()代替onWrite()
- 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 Consider using onCreate() instead of onWrite()
使用admin sdk向您的应用程序用户发送通知.这将需要发送到组主题或遍历用户数据库并发送到用户的设备令牌.当您的用户使用设备登录时,
Use the admin sdk to send notifications to your app users. This will entail either sending to a group topic or iterating through the user database and sending to the user's device tokens. When your user logs in using a device, grab their token from FirebaseMessaging and write it to the Realtime database that holds their profile.
User2请求要约,并且要约被请求":
User2 requests the offer and the offer is 'requested':
- android应用调用requestOffer(...)Firebase云功能
- requestOffer(...)修改实时数据库中的报价. 在这里,您需要研究交易防止多个用户获得对商品的锁定 在活动商品列表级别上触发
- onWrite(...)实时数据库触发器
- android app calls requestOffer(...) firebase cloud function
- requestOffer(...) modifies the offer in realtime database. Here you need to look into transactions to prevent multiple users from acquiring a lock on the Offer
- onWrite(...) realtime database trigger is fired on active offers list level
仅向主题组发送 data
通知.在您的Android应用中,处理数据有效载荷,打开
Send a data
only notification to the Topic group. Inside your Android app, process the data payload, open the NotificationManager and update the previous notification to 'requested'.
**在数据库中创建报价后,请确保包括所有者的设备令牌.这样,当要约状态更改为请求"时,您可以使用admin sdk向所有者发送通知.*
User1确认/拒绝User1的请求:
User1 confirms/denies the User1's request:
a)确认:
- android应用调用confirnOfferRequest(...)firebase云功能
- confirmOfferRequest(...)从活动要约列表中删除要约. 在活动商品列表级别上触发
- onWrite(...)实时数据库触发器
- 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)拒绝:
- android应用调用denyOfferRequest(...)Firebase云功能
- denyOfferRequest(...)修改活动商品列表中的商品.在活动商品列表级别上触发onWrite(...)实时数据库触发器.
.
-
**与#4相同,但如果被接受,则使用通知管理器完全取消通知**
** Same as #4, but if accepted, then use the Notification Manager to Cancel the notification completely **
当User2触发第3步时,他们应发送设备令牌作为请求的一部分.这样,当请求被接受时,您的云功能将知道要通知谁.
当用户登录设备时,将其令牌发送到主题组.他们注销后,从组中删除令牌(在实际注销之前).
When the user logs into the device, send their token to the Topic group. When they log out, remove the token from the group (before actually logging out).
由于所有内容都是通过云功能路由的,因此您可以将数据库的权限设置为 .read:false,.write:false
,然后对于配置文件部分(如果需要)可以添加一个特殊的 .write:auth!== null&&auth.user === uid
.否则,Admin SDK拥有从您的数据库读取/写入的特殊权限.
Since everything is routed through your cloud functions, you could set the permissions on your Database to .read : false, .write : false
, then for the profile section (if needed) you can add a special .write : auth !== null && auth.user === uid
. Otherwise, the Admin SDK has special permissions to read/write from your database.
另外,请检查一下 查看全文