2015-10-01 1 views
23

Ich verwende RMSPushNotificationsBundle für die Handhabung von Push-Benachrichtigungen. Ich sende pushNotifications von einem Server an mehrere Apps. Ich benutze die setAPNSPemAsString-Methode, die das richtige Zertifikat auswählt. Die Push-Benachrichtigung wird jedoch nur beim ersten Mal gesendet. Kann mir jemand sagen warum? Vielen Dank!Push-Benachrichtigungen für mehrere Apps von einem Server

public function sendIOS($appName){ 
    $notifications = $this->container->get('rms_push_notifications'); 

    $message = new iOSMessage(); 
    $message->setMessage($this->message); 
    $message->setData($this->getData()); 
    $message->setAPSSound("default"); 
    $message->setDeviceIdentifier($this->pushToken); 

    if ($appName !="appName") { 
     $pemFile = $this->container->getParameter("rms_push_notifications.ios.".$appName.".pem"); 
     $passphrase = $this->container->getParameter("rms_push_notifications.ios.".$appName.".passphrase"); 

      $pemContent = file_get_contents($pemFile); 
      $notifications->setAPNSPemAsString($pemContent, $passphrase); 
    } 
    return $notifications->send($message); 
} 
+1

Sie alles in den Protokollen haben Sie? Wo wird '$ appName' festgelegt? – tftd

+0

mit appName ist nur eine Variable, mit der wir entscheiden, welches Zertifikat wir für Push-Benachrichtigungen verwenden. Mit appName wissen wir, welche Version unseres Anwendungsbenutzers auf seinem Telefon verwendet. Die Verwendung der setAPNSPemAsString-Funktion funktioniert nur einmal mit der ersten Push-Benachrichtigung für andere. Send-Funktion gibt uns false zurück. – Gasper

+0

Für IOS enthält das Paket einen Feedback-Service. Vielleicht finden Sie hier eine Antwort. Hast du es gesehen? https://github.com/richsage/RMSPushNotificationsBundle#ios-feedback-service –

Antwort

1

Ich bin mir nicht sicher, was das Problem ist, aber die Befolgung des kleinen Codes funktionierte für mich. Zumindest können Sie damit die Verbindung zum APNS-Server testen.

<?php 
// your private key's passphrase 
$passphrase = $_POST('passphrase'); 

$pemFilesPath = 'path/to/pem/folder/'; 

// path to pem file 
$appCert = $_POST('pemfile'); 

$pemFile = $pemFilePath.$appCert; 

$notifications = $_POST('notifications'); 

//////////////////////////////////////////////////////////////////////////////// 

$ctx = stream_context_create(); 
stream_context_set_option($ctx, 'ssl', 'local_cert', $pemFile); 
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 

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

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

echo 'Connected to APNS' . PHP_EOL; 

$records = 0; 

foreach ($notifications as $deviceToken => $message) 
{ 
    // Create the payload body 
    $body['aps'] = array(
     'alert' => $message, 
     'sound' => 'default' 
     ); 

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

    // Build the binary notification 
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; 

    if (!$fp) { 
     exit("Connection intruptted " . E_USER_ERROR . PHP_EOL); 
    } 
    // Send it to the server 
    $result = fwrite($fp, $msg, strlen($msg)); 
    if(!$result) { 

           print_r("Failed writing to stream.", E_USER_ERROR); 
           fclose($fp); 
           die; 
         } 
        /* uncomment this part for troubleshooting 
         else { 
           $tv_sec = 1; 
           $tv_usec = null; // Timeout. 1 million micro seconds = 1 second 
           $read = array($fp); $we = null; // Temporaries. "Only variables can be passed as reference." 
           $numChanged = stream_select($read, $we, $we, $tv_sec, $tv_usec); 
           if(false===$numChanged) { 
             print_r("Failed selecting stream to read.", E_USER_ERROR); 
             fclose($fp); 
             die; 
           } 
           else if($numChanged>0) { 
             $command = ord(fread($fp, 1)); 
             $status = ord(fread($fp, 1)); 
             $identifier = implode('', unpack("N", fread($fp, 4))); 
             $statusDesc = array(
               0 => 'No errors encountered', 
               1 => 'Processing error', 
               2 => 'Missing device token', 
               3 => 'Missing topic', 
               4 => 'Missing payload', 
               5 => 'Invalid token size', 
               6 => 'Invalid topic size', 
               7 => 'Invalid payload size', 
               8 => 'Invalid token', 
               255 => 'None (unknown)', 
             ); 
             print_r("APNS responded with command($command) status($status) pid($identifier).", E_USER_NOTICE); 

             if($status>0) { 
               $desc = isset($statusDesc[$status])?$statusDesc[$status]: 'Unknown'; 
               print_r("APNS responded with error for pid($identifier). status($status: $desc)", E_USER_ERROR); 
               // The socket has also been closed. Cause reopening in the loop outside. 
               fclose($fp); 
               die; 
             } 
             else { 
               // Apple docs state that it doesn't return anything on success though 
               $records++; 
             } 
           } else { 
             $records++; 
           } 
         } 
         */ 
    $records++; 
    } 

echo "Send notifications to $records devices"; 
// Close the connection to the server 
fclose($fp); 

?> 

Hinweis: Schrieb diesen kleinen Code vor langer Zeit und es hat gut funktioniert. Ich erinnere mich nicht mehr an die Quelle, aber es gab ein Tutorial. Haben Sie nicht kürzlich getestet, so dass Sie es möglicherweise überarbeiten müssen. Einige Vorschläge:

  1. Öffnen Sie die Verbindung und schreiben Sie alle Benachrichtigungen auf den Server, dann schließen Sie es.
  2. Das Lesen der Serverantwort reduziert die Funktionalität, eignet sich jedoch gut für die Fehlerbehebung und für den Start. Verwenden Sie dieses Teil also nach Bedarf.
  3. Werfen Sie einen Blick auf diese Dokumentation für weitere Fehler Erklärung APNs Provider API