3

I've use Parse to give my iOS and Android App notifications. But I want to do that from my website.

I've found this in de docs of Parse:

To send a push notification, send a POST request to https://api.parse.com/1/push with the Content-Type header set to application/json. A simple alert can be sent to Android devices on the global broadcast channel using the following command:

curl -X POST \

-H "X-Parse-Application-Id: ${APPLICATION_ID}" \

-H "X-Parse-REST-API-Key: ${REST_API_KEY}" \

-H "Content-Type: application/json" \

-d '{ "channel": "", \

    "type": "android", \
    "expiry": 1451606400, \
    "data": { "alert": "greetings programs" } }' \

https://api.parse.com/1/push

Who can help me to make a PHP-file to post this ? Thanks!

Rick de Jong
  • 237
  • 1
  • 4
  • 12

2 Answers2

13

Translating your command line curl to PHP you get something along the lines of

<?php
$url = 'https://api.parse.com/1/push';
$data = array(
    'channel' => '',
    'type' => 'android',
    'expiry' => 1451606400,
    '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);

UPDATE

<?php

$APPLICATION_ID = "your-app-id";
$REST_API_KEY = "your-api-key";
$MESSAGE = "your-alert-message";

if (!empty($_POST)) {

    $errors = array();
    foreach (array('app' => 'APPLICATION_ID', 'api' => 'REST_API_KEY', 'body' => 'MESSAGE') as $key => $var) {
        if (empty($_POST[$key])) {
            $errors[$var] = true;
        } else {
            $$var = $_POST[$key];
        }
    }

    if (!$errors) {
        $url = 'https://api.parse.com/1/push';
        $data = array(
            'channel' => '',
            'type' => 'android',
            'expiry' => 1451606400,
            'data' => array(
                'alert' => $MESSAGE,
            ),
        );
        $_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_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($curl);
    }
}
?><!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Parse API</title>
</head>
<body>
    <?php if (isset($response)) {
        echo '<h2>Response from Parse API</h2>';
        echo '<pre>' . htmlspecialchars($response) . '</pre>';
        echo '<hr>';
    } elseif ($_POST) {
        echo '<h2>Error!</h2>';
        echo '<pre>';
        var_dump($APPLICATION_ID, $REST_API_KEY, $MESSAGE);
        echo '</pre>';
    } ?>

    <h2>Send Message to Parse API</h2>
    <form id="parse" action="" method="post" accept-encoding="UTF-8">
        <p>
            <label for="app">APPLICATION_ID</label>
            <input type="text" name="app" id="app" value="<?php echo htmlspecialchars($APPLICATION_ID); ?>">
        </p>
        <p>
            <label for="api">REST_API_KEY</label>
            <input type="text" name="api" id="api" value="<?php echo htmlspecialchars($REST_API_KEY); ?>">
        </p>
        <p>
            <label for="api">REST_API_KEY</label>
            <textarea name="body" id="body"><?php echo htmlspecialchars($REST_API_KEY); ?></textarea>
        </p>
        <p>
            <input type="submit" value="send">
        </p>
    </form>
</body>
</html>

With this, your unstated question should be answered. If you still can't figure out how to do this, you should seriously consider learning yourself some webdev or switch jobs. This is the most basic thing you can do.

rodneyrehm
  • 13,442
  • 1
  • 40
  • 56
  • Application ID en REST API Key are not variable. So what can I do that I get an form to make the "Alert" variable with an Textbox? – Rick de Jong Feb 25 '12 at 12:56
  • Excuse me? If you want to use that Parse API thing, you've probably registered with them, right? If so, they assigned you an APPLICATION_ID and gave you a corresponding REST_API_KEY. Just insert the values you've got. – rodneyrehm Feb 25 '12 at 12:59
  • Yes, but I want an Textbox on the page to make the change for the "data" field – Rick de Jong Feb 25 '12 at 13:02
  • Your question said nothing about a textbox. have a look at the [Variables From External Sources](http://www.php.net/manual/en/language.variables.external.php) section in the PHP docs. It's dead simple. – rodneyrehm Feb 25 '12 at 13:08
  • Okay, I must change "$APPLICATION_ID" to: $xxxxxxxxxx and "greeting program" to $text ? – Rick de Jong Feb 25 '12 at 13:10
  • Wow. you are not seriously considering having the APPLICATION_ID posted to your script? APPLICATION_ID and REST_API_KEY are information that should NEVER be visible to / editable by a user. It's bad enough you're considering having users send arbitrary info to a mobile device. Have you thought about this form being abused - at all? – rodneyrehm Feb 25 '12 at 13:16
  • I want to change it for my company – Rick de Jong Feb 25 '12 at 13:48
  • I've updated my answer and provided an example for you regarding the integration into a form. If that doesn't help, I don't know what will. – rodneyrehm Feb 25 '12 at 14:50
  • If I run the above code, the API responds with `{"error":"unauthorized"}`, which could be expected since `your-app-id` is certainly no valid application id. – rodneyrehm Feb 26 '12 at 15:30
  • The message field has the wrong ID, should be: – NoelHunter Feb 06 '14 at 16:38
  • I am using JSP for WEB APP. what should i do to implement Parse-push notifiation in JSP-SPRING framework? – Ram Mansawala Nov 21 '15 at 18:39
0

For me it look like you have to do a simple HTTP request in json format. You may do it in javascript via ajax or by using curl extension in php.

'-H' is probably header, -d stands for data in json format.

jszoja
  • 339
  • 3
  • 7
  • And how can I make a form for that? That I have a textfield for the "Alert" data? – Rick de Jong Feb 25 '12 at 12:39
  • I would rather use some javascript library like jquery to fire ajax when you click on some button. It might look like: jQyery('#buttonElId').click( $.ajax() ); Read jquery doc about ajax params: where to put headers and data. I believe that it could be in {} block: $.ajax({ url: yourapi, headers:'some headers', 'data: 'contents od -d line' }) – jszoja Feb 25 '12 at 12:40
  • Haha, Thats Chinese for me? :-p – Rick de Jong Feb 25 '12 at 12:45
  • not sure about it: http://blogfreakz.com/jquery/jquery-toastmessage-android-like-notification/ – jszoja Feb 25 '12 at 12:54
  • Thats not the Push Notification that I mean :) – Rick de Jong Feb 25 '12 at 12:59