php-firebase-cloud-messaging icon indicating copy to clipboard operation
php-firebase-cloud-messaging copied to clipboard

Token problem

Open ElRochito opened this issue 8 years ago • 4 comments

Hi,

I've got this error when I try to send a push notification :

JSON_PARSING_ERROR: Unexpected token END OF FILE at position 0.

I don't see how to fix it

Thanks

ElRochito avatar Oct 24 '17 07:10 ElRochito

Can you paste an example of the code you are using?

redjanym avatar Oct 24 '17 14:10 redjanym

use sngrl\PhpFirebaseCloudMessaging\Client;
use sngrl\PhpFirebaseCloudMessaging\Message;
use sngrl\PhpFirebaseCloudMessaging\Notification;
use sngrl\PhpFirebaseCloudMessaging\Recipient\Device;

$defaults = array(
    "title"        => "Title",
    "message"      => "Message",
    "priority"     => "normal",
    "device_token" => "<my_device_token>",
);

extract($defaults);

require_once WP_CONTENT_DIR . "/../../vendor/autoload.php";
$client = new Client();
$client->setApiKey("server_key");
$client->injectGuzzleHttpClient(new \GuzzleHttp\Client());

$message = new Message();
$message->setPriority($priority);
$message->addRecipient(new Device($device_token));
$message->setNotification(new Notification($title, $message));
$message->setData(array(
    "title" => $title,
    "body"  => $message,
));
try {
    $response = $client->send($message);
} catch (Exception $e) {
    echo $e->getMessage();
}

ElRochito avatar Oct 24 '17 16:10 ElRochito

When extracting the $defaults array here:

$defaults = array(
    "title"        => "Title",
    "message"      => "Message",
    "priority"     => "normal",
    "device_token" => "<my_device_token>",
);

extract($defaults);

You are creating a $message variable for the body of the notification, but then you create a new $message variable and give the value of the Message object: $message = new Message();

Then when you set the data of the Notification you pass as body of notification the new Message object:

$message->setData(array(
    "title" => $title,
    "body"  => $message,
));

This causes a malformed JSON when the HTTP Client encodes it before doing the request. Instead a valid code would be this:

use sngrl\PhpFirebaseCloudMessaging\Client;
use sngrl\PhpFirebaseCloudMessaging\Message;
use sngrl\PhpFirebaseCloudMessaging\Notification;
use sngrl\PhpFirebaseCloudMessaging\Recipient\Device;

$defaults = array(
    "title"        => "Title",
    "body_message"      => "Message",
    "priority"     => "normal",
    "device_token" => "<my_device_token>",
);

extract($defaults);

require_once WP_CONTENT_DIR . "/../../vendor/autoload.php";
$client = new Client();
$client->setApiKey("server_key");
$client->injectGuzzleHttpClient(new \GuzzleHttp\Client());

$message = new Message();
$message->setPriority($priority);
$message->addRecipient(new Device($device_token));
$message->setNotification(new Notification($title, $body_message));
$message->setData(array(
    "title" => $title,
    "body"  => $body_message,
));
try {
    $response = $client->send($message);
} catch (Exception $e) {
    echo $e->getMessage();
}

redjanym avatar Oct 24 '17 19:10 redjanym

Ohhhhh i'm so sorry for inconvenience !!! I should have to see myself :(

ElRochito avatar Oct 24 '17 19:10 ElRochito