首页 > 解决方案 > Firebase Cloud functions to Android application notifications

问题描述

I need some hints for the following system:

One of the scenarios is as follows:

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':

    • 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
  2. here I need the hint

  3. 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
  4. here I need the hint

  5. here I need the hint

  6. User1 confirms/denies the User1's request:

a) confirmation:

b) denial:

.

  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

I guess I should use FCM, but:

  1. Android tutorials I find are about the deprecated 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.

  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. 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() ?

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 ?

  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.'));
}

标签: androidfirebasefirebase-cloud-messaginggoogle-cloud-functions

解决方案


以下要点是您要使用Admin SDK 发送消息。

您可以创建主题,让用户订阅主题,然后向主题发送消息(在上面的同一链接中介绍),或者如果有一小部分用户,那么也许您只想向数据库中的每个人发送消息。

此外,不要依赖 Android 中的默认通知处理来处理您的 FCM 消息,请使用步骤 #2 中的链接手动将通知发布到用户的托盘。


  1. User1 创建了一个报价并且报价正在“等待”:

    • android 应用调用 createOffer(...) firebase 云函数
    • createOffer(...) 在实时数据库中的报价列表中添加一个新报价。
    • onWrite(...) 在活动商品列表级别触发实时数据库触发器考虑使用 onCreate() 而不是 onWrite()
  2. 使用 admin sdk 向您的应用用户发送通知。这将需要发送到组主题或遍历用户数据库并发送到用户的设备令牌。当您的用户使用设备登录时,从 FirebaseMessaging 获取他们的令牌并将其写入保存其个人资料的实时数据库。

  3. User2 请求报价并且报价被“请求”:

    • android 应用调用 requestOffer(...) firebase 云函数
    • requestOffer(...) 在实时数据库中修改报价。在这里,您需要查看交易以防止多个用户获得对 Offer 的锁定
    • onWrite(...) 实时数据库触发器在活动报价列表级别触发
  4. 向主题组发送data唯一通知。在您的 Android 应用程序中,处理数据负载,打开NotificationManager并将之前的通知更新为“已请求”。

  5. *在数据库中创建报价时,请务必包含所有者的设备令牌。这样,当报价状态更改为“已请求”时,您可以使用 admin sdk 向所有者发出通知。*

  6. User1 确认/拒绝 User1 的请求:

a) 确认:

  • android 应用调用 confirnOfferRequest(...) firebase 云函数
  • confirmOfferRequest(...) 从活动报价列表中删除报价。
  • onWrite(...) 实时数据库触发器在活动报价列表级别触发

b) 否认:

  • android应用调用denyOfferRequest(...) firebase云函数
  • denyOfferRequest(...) 修改活动报价列表中的报价。onWrite(...) 实时数据库触发器在活动报价列表级别触发。

.

  1. ** 与#4 相同,但如果接受,则使用通知管理器完全取消通知 **

  2. 当 User2 触发第 3 步时,他们应该将他们的设备令牌作为请求的一部分发送。这样,当请求被接受时,您的云功能将知道该通知谁。


当用户登录设备时,将他们的令牌发送到主题组。当他们注销时,从组中删除令牌(在实际注销之前)。

由于所有内容都通过您的云功能进行路由,因此您可以将数据库上的权限设置为.read : false, .write : false,然后对于配置文件部分(如果需要),您可以添加一个特殊的.write : auth !== null && auth.user === uid. 否则,Admin SDK 具有从您的数据库读取/写入的特殊权限。

此外,当有人调用您的 HTTPS 函数时检查授权,以确保他们已登录 Firebase。


推荐阅读