【问题标题】:Push notification with Parse & PHP使用 Parse 和 PHP 推送通知
【发布时间】:2015-09-21 20:49:49
【问题描述】:

我有一个从 PARSE.COM 接收推送通知并且工作正常的 Android 应用程序,现在我想从 PHP 发送通知并且我收到了这条消息... {"code":107,"error":"Invalid JSON: m01011205"}

怎么了,你有什么想法吗?

这是我的 PHP 代码

   <?php

  $APPLICATION_ID = "my application id";
  $REST_API_KEY = "my api key";
  $MESSAGE = "Notificación de Prueba";

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(
            "channels" => "m01011205",
            "deviceType" => 'android',
            "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>

【问题讨论】:

  • 尝试将"更改为'这里"channels" =&gt; 'm01011205'
  • 嗨....我也遇到了同样的错误
  • 通过将" 更改为' ??
  • 正确,我把"改成了'
  • 不....我也遇到了同样的错误

标签: php parse-platform push


【解决方案1】:

我通常使用 Parse 框架来发送推送通知,因为它有一个 SDK 和示例。

https://parse.com/docs/php/guide#push-notifications

例如:

// Find users near a given location
$userQuery = ParseUser::query();
$userQuery->withinMiles("location", $stadiumLocation, 1.0);

// Find devices associated with these users
$pushQuery = ParseInstallation::query();
$pushQuery->matchesQuery('user', $userQuery);

// Send push notification to query

ParsePush::send(array(
  "where" => $pushQuery,
  "data" => array(
    "alert" => "Free hotdogs at the Parse concession stand!"
  )
));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多