Firebase, listening for a read - firebase

Is there any way of listening to a read on a reference?
E.g
user1 adds a readListener to ref/books/book1
user2 observes/reads ref/books/book1 and gets its value
user1's readListener is triggered
I haven't seen anything like it in Firebase yet, and I guess a workaround would be to have user2 set some ref/books/book1/read value, but I'm interested to hear if anyone has any other ideas.

Firebase is a fairly simple JSON database. There is nothing built-in to fire an event when another user has received specific data.
But as you indeed said, you can build something like that yourself. Most chat applications that need read-confirmation either do this per message or write a "last message ID that I received" marker.

Related

telegram use schedule message

I want to schedule a telegram bot message to be sent at a specific unixtime.
As from telegrams official api (https://core.telegram.org/api/scheduled-messages) that should be possible by setting the schedule_date flag.
To schedule a message, simply provide a future unixtime in the schedule_date flag of messages.sendMessage or messages.sendMedia.
However I was not able to set that flag. To be more precisely, I do not even know how to set a flag, or if I am using the correct api.
What I have tried is to use the api directly via the browser (could use curl as well) like so: https://api.telegram.org/botBOT:TOKEN/sendMessage?chat_id=ID&text=Test&schedule_date=1653503351
I also did not find any way to access this flag via https://pypi.org/project/pyTelegramBotAPI/#description https://telepot.readthedocs.io/en/latest/#send-a-message, nor https://github.com/nickoala/telepot.
I want to implement this feature in a python environment, but any working suggestion would be much appreciated.
EDIT:
I decided to save the intention to send a telegram bot message at a certain unixtime in a database. I then create an infinite loop that checks if there are any unsent messages before the current timestamp. If the loop detects such a message it sends the message and sets a flag, that that message has been sent.
And as promised, here is a fully dockerized example of that behaviour in action: https://github.com/Sokrates1989/nameTheCountDown-lightweight
It creates a bot that you can pass a name and the duration. Once the duration has passed it sends a message with the passed name. Basically a simple countdown that you can give several names, that run simltaniously. As it is a telegram chat, you can modify the way you are informed about the end of a countdown by modifying the notificaiton of that chat.
And here is the Bot in action: http://t.me/NameTheCountdownBot
We can't do this by bot API itself, and there's no schedule_date parameter in sendMessage method:
https://core.telegram.org/bots/api#sendmessage
And what you've read is for Telegram clients, not bot API consumers.
If you don't really need unixtime, you can simply create a table for scheduled messages with a text, chat_id and a publish_time column (like 22:15), and run a command every minute to look if there's a message for current time to send. Then send the message and delete the record.
Note that the python-telegram-bot library has a built-in solution for scheduling tasks: The JobQueue. This feature is based on the APScheduler library, which you can ofc also use without python-telegram-bot.
Disclaimer: I'm currently the maintainer of python-telegram-bot.
https://core.telegram.org/method/messages.sendScheduledMessages
Now you can send scheduled messages right away

Firebase Cloud Messaging Reports wrong

I am sending cloud-messages to my app but Firebase-CF-Reports tells me that they would not be received:
But I know for sure that some devices do receive them. e.g. my own. So something is going wrong here in the reports.
I read about this problem here and here but I already have an analytics-label that I send with my cloud-message.
This is how I sent my notifications with java-admin-sdk:
Message message = Message.builder()
.setTopic(topic)
.setAndroidConfig(AndroidConfig.builder()
.setPriority(AndroidConfig.Priority.HIGH)
.build())
.setApnsConfig(ApnsConfig.builder()
.setAps(Aps.builder()
.setMutableContent(true)
.setContentAvailable(true)
.build())
.putHeader("apns-push-type", "background")
.putHeader("apns-priority", "5")
.putHeader("apns-topic", "my.bundle.id")
.build())
.putData("\"content\"", contentString)
.putData("\"actionButtons\"", actionButtonsString)
.setFcmOptions(FcmOptions.withAnalyticsLabel("SendToAll"))
.build();
Also interesting is, that If I am not filtering for Platform/Channel (altough still filter only for my android app with Apps=) I get this:
But these numbers still don't make any sense. I also opened some notifications on my own device. And I can't believe that only 18 were received.
Has anyone an idea what I am doing wrong?
I use this fcm-sdk in my flutter app:
firebase_messaging: ^9.1.2
Despite I did not find this in the official documentation, I found information in this discussion in the comments to this answer here. Turns out that subscribing to a topic in FCM is not necessarily permanent. So don't subscribe users to a topic once. Instead do it on every app start, although it is
"not technically necessary. It may depend on your use case. For
example, if you want a global topic where all users are a member of,
you'd have to make sure that they are subscribed to it. Putting the
subscribe method when the app starts guarantees this."
-#AL.
Since I changed that, the Notifications are received by a lot more people than before. Only the open-count is still not working for me. It is always on zero.

Using Firebase-Functions to Send Notifications Only When A Condition Is Met

I am working on a project that's supposed to send notifications to it's users when current date is equal to a given date specified by the user. So which firebase-function method is best suited for such kind of application: onUpdate, onWrite, onCreate or https.onRequest?
Note: the app or firebase-function should always check and compare the currentDate to the dates of every record in the database and send notifications if they match.
If I understand correctly that you want to regularly check if today's date (i.e. current date) is equal to a specific field date of one or more records in the database, you could indeed call a dedicated Cloud Function via an HTTP Request every day with an online CRON job like https://www.easycron.com/
You would then, in this Cloud Function, make a query to retrieve all records having this field equal to today's date and send a notification to the user.
If you give more details on your database structure and on the code you have already written, the community may be able to help you further.
PS: look also this answer from Frank van Puffelen: Cloud Functions for Firebase trigger on time?
I think I got a better site for this. Please try https://cron-job.org/en/. It's free and works / worked fine for me.
Thanks to everyone who spared there time to check out and possibly try to answer this question.

Can I implement beforeCreate trigger on Firebase Functions

On the Firebase docs they mention 4 types of triggers:
onCreate
onDelete
onUpdate
onWrite
Is there a way to listen to added row in the Cloud Functions and modify fields of an added row before the "child_added" listeners are triggered? Is there a way to implement BeforeCreate?
Desired BeforeCreate cycle (in Cloud Functions):
Request to add a new message
Change the message fields
Add a new message with modified fields
Clients receive a "child_added" event
All events for the Realtime Database in Cloud Functions trigger asynchronously after the write has been committed. For this reason, other users may already have seen the data before your function can change it.
To solve this problem you'll want to ensure the data only gets written to the location everyone sees after it's been validated/modified.
To validate/modify the new data before listeners to that data can see it, you have two options:
Use a HTTP triggered function for writing the data. The application code calls the HTTP function, which does the data manipulation you want, and then writes the result to the database.
Have the applications write to a "moderation queue", which is just a separate location in the database. The Cloud Function triggers fro this queue, validates/modifies the data, writes it to the actual location, and then deletes it from the queue.
With both of these approaches you lose parts of the transparent offline behavior of the Firebase Realtime Database though, so you'll have to choose.
You need to use onWrite for this to work, as you are saving to the database more than once when you are using child_added.
onWrite(handler) returns functions.CloudFunction containing non-null functions.database.DeltaSnapshot
Event handler that fires every time a Firebase Realtime Database write of any kind (creation, update, or delete) occurs.
more info here: https://firebase.google.com/docs/reference/functions/functions.database.RefBuilder#onWrite
Op wants to do the following:
Request to add a new message
If he wants to request a new message from the end-user then it is better done on the client side.
Change the message fields
Here he wants to change what was written inside the field, which is also usually done on the client side not in cloud functions.
Add a new message with modified fields
Here he wants to add the new message to the database (according to my analysis). Then this can be done in the cloud functions and the message can be added using set()
Clients receive a "child_added" event
then here after adding the new message to the database, he wants the client to receive the database trigger, that will be triggered with the new message. Here he can use cloud functions like onWrite() or onCreate()

can i get more granular ami events for app_fax?

When sending a fax using spandsp/asterisk/app_fax, it would really be nice to see more granular status events being fired.
Right now, it just looks like AMI events are fired when the call is started and when the call finishes. I'm looking to get more detailed info like "DIALING", "SENDING PAGE 1", etc.
Is there a way (either using the AMI or another method) to get this information?
I know it definitely exists somewhere, as I can see DEBUG statements with this information in the logs. I can't (and don't want to) parse the log files, as the DEBUG statements don't really say which call they relate to.
Thanks!
no
but nojee fax project did special patch for app_fax.c to give more verbosity.
probably u can create patch for app_fax.c to show needed info.

Resources