简体   繁体   中英

Receive push notifications sent out within a certain range Parse

I'm building a parse app and I want the user to be able to set the range in which they receive notifications from other users in. I know you can choose how far you want to send the notification with something like this

// Find users near a given location
ParseQuery userQuery = ParseUser.getQuery();
userQuery.whereWithinMiles("location", stadiumLocation, 1.0)

// Find devices associated with these users
ParseQuery pushQuery = ParseInstallation.getQuery();
pushQuery.whereMatchesQuery("user", userQuery);

// Send push notification to query
ParsePush push = new ParsePush();
push.setQuery(pushQuery); // Set our Installation query
push.setMessage("Free hotdogs at the Parse concession stand!");
push.sendInBackground();

But if the user receiving these notifications has their receiving range set as .5 miles or something like that I dont wan't them to receive it. Is there anyway to make this happen?

Ex: User A has a radius set to 1 mile and user B sends the message to everyone within 2 miles and User A is 1.5 miles away from User B so I don't want him to receive it.

Ex: User A has a radius set to 1 mile and user B sends message to everyone within 2 miles and User A is .5 miles away from User B he will receive that message and notification.

The second example is easy to implement its the first one that I'm not sure how to do.

Your code is working, but you need to make "SignUp" and "Login" on each device and put location to each loggined user I do it like that but it is not properly, but working: add it to your MyApplication.class extands Application :

            Parse.enableLocalDatastore(this);
            Parse.initialize(this, "TWRSFDFayfTlDsiiELyX37crtgezMs0DeQ5IXTdx", "ukuYgUJhhVybV4MtmlkKqtbkj2drEWjl5RJgpqYl");
            ParseInstallation.getCurrentInstallation().saveInBackground();
          // Also in this method, specify a default Activity to handle push notifications
            PushService.setDefaultPushCallback(this, MainActivity.class);


            String  android_id = Secure.getString(getApplicationContext().getContentResolver(),Secure.ANDROID_ID);         
            Log.e("LOG","android id >>" + android_id);


            ParseInstallation installation = ParseInstallation.getCurrentInstallation();
            installation.put("UniqueId",android_id);

            installation.saveInBackground();

            String installationID = installation.getInstallationId();


            ParseUser.logInInBackground(installationID, installationID, new LogInCallback() {
                  public void done(ParseUser user, ParseException e) {
                    if (user != null) {
                        Log.e(tag, "Logigned 1");
                        addCurrentLocation();
                    } else {

                        ParseInstallation installation = ParseInstallation.getCurrentInstallation();
                        String installationID = installation.getInstallationId();
                        user = new ParseUser();
                        user.setUsername(installationID);
                        user.setPassword(installationID);
                        user.setEmail(installationID+"@example.com");

                        // other fields can be set just like with ParseObject
//                      user.put("phone", "650-253-0000");

                        user.signUpInBackground(new SignUpCallback() {
                          public void done(ParseException e) {
                            if (e == null) {
                                Log.e(tag, "signUp 1");
                                ParseInstallation installation = ParseInstallation.getCurrentInstallation();
                                String installationID = installation.getInstallationId();
                                ParseUser.logInInBackground(installationID, installationID, new LogInCallback() {
                                      public void done(ParseUser user, ParseException e) {
                                        if (user != null) {
                                            Log.e(tag, "Logigned 2");
                                            addCurrentLocation();
                                        } else {
                                            Log.e(tag, "CANT LOGIN");
                                        }
                                      }
                                    });
                            } else {
                                Log.e(tag, "signUp error 2");
                            }
                          }
                        });
                    }
                  }
                });

after first run of app each device will be logigned with username= installationID and password = installationID you will see it on Core->Data->User Doasboard on website after logining you need to set to each user his location simply method addCurrentLocation();

void addCurrentLocation(){
        ParseGeoPoint point = new ParseGeoPoint(12, 12);
        ParseInstallation parseInstallation  = ParseInstallation.getCurrentInstallation();;
        parseInstallation.put("user",
                ParseUser.getCurrentUser());
        ParseUser u= ParseUser.getCurrentUser() ;
        u.put("location", point);
        u.put("application", getString(R.string.app_name));
        ParseGeoPoint userlocation =  u.getParseGeoPoint("location");
        Log.e(tag , 
                    "user current location: "
                    +userlocation.getLatitude()
                    +" : "+ 
                    userlocation.getLongitude());
        u.saveInBackground();

    }

and then post your push notification :

        // Find users near a given location
        ParseQuery userQuery = ParseUser.getQuery();
        ParseGeoPoint stadiumLocation = new ParseGeoPoint(12.1,12.1);
        userQuery.whereWithinMiles("location", stadiumLocation , 1.0);

        // Find devices associated with these users
        ParseQuery pushQuery = ParseInstallation.getQuery();
        pushQuery.whereMatchesQuery("user", userQuery);

        // Send push notification to query
        ParsePush push = new ParsePush();
        push.setQuery(pushQuery); 

        push.setMessage("Only users near 12,12 will recieve this push notification");
        push.sendInBackground();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM