[英]How to send Parse push notification using PHP to specific channel?
在服务器端(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 :)");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.