Giter Site home page Giter Site logo

Token problem about php-firebase-cloud-messaging HOT 4 OPEN

sngrl avatar sngrl commented on August 31, 2024
Token problem

from php-firebase-cloud-messaging.

Comments (4)

ElRochito avatar ElRochito commented on August 31, 2024 1

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

from php-firebase-cloud-messaging.

redjanym avatar redjanym commented on August 31, 2024

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

from php-firebase-cloud-messaging.

ElRochito avatar ElRochito commented on August 31, 2024
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();
}

from php-firebase-cloud-messaging.

redjanym avatar redjanym commented on August 31, 2024

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();
}

from php-firebase-cloud-messaging.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.