Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.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
iOS推送通知无法使用PHP工作_Php_Ios_Push Notification_Apple Push Notifications - Fatal编程技术网

iOS推送通知无法使用PHP工作

iOS推送通知无法使用PHP工作,php,ios,push-notification,apple-push-notifications,Php,Ios,Push Notification,Apple Push Notifications,我正在尝试使用APNS库在PHP的iOS中实现推送通知。这是我的密码: <?php // Put your device token here (without spaces): $deviceToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // Put your private key's passphrase here: $passphrase = 'XXXXXX'; // Put your alert message here

我正在尝试使用APNS库在PHP的iOS中实现推送通知。这是我的密码:

<?php

// Put your device token here (without spaces):    
$deviceToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';    

// Put your private key's passphrase here:
$passphrase = 'XXXXXX';

// Put your alert message here:
$message = 'My first push notification!';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'Certificates.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
    'ssl://gateway.sandbox.push.apple.com:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
    );

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));print_r($result);

if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);
?>

为什么会这样?我遗漏了什么吗?

有两个潜在的原因导致了这种情况:

  • 您的应用程序位于前台
  • 您的设备令牌无效

  • 在我的应用程序中。我正在直接获取
    NSData
    中的
    deviceToken

    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
        NSString *deviceTokenString = [NSString stringWithFormat:@"%@", deviceToken];
        // this is producing something like:
        // <xxxxxxx 9b0f527f xxxxxxxx 2727ed28 xxxxxxxx 4e693a61 xxxxxxx ac2f7dbb>
        // 
        // and i am directly saving that to my servers database
    
    -(void)应用程序:(UIApplication*)应用程序DIDregisterforRemotionTificationswithDeviceToken:(NSData*)deviceToken
    {
    NSString*deviceTokenString=[NSString stringWithFormat:@“%@”,deviceToken];
    //这产生了类似于:
    // 
    // 
    //我直接将其保存到我的服务器数据库中
    
    这是我服务器中的内容

    $from_database = "<xxxxxxx 9b0f527f xxxxxxxx 2727ed28 xxxxxxxx 4e693a61 xxxxxxx ac2f7dbb>";
    
    // this removes the '<' and '>' to make the device token valid
    //
    $token_string = substr($from_database, 1, -1);
    
    $token_array[] = $token_string;
    
    // you can also add multiple 'token_string' to the 'token_array' for push notification broadcasting, i am currently doing that and it is working properly.
    
    ...
    
    public function __push_notification($message_input, $token_array) 
    {
        $message = stripslashes($message_input);
    
        $passphrase = 'xxxxxx';
    
        $cert = realpath('xxx.pem');
    
        $payload = '{
            "aps" :
                {
                    "alert" : "'.$message.'",
                    "badge" : 1, 
                    "sound" : "bingbong.aiff"
                }
        }';
    
        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', $cert);
        stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
    
        $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
    
        if (!$fp) 
        {
            // echo "Failed to connect $err $errstr <br>";
    
            return;
        }
        else 
            // echo "Post notification sent<br>";
    
        $dev_array = array();
    
        $dev_array = $token_array;
    
        foreach ($dev_array as $device_token) 
        {
            $msg =  chr(0) .
                    pack("n", 32) . 
                    pack('H*', str_replace(' ', '', $device_token)) . 
                    pack("n", strlen($payload)) . 
                    $payload;
    
            // echo "sending message :" . $payload . "n";
    
            fwrite($fp, $msg);
        }
        fclose($fp);
    }
    
    $from_database=”“;
    //这将删除“”以使设备令牌有效
    //
    $token_string=substr($from_数据库,1,-1);
    $token_数组[]=$token_字符串;
    //您还可以将多个“token\u string”添加到“token\u array”以进行推送通知广播,我目前正在这样做,并且它工作正常。
    ...
    公共函数推送通知($message\u input,$token\u array)
    {
    $message=stripslashes($message\u input);
    $passphrase='xxxxxx';
    $cert=realpath('xxx.pem');
    $1{
    “aps”:
    {
    “警报”:“'.$message.”,
    "徽章":一,,
    “声音”:“bingbong.aiff”
    }
    }';
    $ctx=stream_context_create();
    流上下文设置选项($ctx,'ssl','local_cert',$cert);
    stream_context_set_选项($ctx,'ssl','passphrase',$passphrase);
    $fp=流\u套接字\u客户端($fp)ssl://gateway.sandbox.push.apple.com:2195“,$err,$errstr,60,流式客户端连接|流式客户端持久化,$ctx);
    如果(!$fp)
    {
    //echo“无法连接$err$errstr
    ”; 返回; } 其他的 //回显“发送后通知
    ”; $dev_array=array(); $dev_数组=$token_数组; foreach($dev_数组作为$device_令牌) { $msg=chr(0)。 包装(“n”,32)。 包装('H*',str_替换('',$device_标记))。 包装(“n”,strlen($payload))。 美元有效载荷; //回显“发送消息:”.$payload.“n”; fwrite($fp,$msg); } fclose($fp); }

    另外,检查一下这一点,可能会有所帮助:

    他的错误在于ios 9,如果是这样,请停止使用objective C中的代码,并在swift中使用,因为它在为ios 9生成objective token-C时存在错误。 我建议紧急更改您的代码。 同样通过这种想法,当swift的预定ios发生变化时,服务器的一切都得到了解决,我的php几乎是相同的。 护理:


    是的..首先需要记录“$string\u令牌”`确保设备令牌没有两端..另一个是取消对echos的注释
    if(!$fp)
    ,最后是注释
    fwrite($fp,$msg);
    以防止发送通知..使用终端浏览器进行检查..
    echo“无法连接$err$errstr
    ”
    是检查正在发生的事情的一个好方法。它进入其他部分并打印-发布通知sentHmm。确保为您的应用程序启用推送通知。检查本教程的设置我认为问题不在您的服务器中。使用沙盒您可以接收通知吗?我认为这是他的问题。他正在发送到sandbox服务器,该服务器充当虚拟设备(读开发)。
    $from_database = "<xxxxxxx 9b0f527f xxxxxxxx 2727ed28 xxxxxxxx 4e693a61 xxxxxxx ac2f7dbb>";
    
    // this removes the '<' and '>' to make the device token valid
    //
    $token_string = substr($from_database, 1, -1);
    
    $token_array[] = $token_string;
    
    // you can also add multiple 'token_string' to the 'token_array' for push notification broadcasting, i am currently doing that and it is working properly.
    
    ...
    
    public function __push_notification($message_input, $token_array) 
    {
        $message = stripslashes($message_input);
    
        $passphrase = 'xxxxxx';
    
        $cert = realpath('xxx.pem');
    
        $payload = '{
            "aps" :
                {
                    "alert" : "'.$message.'",
                    "badge" : 1, 
                    "sound" : "bingbong.aiff"
                }
        }';
    
        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', $cert);
        stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
    
        $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
    
        if (!$fp) 
        {
            // echo "Failed to connect $err $errstr <br>";
    
            return;
        }
        else 
            // echo "Post notification sent<br>";
    
        $dev_array = array();
    
        $dev_array = $token_array;
    
        foreach ($dev_array as $device_token) 
        {
            $msg =  chr(0) .
                    pack("n", 32) . 
                    pack('H*', str_replace(' ', '', $device_token)) . 
                    pack("n", strlen($payload)) . 
                    $payload;
    
            // echo "sending message :" . $payload . "n";
    
            fwrite($fp, $msg);
        }
        fclose($fp);
    }
    
                $ msg = chr (0). pack ('N', 32). pack (H * ','. '$deviceToken [0].'). pack ('N', strlen ($ payload)). $ payload;