push notifications wenn App nicht läuft

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • push notifications wenn App nicht läuft

    Hallo zusammen,

    ich habe eine App geschrieben, die push notifications empfängt. Dies klappt aber nur, wenn die App im Vorder- oder Hintergrund läuft. Ich erhalte auf dem Device keine notification, wenn die App nicht läuft.
    Woran kann das liegen???

    Gruß Berthold
  • Hallo Matz,

    ich wollte die Fragestellung so einfach wie möglich halten.

    Also ich habe im iOS Target die PushNotifications aktiviert. Die Zertifikate erzeugt und auf dem Server abgelegt. Über ein PHP-Skript kann ich vom Server aus die Notifications absenden. Diese kommen auch an, wenn die App im Vorder- oder Hintergrund läuft.
    Somit sollte der Code der App und auf dem Server sowie das Zertifikat korrekt sein, denn sonst käme keine Notification an wenn die App läuft. Oder habe ich da einen Denkfehler? Nur wenn die App aus dem Speicher geworfen wurde kommt auf dem Gerät keine Notification an. Ich kann nirgends eine Einstellung finden, die dieses Verhalten bewirken würde.

    Auf dem Server läuft folgendes Script:

    PHP-Quellcode

    1. <?php
    2. require('addOnConfig.php');
    3. ////////////////////////////////////////////////////////////////////////////////
    4. function sendNotification($deviceToken){
    5. $passphrase = 'XXXXXXXX';
    6. $message = 'Neue Anzeige im TR-Marktplatz';
    7. $url = 'TR-Marktplatz.de';
    8. $ctx = stream_context_create();
    9. stream_context_set_option($ctx, 'ssl', 'local_cert', 'TRInfo.pem');
    10. stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
    11. // Open a connection to the APNS server
    12. $fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
    13. if (!$fp)
    14. exit("Failed to connect: $err $errstr" . PHP_EOL);
    15. // echo 'Connected to APNS <br>' . PHP_EOL;
    16. // Create the payload body
    17. $body['aps'] = array(
    18. 'alert' => $message,
    19. 'sound' => 'default',
    20. 'link_url' => $url,
    21. );
    22. // Encode the payload as JSON
    23. $payload = json_encode($body);
    24. // Build the binary notification
    25. $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
    26. // Send it to the server
    27. $result = fwrite($fp, $msg, strlen($msg));
    28. /*
    29. if (!$result)
    30. echo 'Message not delivered <br>' . PHP_EOL;
    31. else
    32. echo 'Message successfully delivered <br>' . PHP_EOL;
    33. */
    34. // Close the connection to the server
    35. fclose($fp);
    36. }
    37. ////////////////////////////////////////////////////////////////////////////////
    38. $db=mysqli_connect(AddOn_SQL_HOST,AddOn_SQL_USER,AddOn_SQL_PASSWORD);
    39. // Check connection
    40. if (mysqli_connect_errno()) {
    41. echo "Failed to connect to MySQL: " . mysqli_connect_error();
    42. }
    43. $db->select_db(AddOn_SQL_DBNAME);
    44. $query = sprintf("select * from `trfpush`;");
    45. $result = mysqli_query($db, $query);
    46. while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
    47. $deviceToken = $row["token"];
    48. sendNotification($deviceToken);
    49. }
    50. ?>
    Alles anzeigen
    Ich hoffe die Info reicht.

    Gruß Berthold