kreait / laravel-firebase

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

Unable to get Firebase::dynamicLinks to work #92

Closed pratamatama closed 3 years ago

pratamatama commented 3 years ago

I'm having a problem when trying to implement a dynamic link to be passed down to the notificaiton channel, I already followed the instruction from the readme, sets up Env, and other stuff necessary, I didn't make any changes to firebase.php file.

But it keep showing me this error

{#3613
  +"message": "Kreait\Laravel\Firebase\FirebaseProjectManager::getDefaultProject(): Return value must be of type string, null returned"
  +"exception": "TypeError"

What causes this and how can I fix it? Here is my implementation...

<?php

namespace App\Http\Controllers;

// use statements omitted for readability

class WorkspaceController extends Controller
{
    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\RequestWorkspace  $request
     * @return \Illuminate\Http\Response
     */
    public function store(RequestWorkspace $request)
    {
        // unrelated code omitted for readability

        if ($request->has('invites')) {

            foreach ($request->invites as $email) {

                $notRegistered = User::whereEmail($email)->first() == null;

                if ($notRegistered) {
                    $url = $request->getSchemeAndHttpHost() . '/workspaces/' . $workspace->id . '/join?email=' . $email;

                    $dynamicLink = CreateDynamicLink::forUrl($url);

                    $link = Firebase::dynamicLinks()->shortenLongDynamicLink($dynamicLink);

                    Notification::send(
                        new AnonymousNotifiable,
                        new WorkspaceInvitationMail($workspace, $link)
                    );
                }
            }
        }

        return response()->json($workspace, Response::HTTP_CREATED);
    }
}

And here is the full stackTrace if needed

Click to expand ``` {#3613 +"message": "Kreait\Laravel\Firebase\FirebaseProjectManager::getDefaultProject(): Return value must be of type string, null returned" +"exception": "TypeError" +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\kreait\laravel-firebase\src\FirebaseProjectManager.php" +"line": 126 +"trace": array:64 [ 0 => {#3786 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\kreait\laravel-firebase\src\FirebaseProjectManager.php" +"line": 27 +"function": "getDefaultProject" +"class": "Kreait\Laravel\Firebase\FirebaseProjectManager" +"type": "->" } 1 => {#1685 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\kreait\laravel-firebase\src\FirebaseProjectManager.php" +"line": 137 +"function": "project" +"class": "Kreait\Laravel\Firebase\FirebaseProjectManager" +"type": "->" } 2 => {#3459 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php" +"line": 261 +"function": "__call" +"class": "Kreait\Laravel\Firebase\FirebaseProjectManager" +"type": "->" } 3 => {#3289 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\app\Http\Controllers\WorkspaceController.php" +"line": 69 +"function": "__callStatic" +"class": "Illuminate\Support\Facades\Facade" +"type": "::" } 4 => {#3785 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Routing\Controller.php" +"line": 54 +"function": "store" +"class": "App\Http\Controllers\WorkspaceController" +"type": "->" } 5 => {#3461 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php" +"line": 45 +"function": "callAction" +"class": "Illuminate\Routing\Controller" +"type": "->" } 6 => {#3612 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Routing\Route.php" +"line": 254 +"function": "dispatch" +"class": "Illuminate\Routing\ControllerDispatcher" +"type": "->" } 7 => {#671 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Routing\Route.php" +"line": 197 +"function": "runController" +"class": "Illuminate\Routing\Route" +"type": "->" } 8 => {#669 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Routing\Router.php" +"line": 695 +"function": "run" +"class": "Illuminate\Routing\Route" +"type": "->" } 9 => {#67 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php" +"line": 128 +"function": "Illuminate\Routing\{closure}" +"class": "Illuminate\Routing\Router" +"type": "->" } 10 => {#668 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authorize.php" +"line": 45 +"function": "Illuminate\Pipeline\{closure}" +"class": "Illuminate\Pipeline\Pipeline" +"type": "->" } 11 => {#364 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php" +"line": 167 +"function": "handle" +"class": "Illuminate\Auth\Middleware\Authorize" +"type": "->" } 12 => {#670 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php" +"line": 50 +"function": "Illuminate\Pipeline\{closure}" +"class": "Illuminate\Pipeline\Pipeline" +"type": "->" } 13 => {#667 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php" +"line": 167 +"function": "handle" +"class": "Illuminate\Routing\Middleware\SubstituteBindings" +"type": "->" } 14 => {#3784 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php" +"line": 127 +"function": "Illuminate\Pipeline\{closure}" +"class": "Illuminate\Pipeline\Pipeline" +"type": "->" } 15 => {#3464 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php" +"line": 103 +"function": "handleRequest" +"class": "Illuminate\Routing\Middleware\ThrottleRequests" +"type": "->" } 16 => {#3619 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php" +"line": 55 +"function": "handleRequestUsingNamedLimiter" +"class": "Illuminate\Routing\Middleware\ThrottleRequests" +"type": "->" } 17 => {#3293 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php" +"line": 167 +"function": "handle" +"class": "Illuminate\Routing\Middleware\ThrottleRequests" +"type": "->" } 18 => {#845 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php" +"line": 44 +"function": "Illuminate\Pipeline\{closure}" +"class": "Illuminate\Pipeline\Pipeline" +"type": "->" } 19 => {#3292 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php" +"line": 167 +"function": "handle" +"class": "Illuminate\Auth\Middleware\Authenticate" +"type": "->" } 20 => {#3790 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\sanctum\src\Http\Middleware\EnsureFrontendRequestsAreStateful.php" +"line": 33 +"function": "Illuminate\Pipeline\{closure}" +"class": "Illuminate\Pipeline\Pipeline" +"type": "->" } 21 => {#3794 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php" +"line": 128 +"function": "Laravel\Sanctum\Http\Middleware\{closure}" +"class": "Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful" +"type": "->" } 22 => {#3621 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php" +"line": 103 +"function": "Illuminate\Pipeline\{closure}" +"class": "Illuminate\Pipeline\Pipeline" +"type": "->" } 23 => {#3298 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\sanctum\src\Http\Middleware\EnsureFrontendRequestsAreStateful.php" +"line": 34 +"function": "then" +"class": "Illuminate\Pipeline\Pipeline" +"type": "->" } 24 => {#3296 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php" +"line": 167 +"function": "handle" +"class": "Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful" +"type": "->" } 25 => {#3295 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php" +"line": 103 +"function": "Illuminate\Pipeline\{closure}" +"class": "Illuminate\Pipeline\Pipeline" +"type": "->" } 26 => {#3793 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Routing\Router.php" +"line": 697 +"function": "then" +"class": "Illuminate\Pipeline\Pipeline" +"type": "->" } 27 => {#3299 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Routing\Router.php" +"line": 672 +"function": "runRouteWithinStack" +"class": "Illuminate\Routing\Router" +"type": "->" } 28 => {#3470 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Routing\Router.php" +"line": 636 +"function": "runRoute" +"class": "Illuminate\Routing\Router" +"type": "->" } 29 => {#3792 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Routing\Router.php" +"line": 625 +"function": "dispatchToRoute" +"class": "Illuminate\Routing\Router" +"type": "->" } 30 => {#3471 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php" +"line": 166 +"function": "dispatch" +"class": "Illuminate\Routing\Router" +"type": "->" } 31 => {#3297 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php" +"line": 128 +"function": "Illuminate\Foundation\Http\{closure}" +"class": "Illuminate\Foundation\Http\Kernel" +"type": "->" } 32 => {#3791 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php" +"line": 21 +"function": "Illuminate\Pipeline\{closure}" +"class": "Illuminate\Pipeline\Pipeline" +"type": "->" } 33 => {#3795 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull.php" +"line": 31 +"function": "handle" +"class": "Illuminate\Foundation\Http\Middleware\TransformsRequest" +"type": "->" } 34 => {#3301 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php" +"line": 167 +"function": "handle" +"class": "Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull" +"type": "->" } 35 => {#3620 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php" +"line": 21 +"function": "Illuminate\Pipeline\{closure}" +"class": "Illuminate\Pipeline\Pipeline" +"type": "->" } 36 => {#3472 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TrimStrings.php" +"line": 40 +"function": "handle" +"class": "Illuminate\Foundation\Http\Middleware\TransformsRequest" +"type": "->" } 37 => {#3796 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php" +"line": 167 +"function": "handle" +"class": "Illuminate\Foundation\Http\Middleware\TrimStrings" +"type": "->" } 38 => {#3626 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php" +"line": 27 +"function": "Illuminate\Pipeline\{closure}" +"class": "Illuminate\Pipeline\Pipeline" +"type": "->" } 39 => {#3305 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php" +"line": 167 +"function": "handle" +"class": "Illuminate\Foundation\Http\Middleware\ValidatePostSize" +"type": "->" } 40 => {#3799 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance.php" +"line": 86 +"function": "Illuminate\Pipeline\{closure}" +"class": "Illuminate\Pipeline\Pipeline" +"type": "->" } 41 => {#3632 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php" +"line": 167 +"function": "handle" +"class": "Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance" +"type": "->" } 42 => {#3803 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\fruitcake\laravel-cors\src\HandleCors.php" +"line": 52 +"function": "Illuminate\Pipeline\{closure}" +"class": "Illuminate\Pipeline\Pipeline" +"type": "->" } 43 => {#3479 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php" +"line": 167 +"function": "handle" +"class": "Fruitcake\Cors\HandleCors" +"type": "->" } 44 => {#3802 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\fideloper\proxy\src\TrustProxies.php" +"line": 57 +"function": "Illuminate\Pipeline\{closure}" +"class": "Illuminate\Pipeline\Pipeline" +"type": "->" } 45 => {#3631 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php" +"line": 167 +"function": "handle" +"class": "Fideloper\Proxy\TrustProxies" +"type": "->" } 46 => {#3478 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php" +"line": 103 +"function": "Illuminate\Pipeline\{closure}" +"class": "Illuminate\Pipeline\Pipeline" +"type": "->" } 47 => {#3804 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php" +"line": 141 +"function": "then" +"class": "Illuminate\Pipeline\Pipeline" +"type": "->" } 48 => {#3310 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php" +"line": 110 +"function": "sendRequestThroughRouter" +"class": "Illuminate\Foundation\Http\Kernel" +"type": "->" } 49 => {#3481 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php" +"line": 509 +"function": "handle" +"class": "Illuminate\Foundation\Http\Kernel" +"type": "->" } 50 => {#3634 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php" +"line": 475 +"function": "call" +"class": "Illuminate\Foundation\Testing\TestCase" +"type": "->" } 51 => {#3805 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php" +"line": 334 +"function": "json" +"class": "Illuminate\Foundation\Testing\TestCase" +"type": "->" } 52 => {#3311 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\tests\Feature\WorkspaceTest.php" +"line": 88 +"function": "postJson" +"class": "Illuminate\Foundation\Testing\TestCase" +"type": "->" } 53 => {#3482 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\phpunit\phpunit\src\Framework\TestCase.php" +"line": 1526 +"function": "test_workspace_invitation_notification_should_be_sent_when_invites_field_provided" +"class": "Tests\Feature\WorkspaceTest" +"type": "->" } 54 => {#3635 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\phpunit\phpunit\src\Framework\TestCase.php" +"line": 1132 +"function": "runTest" +"class": "PHPUnit\Framework\TestCase" +"type": "->" } 55 => {#3806 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\phpunit\phpunit\src\Framework\TestResult.php" +"line": 722 +"function": "runBare" +"class": "PHPUnit\Framework\TestCase" +"type": "->" } 56 => {#3312 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\phpunit\phpunit\src\Framework\TestCase.php" +"line": 884 +"function": "run" +"class": "PHPUnit\Framework\TestResult" +"type": "->" } 57 => {#3483 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\phpunit\phpunit\src\Framework\TestSuite.php" +"line": 677 +"function": "run" +"class": "PHPUnit\Framework\TestCase" +"type": "->" } 58 => {#3636 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\phpunit\phpunit\src\Framework\TestSuite.php" +"line": 677 +"function": "run" +"class": "PHPUnit\Framework\TestSuite" +"type": "->" } 59 => {#3807 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\phpunit\phpunit\src\Framework\TestSuite.php" +"line": 677 +"function": "run" +"class": "PHPUnit\Framework\TestSuite" +"type": "->" } 60 => {#3313 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\phpunit\phpunit\src\TextUI\TestRunner.php" +"line": 667 +"function": "run" +"class": "PHPUnit\Framework\TestSuite" +"type": "->" } 61 => {#3484 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\phpunit\phpunit\src\TextUI\Command.php" +"line": 143 +"function": "run" +"class": "PHPUnit\TextUI\TestRunner" +"type": "->" } 62 => {#3637 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\phpunit\phpunit\src\TextUI\Command.php" +"line": 96 +"function": "run" +"class": "PHPUnit\TextUI\Command" +"type": "->" } 63 => {#3808 +"file": "C:\Users\Fubuki\Documents\projects\modular-server\vendor\phpunit\phpunit\phpunit" +"line": 61 +"function": "main" +"class": "PHPUnit\TextUI\Command" +"type": "::" } ] } FAIL Tests\Feature\WorkspaceTest ⨯ workspace invitation notification should be sent when invites field provided --- • Tests\Feature\WorkspaceTest > workspace invitation notification should be sent when invites field provided Response status code [500] does not match expected 201 status code. Failed asserting that 500 is identical to 201. at C:\Users\Fubuki\Documents\projects\modular-server\tests\Feature\WorkspaceTest.php:90 86▕ ]); 87▕ 88▕ $response = $this->postJson(route('workspaces.store'), $workspace->toArray()); 89▕ $response->dump(); ➜ 90▕ $response->assertCreated(); 91▕ 92▕ foreach ($invites as $email) { 93▕ Notification::assertSentTo( 94▕ new AnonymousNotifiable, 1 C:\Users\Fubuki\Documents\projects\modular-server\vendor\phpunit\phpunit\phpunit:61 PHPUnit\TextUI\Command::main() Tests: 1 failed Time: 4.67s ```
jeromegamez commented 3 years ago

Which versions of the package and the SDK do you have installed? You can check with composer show | grep kreait - the current versions are 5.20.1 for the SDK and 3.1.0 for the laravel package. 🤞

pratamatama commented 3 years ago

Hello @jeromegamez , thanks for fast response, it shows me this..

kreait/clock                       1.1.0   A PHP 7.0 compatible clock abstraction
kreait/firebase-php                5.20.1  Firebase Admin SDK
kreait/firebase-tokens             1.15.0  A library to work with Firebase tokens
kreait/laravel-firebase            3.1.0   A Laravel package for the Firebase PHP Admin SDK
jeromegamez commented 3 years ago

I wasn't able to reproduce the issue in my local Laravel Demo Application (stock Laravel 8.45), but I will try further...

Do I see this correctly that this is in the context of running tests? I haven't tested the package in that context (yet), but from the top of my head, it seems that the default config is either not loaded at all when running tests, or - if a config was loaded - that it couldn't be parsed correctly 🤔

Does it work, when you access the route "for real"?

PS: This has most likely nothing to do with the problem, but if you want do create a dynamic link from a "normal" URL, I believe you should use the createDynamicLink() method instead of the shortenLongDynamicLink() method (https://firebase-php.readthedocs.io/en/5.x/dynamic-links.html#create-a-dynamic-link)

pratamatama commented 3 years ago

Ah yes, it was executed from the test, and I've just tested it directly via Postman. It gave me the same error.

image

pratamatama commented 3 years ago

Also, as for the createDynamicLink(), I already use it using this facade CreateDynamicLink::forUrl($url);, are they different?

it was specified here https://firebase-php.readthedocs.io/en/5.x/dynamic-links.html#using-actions

Edit:

Sorry, my fault.. I mis-read your comment, let me fix that.. Updated the method, still getting the same eror 😅

pratamatama commented 3 years ago

I somehow fixed it.. This happen because the values of the variables defined on the .env file are not being loaded (both on testing environment and real one). Tested it by adding dd(env('FIREBASE_CREDENTIALS')); at the top right before the if statement and it shows null.

Got it fixed by clearing the cache

php artisan config:cache
php artisan config:clear

The package works just fine in both environment, I'm closing the issue now. And thank you so much @jeromegamez for your support!

Have good day!

jeromegamez commented 3 years ago

I'm glad you got it working because I'm not sure I would have thought of caching issues 😅. Thanks for sharing the solution!