ParseRelation<ParseUser> relation = profile_user.getRelation(ParseConstants.KEY_LIKES);
relation.add(ParseUser.getCurrentUser());
profile_user.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException arg0) {
Toast.makeText(ViewProfilePic.this, "Liked!", Toast.LENGTH_SHORT).show();
}
});
In this program when a person visits a particular profile he can like that profile.
profile_user is a ParseUser object which is a particular profile. If the user clicks on the like button the current user should be added to the profile_user 's "likes relation. But this does not happen . Please help!
I found the solution. By default parse does not allow you to save unauthenticated users for security reasons I.e only the current user value can be modified. I overcame this using parse cloud code.
Related
I am building a chatapp using firebase database. I need to show online users, how can I do that?
I already tried this
connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
boolean connected = snapshot.getValue(Boolean.class);
if (connected) {
} else {
}
}
#Override
public void onCancelled(DatabaseError error) {
System.err.println("Listener was cancelled");
}
});
But I don't know how to use it, please help me.
To achieve this, you need to create a new category in your Firebase database called onlineUsers. Every time a user is connecting to your app, add him to this newly created category. Then you can query on that category to see the exact number of users like this:
int numberOfUsers = dataSnapshot.getChildrenCount();.
If you need to display a green dot for online members in a list of members, then the way in which you need to add these users in this new node, is to have the id as it's key and the value as a boolean. The default boolean value must be false, which means that the user is not logged in. Your new node should look like this:
userId1: true
userId2: false
userId3: true
Every time a user signs in, change the value of that user from false to true. To display them, just query your database accordingly to see which users have the value of true.
If clients have a server to work up against,
each client connection is reported to the server, which increments a coutner.
And for such increment server is notifying (e.g. using Firebase data notification message) all clients.
This is how all clients will have indication about how many are conencted at any given time.
(When a new user is logged in, server is sending data about current number of connections)
I just started storing usertokens in my database for notification purposes. I have no problem storing and retrieving the data except with certain usertoken string values. But when I take a datasnapshot with a usertoken it returns null, only sometimes though.
My listener looks like this (just a regular listener):
udRef.addListenerForSingleValueEvent(new ValueEventListener () {
#Override
public void onDataChange(DataSnapshot datasnapshot) {
User user = datasnapshot.get value(User.class);
//do something with data here
//eg. Log.e("ERR", String.value Of(user));
//returns "null" for SOME users only, keep reading.
}
#Override
public void onCancelled (DatabaseError databaseError) { ... }
});
When a user creates an account, it automatically inserts a string token that looks like this:
"cL_AEuSZa1I:APA91bF9R-dDetqrSNMQMzAoHRmLOHXK2acK_zJLhFhZ5IhvNChPSx4P943GirVwsy5walc0RrvyGnMm49oLxNJ-uKsGRCELhJ-9kwnPmdHOyWJWz0Esm_Y0MB7BypNUuQ4qZQmoW5nO"
Tokens are used for push notifications using FCM. This is refreshed automatically as recommended by the FB guys. The token above isn't valid, in fact this very usertoken made my datasnapshot not work so I had to manually remove it! And yes, immediately I removed it I was able to capture the snapshot, it didn't return null anymore.
I do not know what I'm doing wrong, but when I look (with my eyes, not a script) at the usertokens in the database I can tell which ones will be troublesome. This is how:
All usertokens are too long, so Firebase's web console cuts them short like this "gftgvdgGhbgfc_fggg..."
All the troublesome tokens get cut also but without the 3 dots or closing quotes, they look like this "gftgvdgGhbgfc_fggg but when expanded they are normal like the working ones (length is same).
I have 30 users so I can easily go through them all :D
This is how my data looks (roughly):
"fhgfFghDfgghhfDDf" : {
"username" : "a_string_here"
"userPic" : "looong_string_link_to_fb_storage"
"usertoken" : "long_user_token_like the_one_in_question"
},...
Am I storing the tokens wrongly?, should I convert them to some other data type? Too many 'gotchas' in FB :(
Please note, I DO NOT have problems with anything else except the usertokens making my datasnapshot null, so any mistakes in the code are just typos (I typed this question from my phone referencing my laptop screen... don't ask)
I'm working in an android app adding the following/follower system, I'm using the lines in the Parse.com, I have a user profile, and when I click on the follow button the app send the both users id (the current user and the other user) in a row. I'm using pointers targeting the _user class.
This is my current code :
Intent i = get intent();
String userId = i.getStringExtra("userid");
ParseObject follow = new ParseObject("Follow");
follow.put("from",ParseUser.getCurrentUser());
follow.put("to", userId);
follow.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
btn_follow.setImageDrawable(getResources().getDrawable(R.drawable.ic_following));
}
});
The btn_follow change the image. which means the following has been done. but my Follow class is empty.
I have changed my Follow table from :
...|from|to|
To :
...|from|to|
This worked well the both users id were saved, but I need pointers not string so I put from and to to pointers again and deleted the line
follow.put("to", userId);
This worked well also but it only saved the current user and not the user I'm trying to follow.
So my problem is with the ID of the user I want to follow.
In the guide they are saying
"ParseUser otheruser = ..."
(I don't no what to put in the 3 points to get the other user id)
You need to assign a ParseObject (or ParseUser) to create a pointer in database. For this you need to create ParseUser without data, just with id like this:
follow.put("to", ParseObject.createWithoutData("_User", userId));
Also, you should check the ParseException e in callback to see whats going on.
I am workin in an Android Project using parse.com and I have a table Requirement which has a column called "user_assigned" which is a relation to ParseUser. This user is in charge to modify the requirement even if he didn't create the Requirement, but when this user try to update the requirement values, it returns "Cannot save a ParseUser that is not authenticated"
P.D. All the ACL in the requirement are public, write and read.
// setting write permission
ParseACL postACL = new ParseACL(ParseUser.getCurrentUser());
postACL.setPublicWriteAccess(true);
postACL.setPublicReadAccess(true);
requirement.setACL(postACL);
requirement.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
callback.onFinish((Exception) e, requirement);
}
I don't want to change the user assigned I want to change other fields but the way I get and change the relation from the table is
ParseRelation<ParseUser> relation = requirement.getRelation("user_assigned");
relation.add(requirement.getUserAssigned());
I found a solution,
What is happening: 3 tables, Project, User and Requirements where a Requirement has a pointer to the Project and the Project has another pointer to an User called projectOwner. The problem happen when another user different to the projectOwner read some values of the projectOwner associated with the Requirements. If the new user wants to save some changes in the Requirements parse return the error "Cannot save a ParseUser that is not authenticated".
To fix it, i only read the objectId of the projectOwner and after I got the rest of the attributes of the ParseUser calling
ParseUser.getQuery();
query.get(objectId)
In other words, in my class Project, i have a method to get the attributes. When I want to get the projectUser (pointer) i got it like this:
User uTemp = (User) this.getParseUser("owner");
if(uTemp != null){
// this.setOwner(uTemp.extractAttributes());
uTemp = uTemp.getObjectId();
uTemp = UserDAO.getUserByObjectId(uTemp.getObjectId());
this.setOwner(uTemp);
}
I could fixed thanks to this, I hope it can be helpful.
I am building an android app with Facebook login, once logged, it'll save information from Facebook to Parse like Facebook ID, name and profile picture. Is there a way to make a column unique so that it can only have 1 kind of FB ID? or check if FB ID exist in my User table? I know it might seem like a very general request, but I've been googling for hours and can't find any specific solution..
I've already faced this problem in iOS, and seems that Android can be solved in the same way.
So, you can mix the FB API with the parse function
ParseFacebookUtils.logIn(String facebookId, String accessToken, Date expirationDate, LogInCallback callback)
relying to facebook app you can obtain the facebookId, accessToken ( i think that for the expirationDate you can set a far date, like what i've done for iOS )
Another solution is:
ParseFacebookUtils.logIn(Arrays.asList(Permissions.User.EMAIL),
this, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
Do your stuff...
}
});
it should automatically detect if an user with that facebook id already exist in your _User table or not ( i can confirm this on iOS, you can check if the user is new or still exist with user.isNew() ). If you need you could also save the retrieved facebookId in a separated field, but i think you could avoid it for this scope
Hope it helps