0

I want to send push notification using query for example

I have this query

 $ages = array(18,19,20,21)
 $parseQuery = new parseQuery('_User');
 $parseQuery->whereContainedIn('age', $ages);
 $result = $parseQuery->find();

and I want to send them a push notification Should I use the Installation or is theres another way ? Thanks

UPDATE:

I am using this sdk https://github.com/apotropaic/parse.com-php-library

from the code I am returning list of people I am going to send them notification I want to send the push notifications to the _User class

scenia
1,62914 silver badges33 bronze badges
asked Feb 8, 2014 at 16:55
3
  • parseQuery has nothing to do with parsing, does it? But parse.com exists as a tag. Commented Feb 10, 2014 at 18:35
  • This is just a query, it return all the users with ages 18, 19, 20 or 21, I want to send them push notification I am using this sdk github.com/apotropaic/parse.com-php-library Commented Feb 10, 2014 at 18:48
  • Peril, check my updated answer below. I've modified the code. Commented Feb 10, 2014 at 20:40

1 Answer 1

6
+25

It looks like you're using parseQuery to retrieve a user with the age constraints you specified. Once you have the user data in your $result, you can use curl to send push notifications, according to the docs.

Here's a bit of code I found on the web, it should be simple to adopt it to your existing code to send a push notification to the user you've retrieved.

<?php
$APPLICATION_ID = "xxxxxxxx";
$REST_API_KEY = "xxxxxxxxx";
$MESSAGE = "your-alert-message";
$url = 'https://api.parse.com/1/push';
$data = array(
 'channel' => '',
 'type' => 'android',
 'expiry' => 1451606400,
 'where' => array(
 'age' => array('$in' => array(18,19,20,21))
 ),
 'data' => array(
 'alert' => 'greetings programs',
 ),
);
$_data = json_encode($data);
$headers = array(
 'X-Parse-Application-Id: ' . $APPLICATION_ID,
 'X-Parse-REST-API-Key: ' . $REST_API_KEY,
 'Content-Type: application/json',
 'Content-Length: ' . strlen($_data),
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_exec($curl);

It's worth noting that you need php-curl for this to work. It's very easy to install or enable if you don't already have it. Alternatively you can use file_get_contents to achieve he same thing if you want to use something out of the box.

Edit: I've updated the $data array with the constraints you defined in the question. You don't need to use parseQuery to find users with the updated code. You're effectively telling Parse to apply the constraints with the call.

Update 2: I haven't tested this code but the library makes it very straightforward how to setup in the README. The library is just using curl underneath so you can use whichever is more convenient.

$parse = new parsePush('Sample push message');
$parse->where = array(
 'age' => array('$in' => array(18,19,20,21))
);
//create acl
$parse->ACL = array("*" => array("write" => true, "read" => true));
$r = $parse->send();
answered Feb 10, 2014 at 18:50
Sign up to request clarification or add additional context in comments.

5 Comments

Theres no place I can tell the push notification to send it to the $result. the code you added will send to all the users
Ok, I've updated $data to accept the where field you specified in the question. Can you try it now?
Peril, I've updated the answer. Please check my new code
Thanks steve for your answer, I am looking to use the sdk not the curl
I've updated the code again but I think you should at least upvote this answer or mark it as the answer because it achieves what you're looking to do (in one or another way). If you have specific questions I can help you out.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.