kreait / firebase-bundle

A Symfony Bundle for the Firebase PHP Admin SDK
https://github.com/kreait/firebase-php
MIT License
140 stars 27 forks source link

Unable to setup bundle, json is ignored #8

Closed Eldhelion closed 6 years ago

Eldhelion commented 6 years ago

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 commented 6 years ago

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.

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 commented 6 years ago

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 commented 6 years ago

1. 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.

2. 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.