1

I am a web developer and hired ios developer to create native app for my client. The client wants to have push notifications. We are using firebase but the problem is that i can't send push notifications from my php script, because of the InvalidRegistration error. I've read all over stackoverflow about this error and still dont have a clue how to fix it.

I am saving the device_token in a mysql table CHAR(64).

This is my script:

if (isset($_POST['submit'])) {
// API access key from Google API's Console
define('API_ACCESS_KEY', 'API');

$users = $db->getResults("device_id", "users", "device_id != '' AND travel_stay = '" . $_POST['group'] . "'");
foreach ($users as $user) {
    $registrationIds = (string)$user['device_id'];
    // prep the bundle
    $msg = array(
        'message'       => $_POST['description'],
        'title'         => $_POST['title'],
        'subtitle'      => 'This is a subtitle. subtitle',
        'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
        'vibrate'       => 1,
        'sound'         => 1,
        'largeIcon'     => 'large_icon',
        'smallIcon'     => 'small_icon');
    $fields = array('to' => $registrationIds, 'data' => $msg);

    $headers = array('Authorization: key=' . API_ACCESS_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, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);
    curl_close($ch);
    echo $result;
}

} ?>

I have no idea what is wrong with it, is the device_token sent wrong? How can i fix it.

  • I am getting the same issue. Did you manage to resolve this? – AndyW Mar 09 '18 at 13:36
  • have you solved it @AndyW – Abhishek Apr 29 '19 at 09:36
  • @dimitar you are saving device_token char 64 that is the issue then you should change it to varchar 200 otherwise token will not be stored properly and notification will not be sent on invalid device_token – Abhishek Apr 29 '19 at 09:50
  • @Abhishek I think for me it was having a too old version of curl, PHP, OpenSSL, etc on the server. My solution: https://stackoverflow.com/questions/47646162/unexpected-http-1-x-request-post-3-device-xxxx/55147621#55147621 – AndyW Apr 30 '19 at 12:11

0 Answers0