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

Uncaught Error: Call to a member function post() on null in Client.php 62 #28

Open
shiyasVp opened this issue Apr 2, 2018 · 8 comments

Comments

@shiyasVp
Copy link

shiyasVp commented Apr 2, 2018

in line 62, it is guzzleHttp object. But it is null

Fatal error: Uncaught Error: Call to a member function post() on null in /var/www/html/magento2/vendor/sngrl/php-firebase-cloud-messaging/src/Client.php:62 Stack trace: #0

i am using this in Magento 2 Progressive Web app plugin by tigren

@Demonslay335
Copy link

Are you sure you are following the example and setting a GuzzleHttp Client via injectGuzzleHttpClient()? You have to provide your own client for it to use, otherwise it is default null.

@sumeetbajaj51
Copy link

sumeetbajaj51 commented Jun 3, 2018

is anyone there for this solution i am also getting same problem ?

Fatal error: Uncaught Error: Call to a member function post() on null in /home/magentoo/public_html/vendor/sngrl/php-firebase-cloud-messaging/src/Client.php:62 Stack trace: #0

@Demonslay335
Copy link

@sumeetbajaj51
As I mentioned above, you need to follow the example code and supply the library with your own GuzzelHttp Client.

$client->injectGuzzleHttpClient(new \GuzzleHttp\Client());

If you are still having issues even with including this, please share your code for review.

@sumeetbajaj51
Copy link

sumeetbajaj51 commented Jun 5, 2018

@Demonslay335
Here is my file code where i am getting error---

namespace sngrl\PhpFirebaseCloudMessaging;
use GuzzleHttp;
/**

  • @author sngrl
    */
    class Client implements ClientInterface
    {
    const DEFAULT_API_URL = 'https://fcm.googleapis.com/fcm/send';
    const DEFAULT_TOPIC_ADD_SUBSCRIPTION_API_URL = 'https://iid.googleapis.com/iid/v1:batchAdd';
    const DEFAULT_TOPIC_REMOVE_SUBSCRIPTION_API_URL = 'https://iid.googleapis.com/iid/v1:batchRemove';

    private $apiKey;
    private $proxyApiUrl;
    private $guzzleClient;

    public function injectGuzzleHttpClient(GuzzleHttp\ClientInterface $client)
    {
    $this->guzzleClient = $client;
    }

    /**

    /**

    • people can overwrite the api url with a proxy server url of their own
    • @param string $url
    • @return \sngrl\PhpFirebaseCloudMessaging\Client
      */
      public function setProxyApiUrl($url)
      {
      $this->proxyApiUrl = $url;
      return $this;
      }

    /**

    • sends your notification to the google servers and returns a guzzle repsonse object

    • containing their answer.

    • @param Message $message

    • @return \Psr\Http\Message\ResponseInterface

    • @throws \GuzzleHttp\Exception\RequestException
      */
      public function send(Message $message)
      {

      return $this->guzzleClient->post(
      $this->getApiUrl(),
      [
      'headers' => [
      'Authorization' => sprintf('key=%s', $this->apiKey),
      'Content-Type' => 'application/json'
      ],
      'body' => json_encode($message)
      ]
      );
      }

    /**

    • @param integer $topic_id
    • @param array|string $recipients_tokens
    • @return \Psr\Http\Message\ResponseInterface
      */
      public function addTopicSubscription($topic_id, $recipients_tokens)
      {
      return $this->processTopicSubscription($topic_id, $recipients_tokens, self::DEFAULT_TOPIC_ADD_SUBSCRIPTION_API_URL);
      }

    /**

    • @param integer $topic_id
    • @param array|string $recipients_tokens
    • @return \Psr\Http\Message\ResponseInterface
      */
      public function removeTopicSubscription($topic_id, $recipients_tokens)
      {
      return $this->processTopicSubscription($topic_id, $recipients_tokens, self::DEFAULT_TOPIC_REMOVE_SUBSCRIPTION_API_URL);
      }

    /**

    • @param integer $topic_id

    • @param array|string $recipients_tokens

    • @param string $url

    • @return \Psr\Http\Message\ResponseInterface
      */
      protected function processTopicSubscription($topic_id, $recipients_tokens, $url)
      {
      if (!is_array($recipients_tokens))
      $recipients_tokens = [$recipients_tokens];

      return $this->guzzleClient->post(
      $url,
      [
      'headers' => [
      'Authorization' => sprintf('key=%s', $this->apiKey),
      'Content-Type' => 'application/json'
      ],
      'body' => json_encode([
      'to' => '/topics/' . $topic_id,
      'registration_tokens' => $recipients_tokens,
      ])
      ]
      );
      }

    private function getApiUrl()
    {
    return isset($this->proxyApiUrl) ? $this->proxyApiUrl : self::DEFAULT_API_URL;
    }
    }

@saranyanuraj
Copy link

saranyanuraj commented Jun 6, 2018

I have also got this error

@Demonslay335
Copy link

@shiyasVp
That's the code from this library, that's not helpful in troubleshooting your issue. You need to share the code where you call this library from. You have to use the injectGuzzleHttpClient() method to inject your own GuzzleHttp\Client instance.

@koushikch7
Copy link

Does anyone got solutions for this? its been long since the issue is notified but no relevant action as been taken why?

@Demonslay335
Copy link

@koushikch7
Please post your code. The OP failed to cooperate, thus we could not troubleshoot any further.

As the documentation examples show, you need to inject your own Guzzle instance. If an instance is not injected, it is default null and thus the crash.

$client->injectGuzzleHttpClient(new \GuzzleHttp\Client());

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

5 participants