我想通过获取设备令牌向ios设备发送推送通知,到目前为止我已经在yii中执行了此操作:
public function actionPushtest(){
$token=$_REQUEST['token'];
$message = 'Hello';
$badge = 1;
$sound = 'default';
$development = true;
$passphrase='pass';
$payload = array();
$payload['aps'] = array('alert' => $message, 'badge' => intval($badge), 'sound' => $sound);
$payload = json_encode($payload);
$apns_url = NULL;
$apns_cert = NULL;
$apns_port = 2195;
if($development)
{
$apns_url = 'gateway.sandbox.push.apple.com';
$apns_cert = dirname(Yii::app()->request->scriptFile).'/file.pem';
}
else
{
$apns_url = 'gateway.push.apple.com';
$apns_cert = dirname(Yii::app()->request->scriptFile).'/file.pem';
}
$stream_context = stream_context_create();
stream_context_set_option($stream_context, 'ssl', 'local_cert', $apns_cert);
stream_context_set_option($stream_context, 'ssl', 'passphrase', $passphrase);
$apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);
$device_tokens= str_replace("<","",$token);
$device_tokens1= str_replace(">","",$device_tokens);
$device_tokens2= str_replace(' ', '', $device_tokens1);
$apns_message = chr(0) . chr(0) . chr(32) . pack('H*', $device_tokens2 /*str_replace(' ', '', $device_tokens1)*/) . chr(0) . chr(strlen($payload)) . $payload;
$msg=fwrite($apns, $apns_message);
if (!$msg){
echo 'Message not delivered' . PHP_EOL;
}else{
echo 'Message successfully delivered' . PHP_EOL;
}
@socket_close($apns);
@fclose($apns);
}
..我没有收到任何错误,但没有收到通知。我做错了什么?
答案 0 :(得分:2)
我最终发现了我的错误,问题出现在pem file
,虽然没有显示错误,并且在应用开启时也没有生成推送通知,您需要将其最小化。对于遇到此问题的其他人,请确保您的pem file
100%正常。并感谢其他人,他们不遗余力地帮助我解决问题。
这是代码:
public function actionPushtest(){
$token=$_REQUEST['token'];
$message = 'Hello';
$badge = 1;
$sound = 'default';
$development = true;//make it false if it is not in development mode
$passphrase='pass';//your passphrase
$payload = array();
$payload['aps'] = array('alert' => $message, 'badge' => intval($badge), 'sound' => $sound);
$payload = json_encode($payload);
$apns_url = NULL;
$apns_cert = NULL;
$apns_port = 2195;
if($development)
{
$apns_url = 'gateway.sandbox.push.apple.com';
$apns_cert = dirname(Yii::app()->request->scriptFile).'/file.pem';
}
else
{
$apns_url = 'gateway.push.apple.com';
$apns_cert = dirname(Yii::app()->request->scriptFile).'/file.pem';
}
$stream_context = stream_context_create();
stream_context_set_option($stream_context, 'ssl', 'local_cert', $apns_cert);
stream_context_set_option($stream_context, 'ssl', 'passphrase', $passphrase);
$apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);
$device_tokens= str_replace("<","",$token);
$device_tokens1= str_replace(">","",$device_tokens);
$device_tokens2= str_replace(' ', '', $device_tokens1);
$apns_message = chr(0) . chr(0) . chr(32) . pack('H*', $device_tokens2) . chr(0) . chr(strlen($payload)) . $payload;
$msg=fwrite($apns, $apns_message);
if (!$msg){
echo 'Message not delivered' . PHP_EOL;
}else{
echo 'Message successfully delivered' . PHP_EOL;
}
@socket_close($apns);
@fclose($apns);
}
答案 1 :(得分:0)
在您输入消息之前,请检查$apns
是否为false。
如果是错误的,请检查$error_string
是否有任何错误。
如果您遇到任何错误,那就是错误。
如果未创建流,则会在false上执行fwrite,这只会返回警告而不是错误:
我会提出这个改变:
if (!apns) {
Yii::app()->user->setFlash('error', "Apple Push failed!");
Yii::log($error_string, 'error', 'myController.actionPushtest');
} else {
$device_tokens = str_replace("<", "", $token);
$device_tokens1 = str_replace(">", "", $device_tokens);
$device_tokens2 = str_replace(' ', '', $device_tokens1);
$apns_message = chr(0) . chr(0) . chr(32) . pack(
'H*',
$device_tokens2 /*str_replace(' ', '', $device_tokens1)*/
) . chr(0) . chr(strlen($payload)) . $payload;
$msg = fwrite($apns, $apns_message);
if (!$msg) {
echo 'Message not delivered' . PHP_EOL;
} else {
echo 'Message successfully delivered' . PHP_EOL;
}
@socket_close($apns);
@fclose($apns);
}