Can player2 sends a notification to player1 using Parse, from the device? - ios

Is it possible to send a push notification from a device (player2), to another device (player1), when player2 has finished, using the "user" identifier in Parse? It would be like this :
player 1 plays, and sends his score
player 2 plays, checks if player1 has finished, if he has, player2 sends a notification to player1 using the "user" identifier of player1 ?

Push notifications are sent to devices, not users. Devices are tracked via the installation table (PFInstallation object). You need to add a column to your installations table in Parse so that it tracks the PFUser that is currently associated with that installation (device). Then you can send a push message that targets installations where the current user is "player 1"
Example -
Whenever your user "logs in" to your game, you need to update the associated installation -
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
currentInstallation[#"currentPlayer"]=[PFUser currentUser];
[PFInstallation saveInBackground];
Then to send a message
PFQUery *pushQuery=[PFInstallation query];
[pushQuery whereKey:#"currentPlayer" equalTo:self.opponent]; // self.opponent is a PFUser
[PFPush sendPushMessageToQueryInBackground:pushQuery
withMessage:#"It's your turn"];

Related

How to send push notification to specific user in Parse? [duplicate]

I have successfully setup parse push notifications and in my installation table I have both an installation, and device token. What I'm really trying to do is send a push notification to certain users, rather than certain devices. How do I bing the installations table to the uses table, so that I can make a query by users and get back a deviceid to push to
From https://parse.com/docs/push_guide#top/iOS, section "Using Advanced Targeting".
You can even create relationships between your Installation objects
and other classes saved on Parse. To associate a PFInstallation with a
particular user, for example, you can simply store the current user on
the PFInstallation.
// Associate the device with a user
PFInstallation *installation = [PFInstallation currentInstallation];
installation[#"user"] = [PFUser currentUser];
[installation saveInBackground];
Now you can create a query on the installation table, where "user" is the user you want to send a push notification to.
Finally, use that query when constructing the push object.
Example in Objective-C (adjust accordingly, if you're sending the push in some other language):
PFQuery *pushQuery = [PFInstallation query];
[pushQuery whereKey:#"user" equalTo:someUser]; // where some user is the user object that is to receive a push notification
PFPush *push = [[PFPush alloc] init];
[push setQuery:pushQuery];
[push setMessage:#"Hi there!"];
[push sendPushInBackground];

error CloudKit access was denied by user settings when user is not sign in

I'm trying to get fetch the notification of changes in the public database:
CKServerChangeToken *token = [[cloudKitToken helper] getCloudKitToken];
CKFetchNotificationChangesOperation *op = [[CKFetchNotificationChangesOperation alloc]
initWithPreviousServerChangeToken:token];
NSMutableArray *noteIds = [[NSMutableArray alloc] init];
op.notificationChangedBlock = ^(CKNotification *note) {
CKNotificationID *noteId = note.notificationID;
[noteIds addObject:noteId];
};
op.fetchNotificationChangesCompletionBlock = ^(CKServerChangeToken *token, NSError *error) {
NSLog(#"error %#", error.localizedDescription);
};
[[CKContainer defaultContainer] addOperation:op];
But when the user has not login to iCloud I get this error: "error CloudKit access was denied by user settings"
By another hand with the same device I can fetch records from the public database with no problem.
What I'm trying to do with the CKFetchNotificationChangesOperation is get the deltas between device and cloudkit.
Any of you knows how can I get the deltas when the user has not login to iCloud?, does fetch records is my only option for this case?
I'll really appreciate your help.
With CKFetchNotificationChangesOperation you can retrieve all the notifications that were send to you since a previous fetch. These notifications are linked to subscriptions that you previously created for a device. Subscriptions are linked to an account. So you must be logged in with the same account in order to fetch these notifications.
In your case you might be better of by using CKFetchRecordChangesOperation. Then you would be able to just query for CloudKit changes since a previous query. Since it's just a query using a 'server change token'. The first time you can query it with nil and then retrieve all the records. you would get a 'server change token' in return. just save that on the device and the next time you start the app just use that token. Then there would be no need to log in to iCloud. (if your data is public)

send message to loggded In user using parse

I have created a database on parse.com
I want to send push notification to logged in user
I have created "Installation" class.
in that class I have added two devices with device token.
and after logged in app I have added field "owner" with loginId.
After that I am trying to send push notification to logged in used from my device but my code not working
here is code of send button
- (IBAction)WRMethodBtnSendMsg:(id)sender {
// Send a notification to all devices subscribed to the "Giants" channel.
PFQuery *tmpQuery = [PFInstallation query];
[tmpQuery whereKey:#"owner" containsString:[self.sdPFObject objectId]];
PFPush *push = [[PFPush alloc] init];
[push setQuery:tmpQuery];
[push setMessage:self.wrTxtMsg.text];
[push sendPushInBackground];
}
but push notification doesn't send
Appreciate for help
I'd use channels. Have every user subscribe to a "user channel", something along the line of user_*user_id_here*. Then, when you want to send a push notification to the user with ID 123456, simply send it to the user_123456 channel.

PFInstallation and adding a pointer to a PFUser

I am trying to get to grips with Parse and build a simple chat app. For my login and signup code I have this snippet:
PFInstallation *installation = [PFInstallation currentInstallation];
installation[#"user"] = [PFUser currentUser];
[installation saveInBackground];
This code connects a PFInstallation to a PFUser so a push can be sent by querying a username.
When the app loads I first check to see if there is already a user logged in:
if ([PFUser currentUser]) {
[self performSegueWithIdentifier:#"showFriends" sender:nil];
}
if a user is already logged in the show friends view controller is loaded. Do I need to set the installation again in this code to match the user? i.e.
if ([PFUser currentUser]) {
PFInstallation *installation = [PFInstallation currentInstallation];
[installation[#"user"] = [PFUser currentUser];
[installation saveInBackground];
[self performSegueWithIdentifier:#"showFriends" sender:nil];
}
Or is there no need because the user is already logged in? Am I right in thinking that the installation file is UNIQUE and only created once, matching the device to the push service so nothing really changes in that file unless I want to update the PFUser field I added?
thanks
if a user is already logged in the show friends view controller is loaded. Do I need to set the installation again in this code to match the user?
No. Installations and User classes act independently, but in your case since you set a relation then they can act together as well. Since you already set it in application didFinishLaunchingWithOptions: that device has uniquely identified its installation with the token you've provided (device token) so you don't have to call it again.
A User session is different. If you want the User to be logged in you will have to present the login VC somewhere, since it won't be there the first launch.
Am I right in thinking that the installation file is UNIQUE and only created once, matching the device to the push service so nothing really changes in that file unless I want to update the PFUser field I added? thanks
Yes. That's pretty accurate. Just don't get confused. PFUser currentUser is not the same as PFInstallation currentInstallation anyone can sign on to a device but the app can only be installed once on a device making the installation unique. Not users.

Sending push notification to a particular user in Parse

I'm having a devil of a time trying to do what seems like the simplest possible thing: I want to send a push notification to a particular user, and I already have the PFUser object for that user.
I tried the following:
// 'recipient' is the PFUser
PFQuery *pushQuery = [PFInstallation query];
[pushQuery whereKey:#"owner" equalTo:recipient];
I also tried replacing "owner" with "user". In both cases, the push seems to succeed (no error reported) but the device never gets the notification.
I know that the device is properly registered and logged in because I can send pushe notifications from the Parse web console.
What's the right way to do this?
Thanks,
Frank
I ended up using a nested query to get this.
var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo('objectId', recipient);
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.matchesQuery('user', userQuery);
Ok, it turns out that this is a two-step process.
When the user logs in or creates an account, you have to put a "user" property in PFInstallation:
[[PFInstallation currentInstallation] setObject:[PFUser currentUser] forKey:#"user"];
[[PFInstallation currentInstallation] saveEventually];
Only then will the query work.
That seems like a lot of manual work for something that the Parse server should know without my having to set it up.

Resources