Peril
Peril

Reputation: 1599

Sending push notification using parse using query

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

Upvotes: 0

Views: 4130

Answers (1)

ray1
ray1

Reputation: 4058

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();

Upvotes: 6

Related Questions