【问题标题】:How to send Parse push notification using PHP to specific channel?如何使用 PHP 向特定频道发送 Parse 推送通知?
【发布时间】:2016-01-07 00:23:46
【问题描述】:

如何使用 php 代码通过 Parse.com 推送通知。我想将此推送到特定定义的频道,即 subscribeInBackground。

如果有人给出一个快速的解决方案,那对我来说真的很有帮助。 我参考了下面的链接,但它没有帮助。

【问题讨论】:

  • 请粘贴您目前尝试过的代码。

标签: php android


【解决方案1】:

在服务器端 (php) 中使用此代码(它适用于我)

function sendPush($message,$email)
{
$APPLICATION_ID = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";  //put your identifiers here
$REST_API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";  // and here 

$url = 'https://api.parse.com/1/push';

$data = array(
    // 'where' => '{}',     //uncomment this line to send push to all users
    'where' => array(     // send to specific user via email
        'email' => $email
    ),

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

$result = curl_exec($curl);
echo $result;
}

在客户端 (android) 中使用此代码通过电子邮件注册和识别每个用户:

public static void subscribeWithEmail(String email) {
    ParseInstallation installation = ParseInstallation.getCurrentInstallation();
    installation.put("email", email);
    installation.saveInBackground();
}

然后...当您在服务器中调用 sendPush 函数时,它将通过传递的电子邮件和消息向特定用户发送消息到 sendPush 函数...

你想要特定的频道是吗?在 php 代码中替换它...

 $data = array(
    'channel' => 'yourChannelName',
    'data' => array(
        'alert' => $message,
    ),
);

并在 android 中使用此代码订阅频道:

ParsePush.subscribeInBackground("yourChannelName", new SaveCallback() {
        @Override
        public void done(ParseException e) {
            Log.d("LOG","successfully subscribed to channel :)"); 
        }
    });

【讨论】:

    猜你喜欢
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多