Push Sending arriving frantically or not enough

Asked

Viewed 54 times

2

I have a system that sends notifications pushs to devices that have an app installed. Specifically on iOS is coming several times (30 and more) the same push.

On Android is happening the following error:

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

I’m doing like 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 in the table row saying that it was already and so do not need to send again. This logic is working. I tested by doing the query in the database before sending a push manually and after. The registration 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 so:

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

    See if it gives a light :https://stackoverflow.com/questions/12919156/apple-push-notification-service-sends-repeated-notifications#Answer-13361561

  • Cool @Magichat. Gave a light really. Now, how will I discover the port that this system uses? I’m using the same APNS.

  • 1

    Or :https://stackoverflow.com/questions/12866600/duplicate-apple-apns-push-notifications#Answer-12891443

  • I know one thing, on the return of the APNS always returns 1443, that $result at the end of the code.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.