2015-12-30 68 views

回答

0

在服務器端(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; 
} 

,並在客戶端(安卓)使用此代碼通過電子郵件註冊和識別每個用戶:

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 :)"); 
     } 
    }); 
相關問題