php-firebase-cloud-messaging
php-firebase-cloud-messaging copied to clipboard
Token problem
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
Can you paste an example of the code you are using?
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();
}
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();
}
Ohhhhh i'm so sorry for inconvenience !!! I should have to see myself :(