简体   繁体   中英

Where to correctly place and name .pem file for apple notification in php

My cron files are under cron folder which is situated at the root directory. But it is always showing an error as -

Warning: stream_socket_client(): SSL: crypto enabling timeout in /home/myname/beta.myhost.com/cron/dailycron.php on line 43

Warning: stream_socket_client(): Failed to enable crypto in /home/myname/beta.myhost.com/cron/dailycron.php on line 43

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /home/myname/beta.myhost.com/cron/dailycron.php on line 43

Line 43 is around -

$apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);

My code is as follows -

<?php

//############################# Database connection ################################/

//######################### Daily notification code ############################################/

$result = mysql_query("SELECT DISTINCT EntryId, twittes, groupname, created_at FROM users_twitte NATURAL JOIN (SELECT EntryId, MAX( created_at ) created_at FROM users_twitte GROUP BY EntryId) t");
while( $row = mysql_fetch_array($result) ) {

  // Get user array from EntryId
  $rowdata = $row['EntryId'];

  $result2 = mysql_query("SELECT * FROM `users_subscription` WHERE EntryId = $rowdata");

  while( $row2 = mysql_fetch_assoc($result2) ) {
    $group_name = $row['groupname'];
    $grp_usr_id = $row2['uid'];
    send_notification($group_name, $grp_usr_id);
  }
}

// Send Notification
function send_notification($groupname, $uid) {

    // GET device token from uid
    //$device_token = get_device_token($uid);   

    $payload = array();
    $payload['aps'] = array('alert' => "THERE IS AN MESSAGE FROM $groupname", 'badge' => 0, 'sound' => 'default');
    $payload = json_encode($payload);

    $apns_url = NULL;
    $apns_cert = NULL;
    $apns_port = 2195;

    $apns_url = 'gateway.sandbox.push.apple.com';
    $apns_cert = '/home/myname/beta.myhost.com/cron/ck.pem';

    $stream_context = stream_context_create();
    stream_context_set_option($stream_context, 'ssl', 'local_cert', $apns_cert);
    stream_context_set_option($stream_context, 'ssl', 'passphrase', "12345");

    $apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);

    $get_token_info = get_device_token_and_type($uid);
    if( 1 === $get_token_info['status'] ) {
        if(0 === $get_token_info['device_type']) {
            $device_token = $get_token_info['$device_token'];   
            $apns_message = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device_token)) . chr(0) . chr(strlen($payload)) . $payload;
        }
        fwrite($apns, $apns_message);
        @socket_close($apns);
        @fclose($apns);
    }
}

// GET device token for uid
function get_device_token_and_type($uid) {
    $dev_tok_type_arr = array();
    // GET EMAIL id from uid
    $result = mysql_query("SELECT LoginData.device_token, LoginData.device_type FROM LoginData INNER JOIN users ON LoginData.userid = users.email WHERE users.uid =$uid");
    if( mysql_num_rows($result) > 0 ) {
        while( $rows = mysql_fetch_assoc($result) ) {
            $dev_tok_type_arr = array('device_token'=> $rows['device_token'], 'device_type'=> $rows['device_type'], 'status'=> 1);
        }
    }
    else {
        $dev_tok_type_arr = array('status'=>0, 'message'=> 'No device token exists for the userid');
    }
    return $dev_tok_type_arr;
}

?>

My .pem file is in same cron folder and I mentioned absolute path at - $apns_cert . Let me know what I am doing wrong, I checked passphrase which is correct.

只需将您的pem文件放在php脚本的同一文件夹中,然后将变量更改为:

$apns_cert = 'ck.pem';

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