I am trying to send a notification to all users of my app whenever there is a write on the database.
Here is the code that I have so far.
export const newUserAdded = functions.database
.ref('1_0_0/updateDate/date')
.onWrite((event)=>{
//send notfigication to all
admin.messaging().sendToCondition()
})
As you can see whenever there is a write to the date key the function will trigger.
But note here that I do not have device token of the user as I want to send a notification to all users. I was not able to find any method do so.
Kindly do let me know how can I achieve this.
Thanks
You can use topic messaging. Arrange for all your client apps to subscribe to a dedicated, named topic for broadcasting to all app installations, then send your message to that topic.
Related
hello I'm new to flutter and trying to get push notification in flutter , I have successfully subscribed the user to a topic and sent a notification through the firebase console by topic , what I'm trying to do is to have a button for admins in the app and when they post for a particular topic the users with that topic get the notification I have heard about cloud functions but I don't know exactly where to start ! i hope I made my point ! I have seen some videos that talks about type script but is there is any workaround this ?
The Firebase documentation names notifying the user when something interesting happens as an example use-case. From there:
Developers can use Cloud Functions to keep users engaged and up to date with relevant information about an app. Consider, for example, an app that allows users to follow one another's activities in the app. Each time a user adds themselves as a follower of another user, a write occurs in the Realtime Database. Then this write event could trigger a function to create Firebase Cloud Messaging (FCM) notifications to let the appropriate users know that they have gained new followers.
The function triggers on writes to the Realtime Database path where followers are stored.
The function composes a message to send via FCM.
FCM sends the notification message to the user's device.
To review working code, see Send FCM notifications.
We are building an app with chat functionality in Flutter, and I have figured out almost everything about how it should be done, except how the notifications can be sent to only one device (Or possibly a few devices).
The best option so far is to send the notification to an FCM registration token since we use Firebase for all of our back-end, but I can't do that without knowing the token. And the only way I can know that is to store it in Firestore (For example the users document) and retrieve it when needed. Is this a good solution, and what happens if the registration token changes? Should I update the token every time the user opens the app to make sure that it is correct?
You don't need to store the token on the server, you just need to know who you are sending the message to. Just follow these steps
Client
1.a When you app starts, retrieve the token via getToken()
1.b Store it locally
1.c if it has changed, send it to a Cloud function, and register it to a topic or device group
1.d Dont forget to register to onTokenRefresh() to repeat 1.a-c for new tokens
Server
2.a Implement the function corresponding to 1.c
2.b When required, send messages to a topic or device group
I want to know whether the email is delivered to the respective email account or not Using Firebase Auth API's
Let me know if there is any workaround ?
I assume you are referring to the Firebase Authentication. The user is retrieved from mAuth.getCurrentUser() method, and the sendEmailVerification() method is called on it. This gets Firebase to send the mail, if the task was successful, you'll see that a toast will pop up showing that it happened.
sendEmailVerification()
I have made an app with the use of firebase. It is basically a chat application in which you van send and receive the text and images. I want to add a functionality in it that whenever a user sends a msg, then another user should get a notification . When I try to send a notification through the firebase console, then it is working, but when a user messages through the app, then it is not showing any notification to another user. So, can anyone tell me that how can this functionality be achieved ?Also, provide some sample code to see how things are working
You need to use cloud functions for that:
https://firebase.google.com/docs/functions
Cloud Functions for Firebase lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests
First, you can register the user to a topic:
FirebaseMessaging.getInstance().subscribeToTopic("news");
https://firebase.google.com/docs/cloud-messaging/android/topic-messaging
Then when the message is sent to the database, you can use onWrite() in cloud functions which is a database trigger to be able to send the notification.
https://firebase.google.com/docs/functions/database-events
Yesterday Google has announced a new set of tools for Firebase, one of them was Notifications the ability to send notifications from server to devices which are using my app.
But can we now notify users when they receive a new message?
And if not, is there a way around to achieve this?
Looking at the documentation, this doesn't seem currently possible automatically. Here is a possible way to accomplish it "manually" with another server:
Subscribe a user to it's own user ID
In android
FirebaseMessaging.getInstance().subscribeToTopic("InsertUserIDHere");
In IOS
[[FIRMessaging messaging] subscribeToTopic:#"/topics/InsertUserIDHere"];
Setup an outside server that checks every sent messages. When a message is sent, the server should create a notification that includes the recipient's user ID as the topic.
Look here for more info.
You can send messages to group of users (targeting a specific "topic") or to a single device (targeting a Firebase Cloud Messaging token).
To subscribe a device to a topic use:
FirebaseMessaging.getInstance().subscribeToTopic("topicName");
To obtain the device token use (*1) :
FirebaseInstanceId.getInstance().getToken();
Then you can use the Firebase Notificaitons web console, or the FCM server API if you want to send messages from your server.
See: https://firebase.google.com/docs/cloud-messaging/downstream#sending_topic_messages_from_the_server
Notes:
[1] getToken() can return null if the token is not yet available.
You can use the callback onTokenRefresh() to be notified when the token is available and when the token is rotated.
See: https://firebase.google.com/docs/cloud-messaging/android/client#sample-register