Skip to content
This repository has been archived by the owner on Jun 6, 2021. It is now read-only.

Does this bundle currently support FCM? #160

Open
sterichards opened this issue Apr 4, 2017 · 6 comments
Open

Does this bundle currently support FCM? #160

sterichards opened this issue Apr 4, 2017 · 6 comments

Comments

@sterichards
Copy link

I'm unsure if the bundle would require any updates to support FCM or does it support GCM handle FCM without any changes to the project?

@Hasib-rafi1
Copy link

Hasib-rafi1 commented Jul 18, 2017

Is there any update on it ?

@jkosmetos
Copy link

Any update on this?

@sterichards
Copy link
Author

@Hasib-rafi1 @jkosmetos - I wrote my own FCM code in the end. I can share that script with you if you like?

@jkosmetos
Copy link

@sterichards - That would be great, thanks man!

@sterichards
Copy link
Author

sterichards commented Aug 29, 2017

Below is my class. I did initially try and get this using working Guzzle but I failed miserably and as time was a huge constraint, I send a manual CURL command via the command line. I guess you want to tidy that up and if you do, using Guzzle, please send me the Guzzle code you use

Ste,

`
class AndroidController extends MobileDevicePushNotificationController
{
protected $apiEndpoint = 'https://fcm.googleapis.com/fcm/send';
protected $serverKey;

/**
 * @param ContainerInterface $container
 */
public function __construct(ContainerInterface $container)
{
    $this->container = $container;
    $this->serverKey = $this->container->getParameter('fcm_server_key');
}

public function send()
{
    foreach ($this->getUserDeviceTokens($this->getUser()) as $deviceToken) {

        $body = $this->getBody();
        $body['to'] = (string) $deviceToken;
        $body['priority'] = (int) $this->getPriority();

        $body = addcslashes(
            json_encode(
                $body
            ),
            '"'
        );

        $curl_string = <<<CURLSTRING

curl -X POST --header "Authorization: key=$this->serverKey" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "$body"
CURLSTRING;

        // Manually run CuRL command from the command line :(
        $responseBody = json_decode(exec($curl_string), 1);

        // Check response from FCM to see if we need to remove any deregistered tokens from our database
        if (array_key_exists('results', $responseBody)) {
            foreach ($responseBody['results'] as $result) {
                if (array_key_exists('error', $result)) {
                    if ($result['error'] == 'InvalidRegistration') {
                        $this->removeTokenFromDatabase($deviceToken);
                    }
                }
            }
        }
    }
}

public function removeTokenFromDatabase($token)
{
    $deviceToken = $this->getObjectManager()->getRepository('MyBundle:MobileDeviceToken')->findBy([
        'user' => $this->getUser(),
        'token' => $token
    ]);

    foreach ($deviceToken as $token) {
        $this->getObjectManager()->remove($token);
    }

    $this->getObjectManager()->flush();
}

}`

@romeugodoi
Copy link

Is this bundle maintained yet? Anyone knows another similar package that is?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants