【问题标题】:Apple push notification not working in phpApple推送通知在php中不起作用
【发布时间】:2020-04-08 16:22:16
【问题描述】:

我的 php 脚本总是为苹果推送通知提供错误代码 0。我使用的代码如下所示

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'passphrase', '');
stream_context_set_option($ctx, 'ssl', 'local_cert', 'TxxxProd.pem'); //TxxxDev.pem
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
stream_set_blocking ($fp, 0); 

$err 总是返回 0

我们在服务器上修改的是我们升级了php版本并且更新了ssl证书。

当前的 php 版本是 PHP 版本 5.6.29

由于代码以前可以工作,我无法找出它现在不工作的原因。作为初学者,我不知道服务器中的.pem 文件?

我们需要对.pem 文件进行一些修改吗?

【问题讨论】:

    标签: php apple-push-notifications stream-socket-client


    【解决方案1】:

    试试这个,

    <?php 
    $ctx = stream_context_create();
    
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'TxxxProd.pem'); //TxxxDev.pem
    
    $fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
    stream_set_blocking ($fp, 0); 
    
    
    $tHost = 'gateway.sandbox.push.apple.com';
    
    $tPort = 2195;
    
    $tToken = '*****************';
    
    $tAlert = 'Hi this is a test message from vineeth';
    
    $tBadge = 8;
    
    $tSound = 'default';
    
    $tPayload = 'APNS Message Handled by LiveCode';
    
    $tBody['aps'] = array (
    'alert' => $tAlert,
    'badge' => $tBadge,
    'sound' => $tSound,
    );
    
    $tBody ['payload'] = $tPayload;
    
    $tBody = json_encode ($tBody);
    
    $tSocket = stream_socket_client ('ssl://'.$tHost.':'.$tPort, $error, $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $tContext);
    
    if (!$tSocket)
    
    exit ("APNS Connection Failed: $error $errstr" . PHP_EOL);
    
    $tMsg = chr (0) . chr (0) . chr (32) . pack ('H*', $tToken) . pack ('n', strlen ($tBody)) . $tBody;
    
    // Send the Notification to the Server.
    
    $tResult = fwrite ($tSocket, $tMsg, strlen ($tMsg));
    
    if ($tResult)
    
    echo 'Delivered Message to APNS' . PHP_EOL; 
    
    else
    
    echo 'Could not Deliver Message to APNS' . PHP_EOL;
    
    // Close the Connection to the Server.
    
    fclose ($tSocket);
    
    ?>
    

    【讨论】:

    • 我收到了同样的错误 "APNS Connection Failed:" 。我试过 $tHost = 'gateway.push.apple.com';和 $tHost = 'gateway.sandbox.push.apple.com';我在我的编辑中附上了上面的截图
    猜你喜欢
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多