Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 未收到Phonegap iOS推送通知_Php_Ios_Cordova_Push Notification - Fatal编程技术网

Php 未收到Phonegap iOS推送通知

Php 未收到Phonegap iOS推送通知,php,ios,cordova,push-notification,Php,Ios,Cordova,Push Notification,我们管理iOS和Android平台的推送通知。Android的程序很好,设备已经注册到GCM,并且收到了通知。问题在于iOS的APN,没有收到通知!即使设备已正确注册,APNS也为设备生成了令牌 下面是接收推送的Javascript和发送消息的PHP代码 接收推送的Javascript代码: var pushNotification; document.addEventListener("deviceready", onDeviceReadyEvent, false); function on

我们管理iOS和Android平台的推送通知。Android的程序很好,设备已经注册到GCM,并且收到了通知。问题在于iOS的APN,没有收到通知!即使设备已正确注册,APNS也为设备生成了令牌

下面是接收推送的Javascript和发送消息的PHP代码

接收推送的Javascript代码:

var pushNotification;
document.addEventListener("deviceready", onDeviceReadyEvent, false);

function onDeviceReadyEvent(){
pushNotification = window.plugins.pushNotification;

var sk_deviceplatform = device.platform;
sk_deviceplatform = sk_deviceplatform.toLowerCase();

if(sk_deviceplatform === 'android'){
pushNotification.register(successHandler, errorHandler, {"senderID":"XXXXXXXXX","ecb":"onNotificationGCM"});
} else {
pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"});
}
}

function tokenHandler(result) {
    console.log("Token: " + result);
    alert("Token: "+ result);
}

function errorHandler(error) {
    console.log("Error: " + error); 
    alert('Error:' + error);
}

    function onNotificationAPNS(e){
    if(e.alert.title) {
    $.mobile.changePage( "handle_notifications.html?id="+e.eventid, { transition: "slide"} );
    }
    if(e.sound) {
    var skpn_snd = new Media(e.sound);
    skpn_snd.play();
    }
    if (e.badge) {
    pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, e.badge);
    }
    if (e.foreground===0){
    // when application is not active
          }else{
    navigator.notification.alert(e.alert.title, null, 'News Notification', 'OK');          
          }
}
/*** PUSH NOTIFICATION FOR IOS VIA APNS ***/

set_time_limit(0);
// charset header for output
header('content-type: text/html; charset: utf-8');
   $deviceIds = array(/* get all devices token ids from the database */);
if(count($deviceIds)>0){
// this is where you can customize your notification

$body['aps'] = array(
    'badge' => +1,
    'alert' => "News Event!",
    'sound' => 'default'
);

$payload = json_encode($body);
////////////////////////////////////////////////////////////////////////////////
// start to create connection
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', "XXXX.pem");
stream_context_set_option($ctx, 'ssl', 'passphrase', "XXXXXXX");
foreach ($deviceIds as $item_device) {    
// Open a connection to the APNS server
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
if(!$fp){ exit("Failed to connect: $err $errstr" . '<br />');}else{/* service online */}
// Build the binary notification
$msg_notification = chr(0) . pack('n', 32) . pack('H*', $item_device) . pack('n', strlen($payload)) . $payload;     
// Send it to the server
$result = fwrite($fp, $msg_notification, strlen($msg_notification));     
if (!$result) { echo 'Undelivered message count: ' . $item_device . '<br />';}
else { /* notifications are sent */ }
if ($fp){
     ## check for errors
     $apple_error_response = fread($fp, 6); //byte1=always 8, byte2=StatusCode, bytes3,4,5,6=identifier(rowID). Should return nothing if OK.
       //NOTE: Make sure you set stream_set_blocking($fp, 0) or else fread will pause your script and wait forever when there is no response to be sent.
       if ($apple_error_response) {
            $error_response = unpack('Ccommand/Cstatus_code/Nidentifier', $apple_error_response); //unpack the error response (first byte 'command" should always be 8)
            if ($error_response['status_code'] == '0') {
                $error_response['status_code'] = '0-No errors encountered';

            } else if ($error_response['status_code'] == '1') {
                $error_response['status_code'] = '1-Processing error';

            } else if ($error_response['status_code'] == '2') {
                $error_response['status_code'] = '2-Missing device token';

            } else if ($error_response['status_code'] == '3') {
                $error_response['status_code'] = '3-Missing topic';

            } else if ($error_response['status_code'] == '4') {
                $error_response['status_code'] = '4-Missing payload';

            } else if ($error_response['status_code'] == '5') {
                $error_response['status_code'] = '5-Invalid token size';

            } else if ($error_response['status_code'] == '6') {
                $error_response['status_code'] = '6-Invalid topic size';

            } else if ($error_response['status_code'] == '7') {
                $error_response['status_code'] = '7-Invalid payload size';

            } else if ($error_response['status_code'] == '8') {
                $error_response['status_code'] = '8-Invalid token';

            } else if ($error_response['status_code'] == '255') {
                $error_response['status_code'] = '255-None (unknown)';

            } else {
                $error_response['status_code'] = $error_response['status_code'].'-Not listed';
            }
            echo '<br><b>ERROR</b> Response Command:<b>' . $error_response['command'] . '</b>&nbsp;&nbsp;&nbsp;Identifier:<b>' . $error_response['identifier'] . '</b>&nbsp;&nbsp;&nbsp;Status:<b>' . $error_response['status_code'] . '</b><br>';
            echo 'Identifier is the rowID (index) in the database that caused the problem, and Apple will disconnect you from server. To continue sending Push Notifications, just start at the next rowID after this Identifier.<br>';
       }

fclose($fp);
$_ENV['connection_status'] = 'The connection has been closed by the client' . '<br />';
}
} 
set_time_limit(30);
}
发送推送的PHP代码:

var pushNotification;
document.addEventListener("deviceready", onDeviceReadyEvent, false);

function onDeviceReadyEvent(){
pushNotification = window.plugins.pushNotification;

var sk_deviceplatform = device.platform;
sk_deviceplatform = sk_deviceplatform.toLowerCase();

if(sk_deviceplatform === 'android'){
pushNotification.register(successHandler, errorHandler, {"senderID":"XXXXXXXXX","ecb":"onNotificationGCM"});
} else {
pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"});
}
}

function tokenHandler(result) {
    console.log("Token: " + result);
    alert("Token: "+ result);
}

function errorHandler(error) {
    console.log("Error: " + error); 
    alert('Error:' + error);
}

    function onNotificationAPNS(e){
    if(e.alert.title) {
    $.mobile.changePage( "handle_notifications.html?id="+e.eventid, { transition: "slide"} );
    }
    if(e.sound) {
    var skpn_snd = new Media(e.sound);
    skpn_snd.play();
    }
    if (e.badge) {
    pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, e.badge);
    }
    if (e.foreground===0){
    // when application is not active
          }else{
    navigator.notification.alert(e.alert.title, null, 'News Notification', 'OK');          
          }
}
/*** PUSH NOTIFICATION FOR IOS VIA APNS ***/

set_time_limit(0);
// charset header for output
header('content-type: text/html; charset: utf-8');
   $deviceIds = array(/* get all devices token ids from the database */);
if(count($deviceIds)>0){
// this is where you can customize your notification

$body['aps'] = array(
    'badge' => +1,
    'alert' => "News Event!",
    'sound' => 'default'
);

$payload = json_encode($body);
////////////////////////////////////////////////////////////////////////////////
// start to create connection
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', "XXXX.pem");
stream_context_set_option($ctx, 'ssl', 'passphrase', "XXXXXXX");
foreach ($deviceIds as $item_device) {    
// Open a connection to the APNS server
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
if(!$fp){ exit("Failed to connect: $err $errstr" . '<br />');}else{/* service online */}
// Build the binary notification
$msg_notification = chr(0) . pack('n', 32) . pack('H*', $item_device) . pack('n', strlen($payload)) . $payload;     
// Send it to the server
$result = fwrite($fp, $msg_notification, strlen($msg_notification));     
if (!$result) { echo 'Undelivered message count: ' . $item_device . '<br />';}
else { /* notifications are sent */ }
if ($fp){
     ## check for errors
     $apple_error_response = fread($fp, 6); //byte1=always 8, byte2=StatusCode, bytes3,4,5,6=identifier(rowID). Should return nothing if OK.
       //NOTE: Make sure you set stream_set_blocking($fp, 0) or else fread will pause your script and wait forever when there is no response to be sent.
       if ($apple_error_response) {
            $error_response = unpack('Ccommand/Cstatus_code/Nidentifier', $apple_error_response); //unpack the error response (first byte 'command" should always be 8)
            if ($error_response['status_code'] == '0') {
                $error_response['status_code'] = '0-No errors encountered';

            } else if ($error_response['status_code'] == '1') {
                $error_response['status_code'] = '1-Processing error';

            } else if ($error_response['status_code'] == '2') {
                $error_response['status_code'] = '2-Missing device token';

            } else if ($error_response['status_code'] == '3') {
                $error_response['status_code'] = '3-Missing topic';

            } else if ($error_response['status_code'] == '4') {
                $error_response['status_code'] = '4-Missing payload';

            } else if ($error_response['status_code'] == '5') {
                $error_response['status_code'] = '5-Invalid token size';

            } else if ($error_response['status_code'] == '6') {
                $error_response['status_code'] = '6-Invalid topic size';

            } else if ($error_response['status_code'] == '7') {
                $error_response['status_code'] = '7-Invalid payload size';

            } else if ($error_response['status_code'] == '8') {
                $error_response['status_code'] = '8-Invalid token';

            } else if ($error_response['status_code'] == '255') {
                $error_response['status_code'] = '255-None (unknown)';

            } else {
                $error_response['status_code'] = $error_response['status_code'].'-Not listed';
            }
            echo '<br><b>ERROR</b> Response Command:<b>' . $error_response['command'] . '</b>&nbsp;&nbsp;&nbsp;Identifier:<b>' . $error_response['identifier'] . '</b>&nbsp;&nbsp;&nbsp;Status:<b>' . $error_response['status_code'] . '</b><br>';
            echo 'Identifier is the rowID (index) in the database that caused the problem, and Apple will disconnect you from server. To continue sending Push Notifications, just start at the next rowID after this Identifier.<br>';
       }

fclose($fp);
$_ENV['connection_status'] = 'The connection has been closed by the client' . '<br />';
}
} 
set_time_limit(30);
}
/***通过APN为IOS推送通知***/
设置时间限制(0);
//用于输出的字符集头
标题('content-type:text/html;charset:utf-8');
$DeviceID=array(/*从数据库获取所有设备令牌ID*/);
如果(计数($DeviceID)>0){
//这是您可以自定义通知的地方
$body['aps']=数组(
“徽章”=>+1,
“警报”=>“新闻事件!”,
“声音”=>“默认值”
);
$payload=json_encode($body);
////////////////////////////////////////////////////////////////////////////////
//开始创建连接
$ctx=stream_context_create();
流上下文设置选项($ctx,'ssl','local_cert','XXXX.pem”);
流上下文设置选项($ctx,'ssl','passphrase','XXXXXXX”);
foreach($deviceID作为$item\u设备){
//打开与APNS服务器的连接
$fp=流\u套接字\u客户端($fp)ssl://gateway.push.apple.com:2195“,$err,$errstr,60,流式客户端连接|流式客户端持久化,$ctx);
if(!$fp){exit(“连接失败:$err$errstr”。“
”);}else{/*在线服务*/} //构建二进制通知 $msg_notification=chr(0).pack('n',32.).pack('H*,$item_device.).pack('n',strlen($payload))。$payload; //将其发送到服务器 $result=fwrite($fp,$msg_通知,strlen($msg_通知)); 如果(!$result){echo'未送达邮件计数:'.$item_device.'
';} 否则{/*将发送通知*/} 如果($fp){ ##检查错误 $apple\u error\u response=fread($fp,6);//字节1=always 8,字节2=StatusCode,字节3,4,5,6=identifier(rowID)。如果确定,则应不返回任何内容。 //注意:请确保设置了stream\u set\u blocking($fp,0),否则当没有响应发送时,fread将暂停脚本并永远等待。 if($apple\u错误\u响应){ $error\u response=unpack('Ccommand/Cstatus\u code/Nidentifier',$apple\u error\u response);//解包错误响应(第一个字节“command”应始终为8) 如果($error\u response['status\u code']='0'){ $error_response['status_code']=“0-未遇到错误”; }否则如果($error\u response['status\u code']='1'){ $error_response['status_code']=“1-处理错误”; }else if($error\u response['status\u code']=='2'){ $error_response['status_code']=“2-缺少设备令牌”; }否则如果($error\u response['status\u code']='3'){ $error_response['status_code']=“3-缺少主题”; }否则如果($error\u response['status\u code']='4'){ $error_response['status_code']=“4-缺少有效负载”; }否则如果($error\u response['status\u code']='5'){ $error_response['status_code']=“5-无效令牌大小”; }否则如果($error\u response['status\u code']='6'){ $error_response['status_code']=“6-无效主题大小”; }否则如果($error\u response['status\u code']='7'){ $error_response['status_code']=“7-无效有效负载大小”; }else if($error\u response['status\u code']='8'){ $error_response['status_code']=“8-无效令牌”; }否则如果($error\u response['status\u code']='255'){ $error\u response['status\u code']=“255无(未知)”; }否则{ $error_response['status_code']=$error_response['status_code'].-未列出'; } 回显'
错误响应命令:'.$ERROR\u Response['Command'].'Identifier:'.$ERROR\u Response['Identifier'.'状态:'.$ERROR\u Response['Status\u code'.]; echo“Identifier是数据库中导致问题的rowID(索引),Apple将断开您与服务器的连接。要继续发送推送通知,只需从该标识符后的下一个rowID开始即可。
; } fclose($fp); $\u ENV['connection\u status']=“该连接已被客户端关闭”。
; } } 设置时间限制(30); }
当我们发送消息时,没有错误,每个想法都很好,但是推送通知没有收到。 问题要么在PHP脚本内部,要么在apache cordova脚本中


谢谢你的建议…

你忘了安装这两个cordova插件


由于这两个原因,您无法检测您的
设备类型
控制台
您的输出。

我们发现了问题…我们尝试使用生产连接服务器通过具有开发证书的APNS发送推送。 对于生产,请使用以下连接:

ssl://gateway.push.apple.com:2195
对于开发,请使用以下连接:

ssl://gateway.sandbox.push.apple.com:2195

您需要完全退出应用程序才能接收推送,如果我在应用程序中,如何继续执行警报?onNotificationAPNS过程从未启动过!您是否在退出应用程序时收到推送通知?如果是,当您在应用程序中时,用此方法捕获通知“-(void)应用程序:(UIApplication*)应用程序DidReceivereMotentification:(NSDictionary*)用户信息然后在alertview中显示通知。推送通知从未被激发和接收,甚至php代码也没有返回任何错误!好心的这两个cordova插件已经在config.xml文件中安装和设置,问题不在于此,因为推送是在Android上接收的。问题出在iOS上。