当前位置: 首页 > 面试题库 >

使用PHP通过APN发送iOS推送通知

葛越
2023-03-14
问题内容

我已经在项目中实现了推送通知,到目前为止一切正常。我尝试过通过Pusher发送通知,但效果很好。但是我必须通过PHP发送它们,但尚无法使用。我发现了许多有关如何实现此目标的旧解释,但似乎没有一个对我有用。

这就是我要使用的方法:

    // APNs Push testen auf Token
$deviceToken = $_GET['key']; // Device-Token

// Payload erstellen und JSON codieren
$payload['aps'] = array('alert' => 'TitleText', 'badge' => 1, 'sound' => 'default');
$payload = json_encode($payload);

$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'certificate.pem';

// Stream erstellen
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'certificate.cer', $apnsCert);

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
if ($apns)
{
  // Nachricht erstellen und senden
  $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
  fwrite($apns, $apnsMessage);

  // Verbindung schliessen
  fclose($apns);
}
else
{
  echo "Fehler!";
  var_dump($error);
  var_dump($errorString);
}

问题答案:

尝试使用此php脚本,确保.pem证书在运行时以与该php脚本相同的路径退出,并获得正确的设备令牌

 <?php
        /* We are using the sandbox version of the APNS for development. For production
        environments, change this to ssl://gateway.push.apple.com:2195 */
        $apnsServer = 'ssl://gateway.sandbox.push.apple.com:2195';
        /* Make sure this is set to the password that you set for your private key
        when you exported it to the .pem file using openssl on your OS X */
        $privateKeyPassword = '1234';
        /* Put your own message here if you want to */
        $message = 'Welcome to iOS 7 Push Notifications';
        /* Pur your device token here */
        $deviceToken =
        '05924634A8EB6B84437A1E8CE02E6BE6683DEC83FB38680A7DFD6A04C6CC586E';
        /* Replace this with the name of the file that you have placed by your PHP
        script file, containing your private key and certificate that you generated
        earlier */
        $pushCertAndKeyPemFile = 'PushCertificateAndKey.pem';
        $stream = stream_context_create();
        stream_context_set_option($stream,
        'ssl',
        'passphrase',
        $privateKeyPassword);
        stream_context_set_option($stream,
        'ssl',
        'local_cert',
        $pushCertAndKeyPemFile);

        $connectionTimeout = 20;
        $connectionType = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
        $connection = stream_socket_client($apnsServer,
        $errorNumber,
        $errorString,
        $connectionTimeout,
        $connectionType,
        $stream);
        if (!$connection){
        echo "Failed to connect to the APNS server. Error no = $errorNumber<br/>";
        exit;
        } else {
        echo "Successfully connected to the APNS. Processing...</br>";
        }
        $messageBody['aps'] = array('alert' => $message,
        'sound' => 'default',
        'badge' => 2,
        );
        $payload = json_encode($messageBody);
        $notification = chr(0) .
        pack('n', 32) .
        pack('H*', $deviceToken) .
        pack('n', strlen($payload)) .
        $payload;
        $wroteSuccessfully = fwrite($connection, $notification, strlen($notification));
        if (!$wroteSuccessfully){
        echo "Could not send the message<br/>";
        }
        else {
        echo "Successfully sent the message<br/>";
        }
        fclose($connection);

  ?>


 类似资料:
  • 如何通过Azure从我的UWP-App向不同设备上的应用程序的其他实例发送推送通知? 以下是注册设备以接收推送的说明。(这是可行的)第二部分是关于如何在控制台应用程序上发送推送(这也是可行的)https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-windows-store-dotnet-get-star

  • 我对通过FCM实现云消息感到困惑。要向iOS设备发送远程(推送)通知,APN需要iOS设备令牌。但是,Firebase应用程序不与APNs接口,它与FCM接口,FCM与APNs接口。但是通过FCM向任何客户端发送推送通知(或消息)需要FCM令牌。这是否意味着Firebase应用程序向iOS设备发送推送通知时不需要iOS设备令牌?

  • 我正在尝试使用Postman通过云消息服务发送一次推送通知。 这是一个用于相同目的的工作命令,我将其用作参考。 到目前为止我所做的。。 1-适当设置标题 2-在Body,我使用 执行时,我返回

  • 你知道如何从PHP向IOS应用程序发送推送通知吗? 这是某人的最佳答案http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1 但它不起作用:

  • 我正在尝试使用Azure通知中心向客户端发送推送通知。我读了这篇文章,它使用标签来识别每个用户。 https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-aspnet-backend-windows-dotnet-notify-users/ 它可以完成这项工作,但标记的数量有限。我正在考虑存储和使用中心返

  • 我的AWS社交网络停止向我的iOS订阅者发送推送通知,我正在试图了解原因。所以我从零开始,目标是向我的测试设备发送1个通知。 我已经做了以下工作: 在iOSDev Portal上创建新的Apple推送服务类型的新证书,而不是旧的APNsiOS生产,导入到钥匙串,导出证书和私钥,按照AWS SNS APNS说明中的说明将其添加到AWS。 已确认此新APNS证书显示在门户上我的应用ID的“生产SSL证