I am implementing push notification in my IBM worklight project. Sample code in developerworks do it with some authentication challenger mechanism to do the subscription. I somehow used the same Authentication challenge to do the subscription (although my app design doesn't need to do this). In the sample code actual trigger of notification call is not written.
I found out from web resource that WL.Server.getUserNotificationSubscription(eventSourceName, userId) need to be used to get the subscribed user details. However I don't know how to get userId details here? Please let me know what is userId here and how to get its value.
Here is information regarding WL.Server.getUserNotificationSubscripion:
getUserNotificationSubscription:
getUserNotificationSubscription(eventSource, userId) Returns a
subscription object for a user. Returns a subscription object for the
user with the specified ID to the specified event source.
Parameters:
eventSource - Mandatory. A string containing the name of the event source.
userId - Mandatory. A string containing the user ID, created during the login process. The user ID can be obtained by calling
WL.Server.getActiveUser.
Returns:
The method returns a subscription object that contains the user ID and the mutable subscription state.
Example: {userId: 'bjones', state: {numCoupons: 3}}
Note:
All subscription object fields are read-only, except for the user subscription state. You can modify the user subscription state in your
JavaScript code, and then must use the save method to save it to the
IBM® Worklight® database.
As you can see the userId is the specific user that was created during the login and authentication process. Worklight provides a server side API WL.Server.getActiveUser() which allows you to retrieve the current user.
In regards to your other statement saying "in the sample code actual trigger of notification call is not written. " Worklight actually provides a jar file inside of the sample project that allows you to trigger the notification. Here is some more information for this backend emulation:
Let me know if you have any further questions.
Related
I need to implement push notifications for my asp.net core project.For a news service
it needs to work in such a manner that if the user is not online while a message is being sent out, then the message will arrive at the client when they are online thenext time.
All the articles I can find about this, are all slightly vague about this case, and just emphasises the "real-time" communication.
So would it be possible to have push notifications that could that wiht signalr?
it needs to work in such a manner that if the user is not online while a message is being sent out, then the message will arrive at the client when they are online thenext time.
To achieve your above requirement, you can try:
mapping user to the connection id(s) and persist that mapping, which would help find/get new connection id(s) based on user name or email etc readable info after user connect/reconnect to hub, then you can send unread message/data to specific user by specifying connection id(s).
Note: you can also send a message to a specific user by passing the user identifier to the User function in a hub method, please refer to this doc: https://learn.microsoft.com/en-us/aspnet/core/signalr/groups?view=aspnetcore-5.0#users-in-signalr
for unread messages, as #SamiKuhmonen mentioned, you can store/persist those messages in somewhere, and if you want to persist information after restarts or you would host hub on multi-servers/instances, in-memory store is not a good approach. You can try to store messages in database etc permanent, external storage.
define and use a flag to indicates if the message has been seen by user. After client received a message, then invoke a hub method to update that flag or delete that message from the store.
My project is creating a website using firebase, but it is using the internal authen micro service instead of Firebase authen.
Every time the user logs in, the internal authen service will generate a token (I call it client_id), and send it to the client.
This token is also stored in the user collection.
Please help me how to write rule for the user via this client_id.
If it is write role, I can send the client_id via mothod update, delete, create and get it out by
request.resource.data.cliend_id and check it.
But if it is read role, I don't have any way to send to the client_id via the get method to authenticate users
I even thought of replacing all the get methods with the update methods to pass the client_id
But the response of the update method is just the update_time, not the object I updated
Firebase RTDB & Firestore Rules uses their own auth only. In every request that firebase client library sends uid along with jwt token and other security parameters. These are fundamental to firebase's working and thus can't be replaced.
As Aleksey mentioned, you can try custom auth. It is specifically tailored for such use cases only.
There is no way to provide extra data to security rules for the purpose of limiting read operations. That would not be secure at all, because a client could simply fake whatever they want in the query.
You can only use information provided by Firebase Auth available in request.auth, the data in the document itself in resource.data, or the contents of other documents using get(). If you want to attach additional data to the user account, consider using custom claims.
Id like to make a chat between two custom users.
I need to send an message to custom user.
scenario:
User1 get User2 ID from my server
My server get User2 ID from Firebase (probably using Firebase admin)
My server return User2 ID to User1
User1 send message to User2 via firebase
Is it possible to make? Do I have to use Firebase Admin + FCM or something else?
[Giving you a simple guideline and logic here]
One of the simplest ways to implement it is by using Cloud Firestore.
You need to create 2 tables, one for the users and one for the chat. The users will be used to store the users' info including UID, emails, friends (array, to store a list of friends), chats (array, to store a list of chats), etc. While the chat will be used to store all the chats. (You can design your own database, some guideline can be found here)
When the user signed-in with Firebase Auth, you can query the database to get the details from the Firestore. Assuming you put everything in some UI element (recyclerview), once you click on the user's friend (friends are stored in the Cloud Firestore mentioned above), a new chat document will be created in the chat database/table, and under the other user (User 2) document record in the users table/database, update the chat array with the new document ID. For the User 2 UI interface, new chat will be created/updated as Firebase has something like onChangeListener() to update your client.
You will need to design your UI for the chat allowing the user to send messages. The UI will need to fetch all the chat records from the chat document. When any party sends a new message, update the message to the chat document.
So basically that's the rough idea, you can get all those references from Firebase Doc.
For notification, you can use Firebase Cloud Messaging to notify the user!
The actual function of FCM as stated in the doc:
Using FCM, you can notify a client app that new email or other data is
available to sync. You can send notification messages to drive user
re-engagement and retention. For use cases such as instant messaging,
a message can transfer a payload of up to 4KB to a client app.
Therefore, it is not intended user to user but is for the App admin to the user for certain events.
For Admin, it is not encouraged to call Admin all the time for a production consumer app.
P/s: I don't spoon-feed with code but to encourage people to learn from their own research.
Hope it helps!
I'm building an app with a social element in that users can have 1 to 1 conversations. At the moment a user can request a conversation with another user and upon acceptance, a cloud function is triggered which then assigns that conversation to both users. This works really well up to now and I have users being able to send each other messages instantly
My problem lies in that I want users to be notified of messages that haven't been read. I know that I could implement notifications via FCM but I also want the user to be able to see that a conversation has unread messages in their conversation list
Is this possible with firebase?
Edit: I should rephrase, I know this is possible. I just can't think of any structure that would be efficient. I could pass a messageRead Boolean property on each message, however I would need to update this property everytime a message is read by updating the field immediately after getting the documents.
With Firebase Cloud Messaging for Web,
How do I maintain the list of valid tokens in my database? For example I've noticed when a user turns off notifications and revisits the site, a new token will be generated and the old token in my database is useless.
I've also tried using Firebase messaging.onTokenRefresh() callback, but it does not get called when I turned off notifications. Also in this case, even if it did get triggered, it returns a new token that was refreshed. How do I keep track of the old token that was refreshed?
Can someone please share with me their thoughts/ways to maintain and ensure the token list in the database are valid or up-to-date?
Any feedback is much appreciated.
Thank you,
Christina
messaging.onTokenRefresh() is probably a wrapper around the event onpushsubscriptionchange.
Indeed that event is currently only called when the subscription is enabled (or enabled again), but not when the permission for push notifications is revoked. So at the moment you can only know that an endpoint has expired when you try to send a notification to it.
More details:
http://blog.pushpad.xyz/2016/05/the-push-api-and-its-wild-unsubscription-mechanism/
In any case you can use the callback to send any new token to the server: at first you will have two tokens stored for the same browser, one expired and the other valid.
Some problems arise if you have data associated to the endpoint (e.g. tag) that you want to preserve during the endpoint change: see the blog post for some suggestions.