【问题标题】:iOS push notification not working using PHPiOS推送通知无法使用PHP
【发布时间】:2015-07-28 06:37:29
【问题描述】:

我正在尝试使用 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);
?>

它显示消息发送成功,但在 iOS 中它不显示通知。

2015-07-28 08:32:39 Connected to APNS result = 97
2015-07-28 08:32:39 Message successfully delivered 
2015-07-28 08:32:39 Connection closed to APNS

为什么会这样?我错过了什么吗?

【问题讨论】:

    标签: php ios push-notification apple-push-notifications


    【解决方案1】:

    他的错误在于 ios 9 如果停止使用目标 C 中的代码并在 swift 中使用,因为它在生成目标令牌 - C for ios9 时存在错误。 我建议紧急更改您的代码。 同样通过这个想法,当预定的 ios 更改为 swift 时,服务器也得到了解决,我的 php 几乎相等。 护理:

                $ msg = chr (0). pack ('N', 32). pack (H * ','. '$deviceToken [0].'). pack ('N', strlen ($ payload)). $ payload;     
    

    【讨论】:

      【解决方案2】:

      造成这种情况的潜在原因有两个:

      1. 您的应用位于foreground
      2. 您的设备令牌无效

      在我的应用程序中。我直接得到deviceToken 那在NSData

      - (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
      

      这是我的服务器中的内容。

      $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);
      }
      

      也检查一下,可能会有所帮助:Facing problems in ios push notification php code

      【讨论】:

      • 是的..首先你需要记录`$string_token`确保设备令牌没有''两端..另一个是取消注释回声if (!$fp)最后评论fwrite($fp, $msg); 防止发送通知.. 使用您的终端浏览器检查它..
      • echo "Failed to connect $err $errstr &lt;br&gt;"; 是检查正在发生的事情的好方法..
      • 它进入其他部分并打印 - 发送通知
      • 嗯.. 确保为您的应用启用推送通知。查看本教程以设置Push notification (app side) 我认为问题不在您的服务器上..
      • 使用沙盒可以接收通知吗?我认为这是他的问题。他正在向沙盒服务器发送数据,该服务器充当虚拟设备(阅读开发here)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多