kreait / firebase-bundle

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

kreait_firebase.my_project.messaging #23

Closed hsnbsst closed 4 years ago

hsnbsst commented 4 years ago

hello how should i use these different accounts, i want to use a few different accounts for example: kreait_firebase.my_project.messaging kreait_firebase.other1.messaging kreait_firebase.other2.messaging

jeromegamez commented 4 years ago

It's written down in the README: https://github.com/kreait/firebase-bundle/blob/master/README.md#configuration - if something is unclear, please be more specific 🙏

hsnbsst commented 4 years ago

okay i saw it but i can explain in more detail how to use the controller too, $this->container->get("kreait_firebase.my_project.messaging"); I get an error when I use it this way

jeromegamez commented 4 years ago

The services are private by default, if you want to get them as you described, in the configuration, set public: true for the projects you want to access this way.

hsnbsst commented 4 years ago

Service "kreait_firebase.my_project.messaging" not found: even though it exists in the app's container, the container inside "App\Controller\AdminController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session" and "twig" services. Try using dependency injection instead.

he did as you said but still gives an error like this

jeromegamez commented 4 years ago

The error message is quite specific - the service is public and exists, but you can't access it in your application. In that case, I would do as the error message suggests: use Dependency Injection. You can do this in Symfony in your service definition files, something like:

// src/Controller/MyController.php

namespace App\Controller;

use Kreait\Firebase\Messaging;

class MyController
{
    /** @var Messaging */
    private $messaging;

    public function __construct(Messaging $messaging)
    {
        $this->messaging = $messaging;
    }
}
# config/services.yaml

services:
    App\Controller\MyController:
        arguments: ['@kreait_firebase.my_project.messaging']
hsnbsst commented 4 years ago

I am designing a panel like this, for example, as a lot of applications and I will choose from these applications and notify the application I want from the same place, so if I do this way, how do I access the applications from the data in the database and how do I choose it? I hope I could tell I don't know English, I write with translation service, Thank you

hsnbsst commented 4 years ago

namespace App\Service; MyService

    public function sendMessage($pbaslik,$picerik,$pplayerid,$paketAdi,$image=null){

        $factori = new Factory();
        $mesaji = $factori->withServiceAccount($this->params->get('kernel.project_dir').'/config/firebase-hizmet-'.$paketAdi.'.json')->createMessaging();

        if ($pplayerid == "toplu") {
            $message = CloudMessage::withTarget('topic', "AllTopic")
                ->withNotification(Notification::create($pbaslik, $picerik,$image))
                ->withData(["click_action"=> "FLUTTER_NOTIFICATION_CLICK", "ekran"=> "bildirim"]);
        } else {
            $message = CloudMessage::withTarget('topic', $pplayerid)
                ->withNotification(Notification::create($pbaslik, $picerik,$image))
                ->withData(["click_action"=> "FLUTTER_NOTIFICATION_CLICK", "ekran"=> "bildirim"]);
        }

        try {
            $sonuc =   $mesaji->send($message);
        } catch (MessagingException $e) {
            $sonuc = $e->errors();
        } catch (FirebaseException $e) {
            $sonuc = $e->getMessage();
        }
        return $sonuc;

    }

I am trying this method for now

jeromegamez commented 4 years ago

Your code has been translated as well 😅 (no problem, I understood what you meant).

I don't know what to tell you - if you use dependency injection as recommended by Symfony (or in general) and use the Symfony mechanics to inject the Firebase Services in your own services, you should be fine - but then, don't use the Factory directly. The purpose of the Bundle is to avoid this.

If you prefer to use the Factory directly, you can of course do so, but then I don't know what you need the bundle for 😅