Firebase Cloud functions to Android application notifications
Firebase Cloud functions to Android application notifications
I need some hints for the following system:
One of the scenarios is as follows:
Users are sign-in via OAuth2 (google accounts).
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.
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':
here I need the hint
User2 requests the offer and the offer is 'requested':
here I need the hint
here I need the hint
User1 confirms/denies the User1's request:
a) confirmation:
b) denial:
.
here I need the hint
here I need the hint
==================================
So I need hints about notifications to andoid 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:
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.
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 Answer
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':
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':
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'.
data
*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:
b) denial:
.
** 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.
.read : false, .write : false
.write : auth !== null && auth.user === uid
Also, look into checking the authorization when someone calls your HTTPS function to make sure they are signed into Firebase.
This app has some similar features: youtube.com/watch?v=p4yLzYwy_4g
– James Poag
Aug 23 at 11:44
Starting at 18 minutes is when they start to reserve items using the firestore (similar to database): youtu.be/p4yLzYwy_4g?t=18m6s
– James Poag
Aug 23 at 11:54
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thank you for the advices. I'll try and give feedback. I'll append my comments to the original post because they exceeds a regular comment size.
– user1010286
Aug 23 at 11:18