问题
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.'));
}
回答1:
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.
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 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 Consider using onCreate() instead of onWrite()
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 requests the offer and the offer is 'requested':
- 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
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'.*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. *
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.
.
** Same as #4, but if accepted, then use the Notification Manager to Cancel the notification completely **
When User2 fires off step #3, they should send their device token as a part of the request. 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
. Otherwise, the Admin SDK has special permissions to read/write from your database.
Also, look into checking the authorization when someone calls your HTTPS function to make sure they are signed into Firebase.
来源:https://stackoverflow.com/questions/51927191/firebase-cloud-functions-to-android-application-notifications