【问题标题】:2195 port is compulsory require to open? IOS Push notification not working in php and Ios Push notification not comming into mobile in laravel2195端口是强制要求开放的吗? IOS 推送通知在 php 中不起作用,而 Ios 推送通知在 laravel 中没有进入移动设备
【发布时间】:2021-04-28 13:29:57
【问题描述】:

我正在使用 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 端口的问题已接近,这是为什么呢?

【问题讨论】:

  • 是的!!!!我得到了解决方案!耶熙
  • 下面我把我的问题的答案请参考这个答案如果有人没有收到 ios 推送通知或在 ios 推送通知中收到错误。
  • IOS 推送通知旧代码已更改,这就是通知将停止的原因。
  • 已解决 - HTTP/2 iOs 推送通知 Laravel Helper 类 - 为我工作 github.com/webblufter/…

标签: php swift laravel push-notification 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;
    }
}

【讨论】:

  • 如何将 lcobucci/jwt 添加到仅具有文件访问权限的共享网站?
  • 你需要安装compose require lcobucci/jwt:^3.3.1库。
【解决方案2】:

我认为现在使用端口 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 标签,我看到其他人也是。

【讨论】:

  • 嘿@VikasKatariya 我仍在使用 Apple 进行故障排除。我不完全确定,我刚刚完全不推荐使用端口 2195 的任何东西。
  • 嘿@VikasKatariya 这是苹果告诉我的。 stackoverflow.com/questions/67308054/…
  • 目前我没有得到任何解决方案
  • 尝试基于证书或令牌的连接?我将在今天晚些时候对其进行测试。更新中...感谢您的回复。
  • 说实话,我对 PEM 方面的看法可能是错误的。我没有看到任何地方提到它。我再看看。
猜你喜欢
  • 2013-09-04
  • 1970-01-01
  • 1970-01-01
  • 2014-05-08
  • 1970-01-01
  • 2016-03-22
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
相关资源
最近更新 更多