简体   繁体   中英

how to send push notification in ios from server using php in newer version of ios, without using pem file

i have sent notification in older versions of ios. but in newer version im not able to create .pem file. Someone told me that pem file is no longer required to send notification from server. But with bad luck i am not able to find any link regarding this. Someone please guide me how to send push notifications from server in newer version of ios. I am stuck in sending notification since last week. Please help. Here is the code i am using

private function pushnotification($deviceToken, $message, $type, $badge, $userid, $jobid) {
    $passphrase = '123456';

    $ctx = stream_context_create();

   // $file = base_path().  "/public/WenderCastPush.pem";
    //stream_context_set_option($ctx, 'ssl', 'local_cert', $file);
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
    stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
    stream_context_set_option($ctx, 'ssl', 'verify_peer_name', false);
    stream_context_set_option($ctx, 'ssl', 'allow_self_signed', true);
    //stream_context_set_option($ctx, 'ssl','ciphers', 'TLSv1');
    // 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);
    $fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);

    if (!$fp)
        exit("Failed to connect: $err $errstr" . PHP_EOL);

    $body['aps'] = array(
        //'badge' => +1,
        'alert' => $message,
        'sound' => 'default',
        //'title' => $message,
        'type' => $type, 
        'userid' => $userid ,
        'jobid' => $jobid, 
    );
    // Encode the payload as JSON
    $payload = json_encode($body);


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

    if (!$result)
        $responce = 'Message not delivered' . PHP_EOL;
    else
        $responce = 'Message successfully delivered' . PHP_EOL;

    // Close the connection to the server
    fclose($fp); 
    return $responce;
}

You can send notification using Firebase.
Your iOS app should be compiled with Firebase.
and call following address using Post method

visit this link

It is not very difficult.

check this video .

Send push notifications to iOS using the new APNs HTTP/2 protocol with token-based (JWT with p8 private key) or certificate-based authentication

You Can use This Package

and documentation of apple developers its useful

You can use iOS send notification with latest PHP version because, if your PHP code is not latest version, then notification send will not work.

if(defined('CURL_HTTP_VERSION_2_0'))
{
    $device_token   = 'enter your device token';
    $pem_file       = 'enter your pem file path ;
    $pem_secret     = enter password';
    $apns_topic     = 'enter topic'; 


    $sample_alert = '{"aps":{"alert":"hi  yourporject name","sound":"default"}}';
    $url = "https://api.development.push.apple.com/3/device/$device_token";

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $sample_alert);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("apns-topic: $apns_topic"));
    curl_setopt($ch, CURLOPT_SSLCERT, $pem_file);
    curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pem_secret);
    $response = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

   

    var_dump($response);
    var_dump($httpcode);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM