2195端口是强制要求开放的吗? IOS 推送通知在 php 中不起作用,并且 Ios 推送通知在 laravel 中无法进入移动设备

2195 port is compulsory require to open? IOS Push notification not working in php and Ios Push notification not comming into mobile in laravel

我正在使用 php 代码发送推送通知,但 ios 没有收到通知,所以我不知道确切的问题是什么,请帮忙。

public static function ios_push($device_token,$title,$msg,$description,$type = "",$r_type = "")
{
    \Log::info('device_token', ['context' => $device_token]);
    \Log::info($device_token);

    $badge_count =2;
    $streamContext = stream_context_create();

    $connectTimeout = 60;

    stream_context_set_option($streamContext, 'ssl', 'passphrase',IPHONE_CERTIFICATE_PASSWORD);

    \Log::info(IPHONE_CERTIFICATE_TYPE);

    if(IPHONE_CERTIFICATE_TYPE == "Development") 

    {

      //For Development

      stream_context_set_option($streamContext, 'ssl', 'local_cert',IOS_PUSH_DEV_PEM_PATH);

      $apns = stream_socket_client('ssl://gateway.push.apple.com:2195', $error, $errorString, $connectTimeout, STREAM_CLIENT_CONNECT |STREAM_CLIENT_PERSISTENT, $streamContext);

    } 

    else 

    {

      //For Production 

      stream_context_set_option($streamContext, 'ssl', 'local_cert',WWW_ROOT_PATH.IOS_PUSH_DEV_PEM_PATH);

      $apns = stream_socket_client('ssl://gateway.push.apple.com:2195', $error, $errorString, $connectTimeout, STREAM_CLIENT_CONNECT |STREAM_CLIENT_PERSISTENT, $streamContext);

    }

    if (!$apns) {
        \Log::info('Error : '.$error.' '.$errorString);
    } else {
        \Log::info("success");
    }

    $music = 'default';

    $payload['aps'] = array('alert' => ['title'=>$title,'body'=>$msg], 'badge' => $badge_count,'title'=>$description,'sound'=> $music , 'notification_type' =>  $type);

    //$payload['aps'] = array('alert' => "vikas", 'badge' => $badge_count,'sound'=> $music , 'notification_type' =>  $type);

    // $data['notification_type'] = $notification_type;

    // $data['sender_first_name'] = $sender_first_name;

    // $data['sender_last_name'] = $sender_last_name;

    // $data['sender_user_id'] = $sender_user_id;

    $data['sound'] = $music;

    $data['title'] = $title;

    $data['notification_type'] = $type;

    $data['report_type'] = !empty($r_type) ? substr($r_type, 0, -8) : "";

    $payload['data'] = $data;



    $payload = json_encode($payload);

    \Log::info('Log message', ['payload' => json_encode($payload)]);



    $apnsMessage = chr(0) . pack('n', 32) . pack('H*',  $device_token) . pack('n', strlen($payload)) . $payload; 

    $fwriteRes = fwrite($apns, $apnsMessage, strlen($apnsMessage));     

    fclose($apns);

    return true;

}

这是我的功能

但是IOS在手机上没有收到任何通知

问题是什么

2195 端口的问题很接近,那是为什么?

我认为现在正在使用端口 443

Sending Notification Requests to APNs

https://support.apple.com/en-us/HT203609

https://developer.apple.com/documentation/usernotifications/sending_push_notifications_using_command-line_tools?language=objc

我一直在通过开发者门户与 Apple 交谈,目前我只知道这些。我现在决定只挑选一些,看看其他使用 APNS 的开发人员做了什么来保持交付成功。我也问过这个问题,现在我正在浏览 Apple-Push-Notifications 标签,我看到其他人也是。

这个解决方案对我很有帮助!!!

------------==>步骤1

首先你需要安装这个作曲库。

compose require lcobucci/jwt:^3.3.1

------------==> 第二步

然后这样写代码..

use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Signer\Ecdsa\Sha256; 

public static function generateiosauthtoken() {
    $key = file_get_contents('Your p8 file');  // p8 file
    $signer = new Sha256();
    $time = time();
    return  (new Builder())->issuedBy("TEAMID") // iss claim
            ->permittedFor('https://appleid.apple.com') // aud claim
            ->expiresAt($time + 3600) // exp claim
            ->issuedAt($time) // iat claim
            ->withHeader('kid', "KEYID") // kid header
            ->getToken($signer, new Key($key));
}


public static function ios_push($device_token,$title,$msg,$description)
{
    $token = ApiHelper::generateiosauthtoken();
    $music = 'default';
    $payload['aps'] = array('alert' => ['title'=>$title,'body'=>$msg], 'badge' => 1,'title'=>$description,'sound'=> $music , 'notification_type' =>  $type);
    $data['sound'] = $music;
    $data['title'] = $title;
    $payload['data'] = $data;
    $payload = json_encode($payload);

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_HTTP09_ALLOWED, true);
    curl_setopt_array($curl, array(
      CURLOPT_PORT => "443",
      CURLOPT_URL => "https://api.push.apple.com:443/3/device/".$device_token."",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => $payload,
      CURLOPT_HTTPHEADER => array(
        "apns-topic: bundleid", // put it here your aplication bundle id
        "authorization: bearer ".$token."",
       
      ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);
    if ($err) {
      $arr = array("code" => 400, "msg" => $err,"flag" => false , "data" => (object)array());
      \Response::json($arr);
    } else {
      echo $response;
    }
}