Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to setup bundle, json is ignored #8

Closed
Eldhelion opened this issue Jul 20, 2018 · 3 comments
Closed

Unable to setup bundle, json is ignored #8

Eldhelion opened this issue Jul 20, 2018 · 3 comments

Comments

@Eldhelion
Copy link

1. Which version of kreait/firebase-php and kreait/firebase-bundle are you using?

"kreait/firebase-bundle": "1.0"
kreait/firebase-php ^4.0

2. Which version of PHP are you using?

7.1.9

3. What's the issue? and 4. Code that lead to the issue

I setup the bundle as show in the readme
my config.yml is

kreait_firebase:
    projects:
        first:
            public: true
            # Optional: If set to true, this project is used when
            # using Kreait\Firebase as a type hint for dependency injection
            #default: true
            credentials: '%kernel.root_dir%/config/model-firebase-adminsdk.json'
            database_uri: 'https://model.firebaseio.com/'
            alias: 'firebase'

The php I created more or less as done in the app:fcm:send-message example ( https://github.com/jeromegamez/firebase-php-examples )

            $messaging = $this->container->get('firebase')->getMessaging();

            $message = $message->setTitle($message->getTitle())->setBody($message->getBody());

            $responseData = $messaging->send($message);

But despite having indicated the path to the json in the config.yml, I'm receiving this error

Kreait\Firebase\ServiceAccount\Discovery\FromEnvironmentVariable: The environment variable "FIREBASE_CREDENTIALS" is not set.
Kreait\Firebase\ServiceAccount\Discovery\FromEnvironmentVariable: The environment variable "GOOGLE_APPLICATION_CREDENTIALS" is not set.
Kreait\Firebase\ServiceAccount\Discovery\FromGoogleWellKnownFile: The well known file is not readable or invalid

also, the doc, say that I could set default: true, but decommenting this line result in

InvalidConfigurationException

Unrecognized option "default" under "kreait_firebase.projects.first"
@jeromegamez
Copy link
Member

Unfortunately, I am not able to reproduce the issue, I tried it by performing the following steps:

$ composer create-project symfony/skeleton fb-bundle-test
$ cd fb-bundle-test
$ composer require kreait/firebase-bundle
$ cp /path/to/my/credentials.json config/service_account_credentials.json
# config/packages/firebase.yaml
kreait_firebase:
    projects:
        first:
            default: true
            credentials: '%kernel.project_dir%/config/service_account_credentials.json'
            alias: 'firebase'
# src/Command/AppTestCommand.php
<?php

namespace App\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class AppTestCommand extends ContainerAwareCommand
{
    protected static $defaultName = 'app:test';

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $firebase = $this->getContainer()->get('firebase');
        
        echo json_encode($firebase->getDatabase()->getRules(), true);
    }
}

bin/console app:test then returned the database rules of my projects.

  • You are specifying that you're using "1.0" as a constraint for the bundle - I recommend using "^1.0" as a version constraint, this ensures that you are always on the stable release for that main version (the current release is 1.1.1). composer show | grep kreait/firebase will show you the currently installed versions of the kreait/firebase-* packages, it should look like this composer_show

  • If your results don't match, you could force newer versions by writing "kreait/firebase-bundle": "^1.1.1" in your composer.json

  • I'm writing all this because of your notice about the dysfunctional default config option - this should definitely be working.

  • When the Firebase PHP SDK doesn't find a credentials file at the specified location, it will try to find one by inspecting environment variables and some default locations (this is needed in Google Cloud Engine (GCE) environments. That it even tried that is an indication that it can't find or read the credentials at the location '%kernel.root_dir%/config/model-firebase-adminsdk.json'

I am sure you already have checked the release versions and the credentials location, but could you check again? When I tried to reproduce the issue, I wall in the trap of copying the credentials with dashes - but using underscores _ in the yaml file :D

@Eldhelion
Copy link
Author

well, I updated the composer json, and it looks to be working now (both, the default option and locating the json). my bad

But its not fully working yet and I would like to ask 2 more questions

  1. your test return a error, I think it is because I have not created a database in the firebase (and other commands as $firebase->getAuth()->getApiClient()->downloadAccount() return stuff), could you confirm for me?
In JSON.php line 58:

  [Kreait\Firebase\Exception\InvalidArgumentException]
  json_decode error: Syntax error


Exception trace:
 Kreait\Firebase\Util\JSON::decode() at C:\Users\Aptor02\Documents\Projetos\Sistemas\asktospekaer\vendor\kreait\firebase-php\src\Firebase\Database\ApiClient.php:28
 Kreait\Firebase\Database\ApiClient->get() at C:\Users\Aptor02\Documents\Projetos\Sistemas\asktospekaer\vendor\kreait\firebase-php\src\Firebase\Database.php:105

2. About my code, I'm trying to do

$messaging = $this->container->get('firebase')->getMessaging();

            $msg = array(
                'condition' => "App == 'br.com.aptor.model'",
                'notification' => array(
                    'title' => 'titulo',
                    'body' => 'corpo',
                )
            );

            $responseData = $messaging->send($msg);

but receiving error

Kreait\Firebase\Exception\Messaging\InvalidMessage:
Client error: `POST https://fcm.googleapis.com/v1/projects/aptor-model/messages:send` resulted in a `400 Bad Request` response:
{
  "error": {
    "code": 400,
    "message": "Request contains an invalid argument.",
    "status": "INVALID_ARGUMENT" (truncated...)


  at vendor\kreait\firebase-php\src\Firebase\Exception\MessagingException.php:76
  at Kreait\Firebase\Exception\MessagingException->withResponse(object(Response))
     (vendor\kreait\firebase-php\src\Firebase\Exception\MessagingException.php:51)
  at Kreait\Firebase\Exception\MessagingException::fromRequestException(object(ClientException))
     (vendor\kreait\firebase-php\src\Firebase\Messaging\ApiClient.php:54)
  at Kreait\Firebase\Messaging\ApiClient->request('POST', 'messages:send', array('json' => array('message' => array('condition' => object(Condition), 'notification' => object(Notification)))))
     (vendor\kreait\firebase-php\src\Firebase\Messaging\ApiClient.php:26)

Any idea about what could be the invalid argument or how to discover it?

@jeromegamez
Copy link
Member

Your stack trace shows that the exception is thrown in Firebase\Database\ApiClient.php - so the exception is not triggered by $firebase->getAuth()->getApiClient()->downloadAccount(), but probably by the $firebase->getDatabase()->getRules() command.

I cannot confirm that the error is thrown - I tried reproducing this be deleting the database rules in the Firebase Web console, by deactivating the Database and by emptying my test database - with no success.

I will try to make the errors more descriptive in a future release. In your case, the error is that only topics are supported in the condition field (see https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-json), that means you can't use App as a condition.

You can check if you message is valid as described in https://firebase-php.readthedocs.io/en/latest/cloud-messaging.html#validating-messages with

try {
    $firebase->getMessaging()->validate($msg);
} catch (InvalidMessage $e) {
    print_r($e->errors());
}

In the Firebase PHP SDK, you can specify custom Guzzle Middlewares that can be used e.g. to debug HTTP Requests to the Firebase APIs - this is not yet implemented in the Bundle, but I will look to add this in one of the next releases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants