I am not sure how is this process called. Say I am a user of the app and I want to know if there is a message for me (or a status change that I need to know about), I am not sure if this is the best way but I am trying to do it like this:
Firebase Structure
Users > User A > Status = "No messages"
Each user has a node Status as you can see above.
When user A sends a message to user B, user A changes user B's Status node.
User B, that had been listening to his own Status node, is now aware that there is something new and can go read the message.
Is this possible and safe making use of Firebase only?
On a simpler note, and more often used scenario similar to yours is like a Chat Application. User A sends a message to User B, User B then receives a notification from Firebase.
From what you have described, you intend to have a listener in place for the Status node, and inform the user whenever it changes. This seems okay, but from what was advised to me before, keeping the listener active tends to have a corresponding active socket on Android, which adds to battery consumption.
What I suggest you make use of is firebase-cloud-messaging:
Firebase Cloud Messaging is a component of the Firebase suite of tools for cross-platform application development.
Send unlimited upstream/downstream messages
Send messages to individual devices or a user segment
Handle all aspects of queueing and delivery
Optimize for battery efficiency
Using FCM, you can notify a client app that new email or other data is available to sync. You can send notifications to drive user reengagement and retention. For use cases such as instant messaging, a message can transfer a payload of up to 4KB to a client app.
I think this Firebase Codelab for a Chat App might also be helpful.
Related
So, I just want to handle the situation when user logs out from the app.
I call method FirebaseMessaging.getInstance().deleteToken() when user logs out, and it works as expected, i.e. user doesn't receive push messages anymore...
But if there is no internet connection method deleteToken() doesn't help. User still gets push messages when a connection is established despite the fact that he logged out.
How can I handle this?
Firebase Cloud Messaging has no knowledge of the user, which explains why it can't change whether it sends/displays a message based on authentication status.
The main problem here is that notification messages are displayed by the system if the app is not being used, and you can't control what the system does with them based on authentication state.
The best way to deal with these situations is to only send data messages (without a notification property). The display of such data-only messages is handled by your application code, so your application code can check whether a user is signed-in, and decide whether to display the message based on that.
I also recommend reading up on FCM messages types to learn more about the distinction between these types.
I'm using Firebase's FCM for push notifications for an app. There is a need to subscribe to certain topics from the moment the user starts the app.
So far I've handled "topics" and various metrics on our own server, but as we're moving logic parts to their appropriate place to lessen the load on our server, this needs to be moved to Firebase itself.
And we want to handle things a bit differently. Users will be able to subscribe and unsubscribe to/from certain notification groups, however FCM's documentation does not mention which is the time, which callback is the proper place to handle this.
The flow would be the following:
User installs app
User launches app
App updates cached data (information that changes in larger intervals, e.g. every 3-6 months)
App pulls synced data, including topics list, from Google account
App registers device for push notifications
App subscribes user to "all" topic (all users that want to receive generic notifications are registered here. Unsubscribing is only possible if the user unchecks the "send me notifications" option in settings)
App subscribes to the topics synced in step 4
What isn't clear is WHERE to place steps 6 and 7. Do I put it into my implementation of FirebaseInstanceIdService, into OnTokenRefresh on Android, and in Messaging.SharedInstance.Connect or InstanceId.Notifications.ObserveTokenRefresh on iOS? Do I need to re-register to topics when an FCM token change happens?
The app is written in Xamarin, so we're using FCM for both iOS and Android.
Where you place the code for subscribing the token depends entirely up to you. Usually though, it is placed on the initial activity of the app. This ensures that the user will be subscribed to that certain topic.
However in your use-case, you could make a method that checks if the Notification Settings for your app is ticked or not, if yes, subscribe the token, if not, unsubscribe. Then simply call this method on your initial activity.
With regard to "Do I need to re-register to topics when an FCM token change happens?", you don't have to. Referring to this answer by #DiegoGiorgini:
If the token is "refreshed" the topic subscriptions are maintained.
The register to topic should be placed in the View (Activity class in Android). It depends on where you need it. It could be achieved by using this line of code
FirebaseMessaging.Instance.SubscribeToTopic("promotion");
Note that when you subscribes to topic that does not exist, the topic will be created in the server. However the creation requires long time so the topic could not appear instantly in your Firebase console.
Later on, when you want to unsubscribe (probably after logout), just call this inside your View as well.
FirebaseMessaging.Instance.UnsubscribeFromTopic("promotion");
Hope this could help.
I want to build an Android app that allows a user to send notification and data messages to other users. I've started using Firebase only recently, and Firebase Cloud Messaging is still pretty confusing to me.
I have already implemented sending a message to a specific device using the Firebase Instance Id. However, in my app, a user can log out and log into their accounts using different devices, so this isn't really what I want. I read the documentation and it's pretty confusing, they mention sending messages to user groups, and topics (which only apps can subscribe to, not users).
Is there a way to send a push notification directly to another user or a group of users using only their UIDs? If not, is there any other way I can implement this?
I did it storing the FCM Token in a device structure by user. When the user login, add the deviceData to the userToken structure. You need clear deviceData when the user logout. This way, you will only send notifications to logged devices.
Ex (This is not like mine structure, but it can help you to wondering a good way to do this):
-userToken
-idUser1
-device
-idDevice1
-fcmToken: "xxxxx"
-idDevice2
-fcmToken: "YYYYY"
Hope that I helped you
Alright, so to make sure this works across multiple devices when a user logs in, I just make the device subscribe to a topic with the name equal to the user's UID. Now if I want to send a notification to a user, I just send an FCM message on the topic with the name equal to the user's UID!
Example: If my UID is equal to "asdf", whenever I login using multiple devices, each of the devices automatically subscribes to the topic with name "asdf". So now, if I send an FCM from the server on this topic, all the devices from which I am logged on to obtain this notification.
I'm working in a friend request module for mobile app base on Firebase so I'm considering to choose the way that notification is pushed.
Assume that userA request to be friend with userB. There're 2 ideas now:
- userA send request to a simple server then it will call FCM to send notification to userB.
- Make a service that listen to data changed in Firebase realtime database then userA will make change on that db and notification will be shown on userB device.
I think both are possible to implement but what is better, and why?
Please give me some advice about this..
Thanks in advance.
Using either one should be fine.
However, a point to consider here is when keeping a listener active for the Real-time database, it also keeps an open socket on the user's device which adds to battery consumption.
While for FCM, it will only trigger once there is a notification is needed to be sent. If a friend request isn't really that app critical, I think using FCM is a way to go.
Have you also considered using both? If the user is currently online, it would be good to use the Real-time DB, but the childAdded won't be triggered if the user is offline (not using the app for instance). In that case, you can set it so that a notification will be sent to the user.
The important thing in your scenario is that the friend request should be saved first in your database or app server, so that it will trigger the corresponding action (FCM notification or Real-time DB update).
We are implementing a mobile app which let users share to-do lists. The idea is to have as little server administration as possible and obviously keep cost down.
For user management and push notifications we will use Parse.com with Cloud Code and PubNub for real time data delivery.
Every user will log in with it facebook' s credentials and subscribe to a read only private channel that only him can read. Every time he create a new to-do list to share with his facebook's friends, the app will make an API call to CloudCode, with it's identity, the data to share and a list of friends. In CloudCode the data is pushed to the PubNub private channels of the list of friends.
In addition in CloudCode the idea is to use PubNub Presence and if the user is offline, send a Push notification.
Is this implementation ok? I'm new to both services and trying to learn. Thanks!
PubNub is appropriate if you are trying to implement a realtime app such as a chat application or a GPS tracking application. But in your case, I think you may not need PubNub's features at all. You can have the afore mentioned functionalities using Parse.com only. The parse push can be used in both the scenarios.
When the user is logged in, and is using the application :- Receive the push, suppress the notification and update the UI with the newly received data.
When the user is not online or logged in, then simply create a notification and add a click listener activity for it.
The reason why I suggest to remove PubNub (for this particular application) is that, PunNub has a different pricing model for loading history. On the free plan, you are limited to one day of message history. On the other hand, you can run this app's backend on Parse.com, almost free cost.
There's nothing in the architecture you describe above that Parse can't handle, including the ability to support Facebook login and external service calls via Cloud Codes Parse.Cloud.httpRequest(). It will do it and do it well thus the answer is yes, this is ok.
Go forth and Parse.