Sending Push arriving frantically or not arriving

2

I have a system that sends push notifications to devices that have an application installed. Specifically in iOS is arriving several times (30 and more) the same push.

On Android the following error is occurring:

https://fcm.googleapis.com/fcm/send connection refused: port 443

I'm doing this on iOS:

private function sendIosPush($title, $message, $token, $data = null){

    /* Define Gateway de Conexão */
    $production = true;

    if ($production){
        $gateway = 'ssl://gateway.push.apple.com:2195';
    }
    else {
        $gateway = 'ssl://gateway.sandbox.push.apple.com:2195';
    }

    $ctx = stream_context_create();

    // Is Your Certificate File
    stream_context_set_option($ctx, 'ssl', 'local_cert', '/certs/ck.pem');
    stream_context_set_option($ctx, 'ssl', 'passphrase', 'senha');
    stream_context_set_option($ctx, 'ssl', 'cafile', '/certs/entrust_2048_ca.cer');

    // Open a Connection to the APNS Server
    $fp = stream_socket_client(
        $gateway, $err,
        $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

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

    // Create The Payload Body
    $body = [];
    $body['aps'] = array(
        'alert' => array(
            'title' => $title,
            'body' => $message,
         ),
        'sound' => 'default'
    );

    if(is_array($data)) $body += $data;

    // Encode the Payload as JSON
    $payload = json_encode($body);

    // Build the Binary Notification
    $msg = chr(0) . pack('n', 32) . pack('H*', $token) . pack('n', strlen($payload)) . $payload;

    // Send it to the Server
    $result = fwrite($fp, $msg, strlen($msg));

    // Close the Connection to the Server
    fclose($fp);

    if (!$result)
        return false;
    else
        return true;
}

When the push is sent I put a flag on the table saying that it was already and so do not need to send again. This logic is working. I tested doing the query in the database before sending a push manually and then. The record is no longer in the query.

I'm thinking it might be a parameter I'm not sending or something like.

And on Android I'm doing this:

$fields = array(
    'registration_ids'  => $registerIds, // registers ids
    'data'              => $msg
);

$headers = array(
    'Authorization: key='.$this->android_push_key, // GCM KEY
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );
//curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result         = curl_exec($ch);
$header_size    = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$httpcode       = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$body           = substr($result, $header_size);

curl_close($ch);
    
asked by anonymous 03.10.2017 / 13:34

0 answers