I am sending push notification using php code and ios didn't get notification so whats the exact issue i don't know please help.
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;
}
This is my function
But IOS didn't get any notification in mobile
SO whats the issue
The issue for 2195 port is close thats why?
This solution is perfectly help for me!!!!
-----------==> Step 1
First of all you need to install this composer library.
compose require lcobucci/jwt:^3.3.1
-----------==> Step 2
Then write code like..
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;
}
}