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,609
  • 14
  • 33
Peril
  • 1,569
  • 6
  • 25
  • 42
  • `parseQuery` has nothing to do with parsing, does it? But `parse.com` exists as a tag. – Daniel W. Feb 10 '14 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 https://github.com/apotropaic/parse.com-php-library – Peril Feb 10 '14 at 18:48
  • Peril, check my updated answer below. I've modified the code. – steve Feb 10 '14 at 20:40

1 Answers1

6

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();
Community
  • 1
  • 1
steve
  • 3,878
  • 7
  • 34
  • 49
  • 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 – Peril Feb 10 '14 at 18:53
  • Ok, I've updated $data to accept the `where` field you specified in the question. Can you try it now? – steve Feb 10 '14 at 19:05
  • Peril, I've updated the answer. Please check my new code – steve Feb 11 '14 at 06:13
  • Thanks steve for your answer, I am looking to use the sdk not the curl – Peril Feb 12 '14 at 09:37
  • 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. – steve Feb 12 '14 at 12:36