kreait / laravel-firebase

A Laravel package for the Firebase PHP Admin SDK
https://github.com/kreait/firebase-php
MIT License
1.04k stars 168 forks source link

I have a problem sending notifications #229

Closed momar3632 closed 3 weeks ago

momar3632 commented 3 weeks ago

Describe the bug

Argument #1 ($reason) must be of type

Installed packages

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^8.1",
        "ext-curl": "*",
        "ext-json": "*",
        "barryvdh/laravel-dompdf": "*",
        "brian2694/laravel-toastr": "^5.54",
        "dgvai/laravel-sslcommerz": "^1.0",
        "doctrine/dbal": "^2.0",
        "fideloper/proxy": "^4.2",
        "fruitcake/laravel-cors": "^2.0",
        "gregwar/captcha": "^1.1",
        "guzzlehttp/guzzle": "^7.0.1",
        "inertiajs/inertia-laravel": "^1.3",
        "intervention/image": "^2.5",
        "iyzico/iyzipay-php": "^2.0",
        "kingflamez/laravelrave": "*",
        "kreait/laravel-firebase": "^5.8",
        "laravel/framework": "^9.0",
        "laravel/passport": "~10.0",
        "laravel/tinker": "^2.0",
        "laravel/ui": "^3.0",
        "lcobucci/jwt": "^4.1",
        "madnest/madzipper": "^1.1",
        "mercadopago/dx-php": "2.4.2",
        "nwidart/laravel-modules": "^9.0",
        "paypal/rest-api-sdk-php": "^1.14",
        "rap2hpoutre/fast-excel": "^3.0",
        "razorpay/razorpay": "^2.5",
        "simplesoftwareio/simple-qrcode": "~4",
        "stancl/tenancy": "^3.8",
        "stripe/stripe-php": "^7.61",
        "tightenco/ziggy": "^2.3",
        "twilio/sdk": "^6.28",
        "unicodeveloper/laravel-paystack": "^1.0",
        "xendit/xendit-php": "^2.19"
    },
    "require-dev": {
        "barryvdh/laravel-debugbar": "^3.5",
        "fzaninotto/faker": "^1.9.1",
        "kitloong/laravel-migrations-generator": "^6.11",
        "mockery/mockery": "^1.3.1",
        "nunomaduro/collision": "^6.1",
        "phpunit/phpunit": "^9.0",
        "spatie/laravel-ignition": "^1.0",
        "tyghaykal/laravel-seed-generator": "^2.0"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true,
        "allow-plugins": {
            "composer/package-versions-deprecated": true
        }
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "files": [
            "app/CentralLogics/banner.php",
            "app/CentralLogics/category.php",
            "app/CentralLogics/product.php",
            "app/CentralLogics/helpers.php",
            "app/CentralLogics/order.php",
            "app/CentralLogics/sms_module.php",
            "app/CentralLogics/Translation.php",
            "app/CentralLogics/Constants.php",
            "app/Library/Constant.php",
            "app/Library/Responses.php",
            "app/CentralLogics/AddFundHook.php"
        ],
        "psr-4": {
            "App\\": "app/",
            "Modules\\": "Modules/",
            "Database\\Seeders\\": "database/seeders/"
        },
        "classmap": [
            "database/factories"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    }
}

PHP version and extensions

composer                2.7.2     Composer package
composer-plugin-api     2.6.0     The Composer Plugin API
composer-runtime-api    2.2.2     The Composer Runtime API
ext-bcmath              8.2.17    The bcmath PHP extension
ext-bz2                 8.2.17    The bz2 PHP extension
ext-calendar            8.2.17    The calendar PHP extension
ext-ctype               8.2.17    The ctype PHP extension
ext-curl                8.2.17    The curl PHP extension
ext-date                8.2.17    The date PHP extension
ext-dom                 20031129  The dom PHP extension
ext-exif                8.2.17    The exif PHP extension
ext-fileinfo            8.2.17    The fileinfo PHP extension
ext-filter              8.2.17    The filter PHP extension
ext-ftp                 8.2.17    The ftp PHP extension
ext-gd                  8.2.17    The gd PHP extension
ext-gettext             8.2.17    The gettext PHP extension
ext-gmp                 8.2.17    The gmp PHP extension
ext-hash                8.2.17    The hash PHP extension
ext-iconv               8.2.17    The iconv PHP extension
ext-imagick             0         The imagick PHP extension (actual version: @PACKAGE_VERSION@)
ext-imap                8.2.17    The imap PHP extension
ext-intl                8.2.17    The intl PHP extension
ext-json                8.2.17    The json PHP extension
ext-libxml              8.2.17    The libxml PHP extension
ext-mbstring            8.2.17    The mbstring PHP extension
ext-mysqli              8.2.17    The mysqli PHP extension
ext-mysqlnd             0         The mysqlnd PHP extension (actual version: mysqlnd 8.2.17)
ext-openssl             8.2.17    The openssl PHP extension
ext-pcntl               8.2.17    The pcntl PHP extension
ext-pcre                8.2.17    The pcre PHP extension
ext-pdo                 8.2.17    The PDO PHP extension
ext-pdo_mysql           8.2.17    The pdo_mysql PHP extension
ext-pdo_sqlite          8.2.17    The pdo_sqlite PHP extension
ext-phar                8.2.17    The Phar PHP extension
ext-posix               8.2.17    The posix PHP extension
ext-pspell              8.2.17    The pspell PHP extension
ext-random              8.2.17    The random PHP extension
ext-reflection          8.2.17    The Reflection PHP extension
ext-session             8.2.17    The session PHP extension
ext-simplexml           8.2.17    The SimpleXML PHP extension
ext-soap                8.2.17    The soap PHP extension
ext-sockets             8.2.17    The sockets PHP extension
ext-sodium              2.0.23    The sodium PHP extension
ext-spl                 8.2.17    The SPL PHP extension
ext-sqlite3             8.2.17    The sqlite3 PHP extension
ext-sysvmsg             8.2.17    The sysvmsg PHP extension
ext-sysvsem             8.2.17    The sysvsem PHP extension
ext-sysvshm             8.2.17    The sysvshm PHP extension
ext-tidy                8.2.17    The tidy PHP extension
ext-tokenizer           8.2.17    The tokenizer PHP extension
ext-xml                 8.2.17    The xml PHP extension
ext-xmlreader           8.2.17    The xmlreader PHP extension
ext-xmlwriter           8.2.17    The xmlwriter PHP extension
ext-xsl                 8.2.17    The xsl PHP extension
ext-zip                 1.21.1    The zip PHP extension
ext-zlib                8.2.17    The zlib PHP extension
lib-bz2                 1.0.6     The bz2 library
lib-curl                7.88.1    The curl library
lib-curl-openssl-fips   1.0.2.11  curl OpenSSL version (1.0.2.11)
lib-curl-zlib           1.2.7     curl zlib version
lib-date-timelib        2022.10   date timelib version
lib-date-zoneinfo       2024.1    zoneinfo ("Olson") database for date
lib-fileinfo-libmagic   540       fileinfo libmagic version
lib-gd                  2.0.35    The gd library
lib-gd-freetype         2.8.0     freetype version for gd
lib-gd-libjpeg          6.2       libjpeg version for gd
lib-gd-libpng           1.5.13    libpng version for gd
lib-gd-libxpm           3.4.11    libxpm version for gd
lib-gmp                 6.0.0     The gmp library
lib-iconv               2.17      The iconv library
lib-icu                 50.2      The ICU unicode and globalization support library
lib-icu-cldr            2.0.78.30 ICU CLDR project version
lib-icu-unicode         6.2.0     ICU unicode version
lib-icu-zoneinfo        2018.5    zoneinfo ("Olson") database for icu
lib-imagick-imagemagick 6.9.10.68 The imagick-imagemagick library
lib-libsodium           1.0.18    The libsodium library
lib-libxml              2.9.1     libxml library version
lib-libxslt             1.1.28    The libxslt library
lib-libxslt-libxml      2.9.1     libxml version libxslt is compiled against
lib-mbstring-libmbfl    1.3.2     mbstring libmbfl version
lib-mbstring-oniguruma  6.8.2     mbstring oniguruma version
lib-openssl             1.0.2.11  OpenSSL 1.0.2k  26 Jan 2017
lib-pcre                10.39     The pcre library
lib-pcre-unicode        14.0.0    PCRE Unicode version support
lib-pdo_sqlite-sqlite   3.7.17    The pdo_sqlite-sqlite library
lib-sqlite3-sqlite      3.7.17    The sqlite3-sqlite library
lib-zip-libzip          1.5.1     The zip-libzip library
lib-zlib                1.2.7     The zlib library
php                     8.2.17    The PHP interpreter
php-64bit               8.2.17    The PHP interpreter, 64bit
php-ipv6                8.2.17    The PHP interpreter, with IPv6 support

Steps to reproduce the issue.

# Insert the commands issued in the terminal if they are needed
# to reproduce the issue. Otherwise, delete this code block.
// Insert the PHP code to reproduce the issue. Please ensure that it is code that
// can be copy pasted to reproduce it.

Error message/Stack trace

Kreait\Firebase\Messaging::Kreait\Firebase\{closure}(): Argument #1 ($reason) must be of type GuzzleHttp\Exception\RequestException, GuzzleHttp\Exception\ConnectException given, called in /home/fireapps/fireapp.com/vendor/guzzlehttp/promises/src/EachPromise.php on line 183

Additional information

At first there was no problem: :"Undefined constant \"CURL_SSLVERSION_TLSv1_2\"

I used this code and solved the problem :

if ( ! defined('CURL_SSLVERSION_TLSv1_2')) {
    define('CURL_SSLVERSION_TLSv1_2', 6);
}

After that, this problem appeared and I was unable to solve it Kreait\Firebase\Messaging::Kreait\Firebase{closure}(): Argument #1 ($reason) must be of type GuzzleHttp\Exception\RequestException, GuzzleHttp\Exception\ConnectException given, called in /home/fireapps/fireapp.com/vendor/guzzlehttp/promises/src/EachPromise.php on line 183

Note that I am using a library https://tenancyforlaravel.com/

And use this code to update config

        if (tenancy()->tenant) {
            $firebaseCredentialsPath = storage_path('tenant' . tenancy()->tenant->id .'/app/private/firebase.json');
            Config::set('firebase.projects.app.credentials.file', $firebaseCredentialsPath);
        }

in provider tenant

            Events\BootstrappingTenancy::class => [
                \App\Listeners\SetFirebaseCredentialsForTenantListener::class,
            ],

code :

class FirebaseService
{
    private $auth;
    private $messaging;

    public function __construct()
    {
//        $auth = app('firebase.auth');
//        $this->auth = $auth;
        $this->messaging = app('firebase.messaging');

    }

    public function sendPushToDevice($deviceToken,$data,$notification = null)
    {
        $body = [
            'token' => $deviceToken,
            "mutable_content" => true,
            'data' => $data, // optional
        ];

        if($notification != null){
            $body['notification'] = $notification;
        }
        $message = CloudMessage::fromArray($body);
        return $this->messaging->send($message);
    }

    public function sendPushToTopic($topic,$data,$notification = null)
    {
        $body = [
            'topic' => $topic,
            "mutable_content" => true,
            'data' => $data, // optional
        ];

        if($notification != null){
            $body['notification'] = $notification;
        }
        $message = CloudMessage::fromArray($body);
        return $this->messaging->send($message);
    }
    }

and i try this

    public function __construct()
    {
        $firebase = (new Factory)
            ->withServiceAccount(config('firebase.projects.app.credentials.file'));

        $messaging = $firebase->createMessaging();
//        $auth = app('firebase.auth');
//        $this->auth = $auth;
        $this->messaging = $messaging;

    }

Same mistake

jeromegamez commented 3 weeks ago

Thank you for the report, there seems to be some place in the SDK in which the exception matching doesn't cover the ConnectException case, I'll look into it.

However - you can't solve the underlying problem by defining the undefined constant, it will still fail because your environment can't make the connection to Firebase. It looks as if you have a fairly recent version of cURL and an SSL binding installed, so I'm at a loss why the connection can't be made 🤔

momar3632 commented 3 weeks ago

I have a server using the CWP control panel with PHP 8.2. Initially, the cURL version was:

curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.90 zlib/1.2.7 libidn/1.28 libssh2/1.8.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp
Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz unix-sockets

However, after some research, I found that CURL_SSLVERSION_TLSv1_2 doesn't work with this version, so I tried to update the version to 7.88.0.

curl 7.88.1 (x86_64-pc-linux-gnu) libcurl/7.88.1 OpenSSL/1.0.2k-fips zlib/1.2.7 nghttp2/1.33.0
Release-Date: 2023-02-20
Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: alt-svc AsynchDNS HSTS HTTP2 HTTPS-proxy IPv6 Largefile libz NTLM NTLM_WB SSL UnixSockets
jeromegamez commented 3 weeks ago

Unfortunately, I can't provide free support for problems that might be related to the environment in which a project using the SDK is running, only share my thoughts and give suggestions when I see something that stands out in a reported issue. Here, I can't see where the Laravel package or the SDK could be the cause of the problem.

I also looked into the code base of the SDK to find out where the "expected RequestException, got ConnectException" error might occur, but couldn't find it. All exceptions related to the messaging component are handled by the MessagingApiExceptionConverter, and it can handle any exception, and explicitly Guzzle's ConnectException (which is a class implementing Psr\Http\Client\NetworkExceptionInterface.

The only suggestion left for me to give is to do a composer update -W to update all composer dependencies in your projects. After that, see which depenencies you can upgrade to a next major version by checking composer outdated.

If you're experiencing the problem in one environment, but not the other, make sure that both environment are identical (same PHP versions, same cURL versions, etc.).

I'm closing this issue now because I currently don't see where the SDK or the Laravel package might be involved in the problem, but if you can find the problem in the underlying SDK, please point me to it and I'll do my best to find a solution.

If you find a solution to the problem, please feel free to share your findings here so that others might benefit from them.

momar3632 commented 3 weeks ago

Actually, the issue was with the environment where the project was located. I reinstalled PHP, and now everything is working without any problems.

Thank you very much

jeromegamez commented 3 weeks ago

Great to hear and that you got it worked out! 🙌🏻