2
votes

I have used Parse for sending and receiving the Push notifications. I have minutely followed the tutorial and implemented it successfully. Now I am required to send the notification to user within certain miles selected by users.

For Eg : If the user select 100, then the notification should be sent to all the users who have installed my app and are within 100 miles from the user's (User who is broadcasting the notification) current location.

I found a code snippet on Parse's website regarding the same but i am not receiving any notification if i use the second query which is commented in the following code The method didReceiveRemoteNotification is not called if i use the second query .Only if i use the first query i am successfully receiving the notification.

I am fresher into Push notifications and Parse. Let me know if i am missing something silly so that i can fix it up.I have been revolving around this issue for a very long time so guys please help.

PFGeoPoint *myLoc = [PFGeoPoint geoPointWithLatitude:myLatitude longitude:myLongitude];

        PFQuery *userQuery = [PFUser query];
        [userQuery whereKey:@"location"
               nearGeoPoint:myLoc
                withinMiles:100];

        PFQuery *myQuery = [PFInstallation query];
        [myQuery whereKey:@"deviceType" equalTo:@"ios"];
        //[myQuery whereKey:@"user" matchesQuery:userQuery];


        NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                              txt.text, @"alert",
                              @"1", @"badge",
                              @"sound.caf", @"sound",
                              nil];


        PFPush *push = [[PFPush alloc] init];
        [push setQuery:myQuery];
        [push setData:data];
        [push sendPushInBackground];
1
'location' in the user table is a geo point correct? And you have a 'user' column in the installation table? - Jacob
In Location column what you put latitude or longitude share that value - Maul
@Jacob : I am afraid i don't have a table on my backend.Its just the query i saw and implemented it as it is. - iCodeAtApple
@Maul : I am sorry i never knew anything about the table stuff ... - iCodeAtApple
you working with parse right than you have a signup process means it creates User Table in parse. and if you want to build notification process than you have to go in settings of parse and check Client Push Enabled is ON and you have to upload push enabled certificate - Maul

1 Answers

5
votes

I was did this same thing in one of the my project hope that following code and steps is helpful for you.

First In your Appdelgate's Method didRegisterForRemoteNotificationsWithDeviceToken put Following code snippet

[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
        if (!error) {
            NSLog(@"User is currently at %f, %f", geoPoint.latitude, geoPoint.longitude);
            PFInstallation *currentInstallation = [PFInstallation currentInstallation];
            [currentInstallation setDeviceTokenFromData:deviceToken];
            currentInstallation[@"location"] = geoPoint;
            [currentInstallation saveInBackground];
        } else { 
            NSLog(@"%@", error); 
            return; 
        } 
    }];

And put following code snippet from where you want to send notification to others

PFInstallation *installation = [PFInstallation currentInstallation];
    NSLog(@"%@",installation);
    [PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error)
     {
         if (!error)
         {
             PFQuery *userQuery = [PFInstallation query];
             [userQuery whereKey:@"location"
                    nearGeoPoint:geoPoint
                     withinMiles:100.00];
             NSString *str_CurrentDeviceToken = [NSString stringWithFormat:@"%@",[installation objectForKey:@"deviceToken"]];
             [userQuery whereKey:@"deviceToken" notEqualTo:str_CurrentDeviceToken];
             NSLog(@"%@",userQuery);


             NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                                   @"Hello", @"alert",
                                   @"1", @"badge",
                                   @"", @"sound",
                                   nil];


             PFPush *push = [[PFPush alloc] init];
             [push setQuery:userQuery];
             [push setData:data];
             [push sendPushInBackground];
         }
         else
         {
             NSLog(@"%@", error);
             return;
         }
     }];