菜鸟教程小白 发表于 2022-12-11 22:41:28

iphone - 为什么 iPhone 收不到推送通知?


                                            <p><p>我真的不知道问题出在哪里。
我正在使用库 ApnsPHP 来发送推送通知。我也尝试了其他脚本,但也没有用。</p>

<p>我使用本教程 (http://code.google.com/p/apns-php/wiki/CertificateCreation) 生成推送证书,并将它们放到苹果开发者网站上。
我从 iphone 获得了正确的 token ,我将其放入 sample_push.php</p>

<p>我有 Macbook Pro 13 英寸,2010 年中,带有 Mac OS Lion。</p>

<pre><code>vojta:~/dev/www/application$ php sample_push.php
Fri, 13 Apr 2012 16:23:24 +0200 ApnsPHP: INFO: Trying ssl://gateway.sandbox.push.apple.com:2195...
Fri, 13 Apr 2012 16:23:32 +0200 ApnsPHP: INFO: Connected to ssl://gateway.sandbox.push.apple.com:2195.
Fri, 13 Apr 2012 16:23:32 +0200 ApnsPHP: INFO: Sending messages queue, run #1: 1 message(s) left in queue.
Fri, 13 Apr 2012 16:23:32 +0200 ApnsPHP: STATUS: Sending message ID 1 (1/3): 109 bytes.
Fri, 13 Apr 2012 16:23:33 +0200 ApnsPHP: INFO: Disconnected.
</code></pre>

<p><strong>如果我执行“php sample_push.php”,我不会收到任何错误,但我的 iPhone 上也没有收到任何推送通知。</strong></p>

<p>来源 sample_push.php:</p>

<pre><code>// Using Autoload all classes are loaded on-demand
require_once &#39;ApnsPHP/Autoload.php&#39;;

// Instanciate a new ApnsPHP_Push object
$push = new ApnsPHP_Push(
    ApnsPHP_Abstract::ENVIRONMENT_SANDBOX,
    &#39;server_certificates_bundle_sandbox.pem&#39;
);

// Set the Root Certificate Autority to verify the Apple remote peer
$push-&gt;setRootCertificationAuthority(&#39;entrust_root_certification_authority.pem&#39;);

// Connect to the Apple Push Notification Service
$push-&gt;connect();

// Instantiate a new Message with a single recipient

$message = new ApnsPHP_Message(&#39;xxxx&#39;); // i put my token here

// Set a custom identifier. To get back this identifier use the getCustomIdentifier() method
// over a ApnsPHP_Message object retrieved with the getErrors() message.
$message-&gt;setCustomIdentifier(&#34;Message-Badge-3&#34;);

// Set badge icon to &#34;3&#34;
//$message-&gt;setBadge(3);

// Set a simple welcome text
$message-&gt;setText(&#39;Hello APNs-enabled device!&#39;);

// Play the default sound
$message-&gt;setSound();

// Set the expiry value to 30 seconds
$message-&gt;setExpiry(30);

// Add the message to the message queue
$push-&gt;add($message);

// Send all messages in the message queue
$push-&gt;send();

// Disconnect from the Apple Push Notification Service
$push-&gt;disconnect();

// Examine the error message container
$aErrorQueue = $push-&gt;getErrors();
if (!empty($aErrorQueue)) {
    var_dump($aErrorQueue);
}
</code></pre>

<p>我也在尝试更简单的脚本(也不起作用)</p>

<pre><code>// Put your device token here (without spaces):
$deviceToken = &#39;xxxxxxxxxxx&#39;;

// Put your private key&#39;s passphrase here:
$passphrase = &#39;pushchat&#39;;

// Put your alert message here:
$message = &#39;My first push notification!&#39;;

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

$ctx = stream_context_create();
stream_context_set_option($ctx, &#39;ssl&#39;, &#39;local_cert&#39;, &#39;ck.pem&#39;);
stream_context_set_option($ctx, &#39;ssl&#39;, &#39;passphrase&#39;, $passphrase);

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

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

echo &#39;Connected to APNS&#39; . PHP_EOL;

// Create the payload body
$body[&#39;aps&#39;] = array(
    &#39;alert&#39; =&gt; $message,
    &#39;sound&#39; =&gt; &#39;default&#39;
    );

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

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

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

if (!$result)
    echo &#39;Message not delivered&#39; . PHP_EOL;
else
    echo &#39;Message successfully delivered&#39; . PHP_EOL;


// Close the connection to the server
fclose($fp);
</code></pre>

<p>我真的不知道该怎么办。请帮助我。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我解决了。
这是因为 XCode 使用 iOS Team Provisioning Profile 为应用程序签名。在我删除此个人资料后,推送通知开始起作用。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 为什么 iPhone 收不到推送通知?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/10143527/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/10143527/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 为什么 iPhone 收不到推送通知?