简体   繁体   English

Firebase Cloud可用于Android应用程序通知

[英]Firebase Cloud functions to Android application notifications

I need some hints for the following system: 我需要一些有关以下系统的提示:

  • client app: Android 客户端应用:Android
  • backend: Firebase Cloud Funtions 后端:Firebase云功能
  • database: realtime database 数据库:实时数据库

One of the scenarios is as follows: 方案之一如下:

  • All users are on android devices. 所有用户都在android设备上。
  • Users are sign-in via OAuth2 (google accounts). 用户通过OAuth2(Google帐户)登录。

    1. User1 creates an offer and the offer is 'waiting' in active offers list. User1创建一个要约,并且要约在活动要约列表中“正在等待”。
    2. Other users are notified for the change is active offers list.. 通知其他用户该更改为活动商品列表。
    3. User2 requests the offer and the offer is 'requested' User2请求要约,并且要约被“请求”
    4. Other users are notified for the change is active offers list.. 通知其他用户该更改为活动商品列表。
    5. User 1 is notified for the User2's request 通知用户1用户2的请求
    6. User1 confirms/denies the User1's request: 用户1确认/拒绝用户1的请求:

      a) confirms and the offer is 'completed' and removed from the active offers list. a)确认并且要约已“完成”并从有效要约列表中删除。

      b) denies and the offer is again 'waiting' in the active offers list. b)拒绝,并且要约在活动要约列表中再次“等待”。

    7. Other users are notified for the change is active offers list.. 通知其他用户该更改为活动商品列表。
    8. User2 is notified for the User1's confirmation/denial 通知用户2用户1的确认/拒绝

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: 目前,我已经实现了以下内容:

  1. User1 creates an offer and the offer is 'waiting': User1创建一个要约,该要约正在“等待”中:

    • android app calls createOffer(...) firebase cloud function Android应用程式呼叫createOffer(...)Firebase Cloud功能
    • createOffer(...) adds a new offer to the offers list in realtime database. createOffer(...)将新商品添加到实时数据库的商品列表中。
    • onWrite(...) realtime database trigger is fired on active offers list level 在活动商品列表级别上触发onWrite(...)实时数据库触发器
  2. here I need the hint 在这里我需要提示

  3. User2 requests the offer and the offer is 'requested': User2请求要约,并且要约被“请求”:

    • android app calls requestOffer(...) firebase cloud function Android应用程式呼叫requestOffer(...)Firebase Cloud功能
    • requestOffer(...) modifies the offer in realtime database. requestOffer(...)修改实时数据库中的报价。
    • onWrite(...) realtime database trigger is fired on active offers list level 在活动商品列表级别上触发onWrite(...)实时数据库触发器
  4. here I need the hint 在这里我需要提示

  5. here I need the hint 在这里我需要提示

  6. User1 confirms/denies the User1's request: 用户1确认/拒绝用户1的请求:

a) confirmation: a)确认:

  • android app calls confirnOfferRequest(...) firebase cloud function android应用程序调用confirnOfferRequest(...)firebase云功能
  • confirmOfferRequest(...) removes the offer from active offers list. confirmOfferRequest(...)从活动商品列表中删除商品。
  • onWrite(...) realtime database trigger is fired on active offers list level 在活动商品列表级别上触发onWrite(...)实时数据库触发器

b) denial: b)拒绝:

  • android app calls denyOfferRequest(...) firebase cloud function Android应用程式呼叫denyOfferRequest(...)Firebase Cloud功能
  • denyOfferRequest(...) modifies the offer in active offers list. denyOfferRequest(...)修改活动商品列表中的商品。 onWrite(...) realtime database trigger is fired on active offers list level. 在活动商品列表级别上触发onWrite(...)实时数据库触发器。

.

  1. here I need the hint 在这里我需要提示

  2. here I need the hint 在这里我需要提示

================================== ==================================

So I need hints about notifications to android application: steps 2, 4, 5, 7, 8 因此,我需要有关向Android应用程序进行通知的提示:步骤2、4、5、7、8

I guess I should use FCM, but: 我想我应该使用FCM,但是:

  1. Android tutorials I find are about the deprecated FirebaseInstanceId 我发现的Android教程与已弃用的FirebaseInstanceId有关

  2. I'm not sure what is my case: 我不确定我的情况是什么:

    • topic messages 主题讯息
    • device groups 设备组
    • upstream messages 上游消息
  3. I'm not sure how to notify android app by onWrite(...) realtime database trigger. 我不确定如何通过onWrite(...)实时数据库触发器通知android应用。

  4. 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. @James Poag ,谢谢您的提示。 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. 似乎更简单。

  1. is there a reason to use onCreate() instead of onWrite() ? 有理由使用onCreate()而不是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. 目前,我的计划是在创建/更新商品时执行相同的操作,因此我认为onWrite()可以解决这两种情况。 Or not ? 或不 ?

  1. thanks for transaction hint :) 感谢交易提示:)

  2. 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. 下面的要旨是您要使用Admin SDK发送消息。

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. 您可以创建主题,让用户订阅主题,然后向主题发送消息(在上面的同一链接中找到),或者如果用户数量很少,那么您可能只想向数据库中的每个人发送消息。

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. 同样,不要依赖Android中默认的FCM消息处理,使用步骤2中的链接手动将通知发布到用户的任务栏中。


  1. User1 creates an offer and the offer is 'waiting': User1创建一个要约,该要约正在“等待”中:

    • android app calls createOffer(...) firebase cloud function Android应用程式呼叫createOffer(...)Firebase Cloud功能
    • createOffer(...) adds a new offer to the offers list in realtime database. createOffer(...)将新商品添加到实时数据库的商品列表中。
    • onWrite(...) realtime database trigger is fired on active offers list level Consider using onCreate() instead of onWrite() 在活动商品列表级别上触发onWrite(...)实时数据库触发器考虑使用onCreate()而不是onWrite()
  2. Use the admin sdk to send notifications to your app users. 使用admin SDK将通知发送给您的应用用户。 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. 当您的用户使用设备登录时,请从FirebaseMessaging中获取其令牌,并将其写入保存其个人资料的Realtime数据库。

  3. User2 requests the offer and the offer is 'requested': User2请求要约,并且要约被“请求”:

    • android app calls requestOffer(...) firebase cloud function Android应用程式呼叫requestOffer(...)Firebase Cloud功能
    • requestOffer(...) modifies the offer in realtime database. requestOffer(...)修改实时数据库中的报价。 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 在活动商品列表级别上触发onWrite(...)实时数据库触发器
  4. Send a data only notification to the Topic group. 向主题组发送仅data通知。 Inside your Android app, process the data payload , open the NotificationManager and update the previous notification to 'requested'. 在您的Android应用中, 处理数据有效负载 ,打开NotificationManager并将先前的通知更新为“ requested”。

  5. *When the offer is created in the database, be sure to include the owner's device token. *在数据库中创建报价后,请确保包括所有者的设备令牌。 This way, when the offer state is changed to 'requested' you can use the admin sdk to fire off a notification to the owner. 这样,当要约状态更改为“请求”时,您可以使用admin sdk向所有者发送通知。 * *

  6. User1 confirms/denies the User1's request: 用户1确认/拒绝用户1的请求:

a) confirmation: a)确认:

  • android app calls confirnOfferRequest(...) firebase cloud function android应用程序调用confirnOfferRequest(...)firebase云功能
  • confirmOfferRequest(...) removes the offer from active offers list. confirmOfferRequest(...)从活动商品列表中删除商品。
  • onWrite(...) realtime database trigger is fired on active offers list level 在活动商品列表级别上触发onWrite(...)实时数据库触发器

b) denial: b)拒绝:

  • android app calls denyOfferRequest(...) firebase cloud function Android应用程式呼叫denyOfferRequest(...)Firebase Cloud功能
  • denyOfferRequest(...) modifies the offer in active offers list. denyOfferRequest(...)修改活动商品列表中的商品。 onWrite(...) realtime database trigger is fired on active offers list level. 在活动商品列表级别上触发onWrite(...)实时数据库触发器。

.

  1. ** Same as #4, but if accepted, then use the Notification Manager to Cancel the notification completely ** **与#4相同,但如果被接受,则使用通知管理器完全取消通知**

  2. When User2 fires off step #3, they should send their device token as a part of the request. 当User2触发第3步时,他们应该发送设备令牌作为请求的一部分。 This way, when the request is accepted, your cloud functions will know who to notify. 这样,当请求被接受时,您的云功能将知道通知谁。


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). 他们注销后,从组中删除令牌(实际注销之前)。

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 . 由于所有内容都是通过云功能路由的,因此您可以将数据库的权限设置为.read : false, .write : false ,然后为配置文件部分(如果需要)可以添加特殊的.write : auth !== null && auth.user === uid Otherwise, the Admin SDK has special permissions to read/write from your database. 否则,Admin SDK具有从数据库读取/写入的特殊权限。

Also, look into checking the authorization when someone calls your HTTPS function to make sure they are signed into Firebase. 另外,当有人调用您的HTTPS函数以确保他们已登录Firebase时,请检查检查授权

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

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