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

Firebase Cloud Messaging support #141

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Command/TestPushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function configure()
->setDescription("Sends a push command to a supplied push token'd device")
->addOption("badge", "b", InputOption::VALUE_OPTIONAL, "Badge number (for iOS devices)", 0)
->addOption("text", "t", InputOption::VALUE_OPTIONAL, "Text message")
->addArgument("service", InputArgument::REQUIRED, "One of 'ios', 'c2dm', 'gcm', 'mac', 'blackberry' or 'windowsphone'")
->addArgument("service", InputArgument::REQUIRED, "One of 'ios', 'c2dm', 'gcm', 'fcm', 'mac', 'blackberry' or 'windowsphone'")
->addArgument("token", InputArgument::REQUIRED, "Authentication token for the service")
->addArgument("payload", InputArgument::OPTIONAL, "The payload data to send (JSON)", '{"data": "test"}')
;
Expand Down Expand Up @@ -111,6 +111,11 @@ protected function getMessageClass($service)
$message = new PushMessage\AndroidMessage();
$message->setGCM(true);

return $message;
case "fcm":
$message = new PushMessage\AndroidMessage();
$message->setFCM(true);

return $message;
case "blackberry":
return new PushMessage\BlackberryMessage();
Expand Down
7 changes: 7 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ protected function addAndroid()
booleanNode("dry_run")->defaultFalse()->end()->
end()->
end()->
arrayNode("fcm")->
canBeUnset()->
children()->
scalarNode("api_key")->isRequired()->cannotBeEmpty()->end()->
booleanNode("use_multi_curl")->defaultValue(true)->end()->
end()->
end()->
end()->
end()->
end()
Expand Down
17 changes: 12 additions & 5 deletions DependencyInjection/RMSPushNotificationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,18 @@ protected function setAndroidConfig(array $config)

// GCM
$this->container->setParameter("rms_push_notifications.android.gcm.enabled", isset($config["android"]["gcm"]));
if (isset($config["android"]["gcm"])) {
$this->container->setParameter("rms_push_notifications.android.gcm.api_key", $config["android"]["gcm"]["api_key"]);
$this->container->setParameter("rms_push_notifications.android.gcm.use_multi_curl", $config["android"]["gcm"]["use_multi_curl"]);
$this->container->setParameter('rms_push_notifications.android.gcm.dry_run', $config["android"]["gcm"]["dry_run"]);
}
// if (isset($config["android"]["gcm"])) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure on the reasoning behind this.

If the user is not interested in GCM configuration (for example, they are only using iOS push notifications), this forces them to supply configuration options. That is why the conditional check is in place, to enable them to leave out the android.gcm key and similar (see the Configuration class which permits the key to be unset/not supplied if not needed).

Commenting this check out will force the user to supply dummy details. Therefore I'd like this to be left in.

This is the same for the FCM change on line 108.

$this->container->setParameter("rms_push_notifications.android.gcm.api_key", isset($config["android"]["gcm"]["api_key"]) ? $config["android"]["gcm"]["api_key"] : null);
$this->container->setParameter("rms_push_notifications.android.gcm.use_multi_curl", isset($config["android"]["gcm"]["use_multi_curl"]) ? $config["android"]["gcm"]["use_multi_curl"] : null);
$this->container->setParameter('rms_push_notifications.android.gcm.dry_run', isset($config["android"]["gcm"]["dry_run"]) ? $config["android"]["gcm"]["dry_run"] : null);
// }

// FCM
$this->container->setParameter("rms_push_notifications.android.fcm.enabled", isset($config["android"]["fcm"]));
// if (isset($config["android"]["fcm"])) {
$this->container->setParameter("rms_push_notifications.android.fcm.api_key", isset($config["android"]["fcm"]["api_key"]) ? $config["android"]["fcm"]["api_key"] : null);
$this->container->setParameter("rms_push_notifications.android.fcm.use_multi_curl", isset($config["android"]["fcm"]["use_multi_curl"]) ? $config["android"]["fcm"]["use_multi_curl"] : null);
// }
}

/**
Expand Down
1 change: 1 addition & 0 deletions Device/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Types
{
const OS_ANDROID_C2DM = "rms_push_notifications.os.android.c2dm";
const OS_ANDROID_GCM = "rms_push_notifications.os.android.gcm";
const OS_ANDROID_FCM = "rms_push_notifications.os.android.fcm";
const OS_IOS = "rms_push_notifications.os.ios";
const OS_MAC = "rms_push_notifications.os.mac";
const OS_BLACKBERRY = "rms_push_notifications.os.blackberry";
Expand Down
84 changes: 83 additions & 1 deletion Message/AndroidMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class AndroidMessage implements MessageInterface
*/
protected $isGCM = false;

/**
* Whether this is a FCM message
*
* @var bool
*/
protected $isFCM = false;

/**
* A collection of device identifiers that the message
* is intended for. GCM use only
Expand All @@ -58,6 +65,13 @@ class AndroidMessage implements MessageInterface
*/
protected $gcmOptions = array();

/**
* Options for FCM messages
*
* @var array
*/
protected $fcmOptions = array();

/**
* Sets the string message
*
Expand Down Expand Up @@ -136,7 +150,15 @@ public function setDeviceIdentifier($identifier)
*/
public function getTargetOS()
{
return ($this->isGCM ? Types::OS_ANDROID_GCM : Types::OS_ANDROID_C2DM);
if($this->isGCM) {
return Types::OS_ANDROID_GCM;
}

if($this->isFCM) {
return Types::OS_ANDROID_FCM;
}

return Types::OS_ANDROID_C2DM;
}

/**
Expand Down Expand Up @@ -192,6 +214,27 @@ public function isGCM()
return $this->isGCM;
}

/**
* Set whether this is a FCM message
* (default false)
*
* @param $fcm
*/
public function setFCM($fcm)
{
$this->isFCM = !!$fcm;
}

/**
* Returns whether this is a FCM message
*
* @return mixed
*/
public function isFCM()
{
return $this->isFCM;
}

/**
* Returns an array of device identifiers
* Not used in C2DM
Expand All @@ -212,6 +255,26 @@ public function addGCMIdentifier($identifier)
$this->allIdentifiers[$identifier] = $identifier;
}

/**
* Returns an array of device identifiers
* Not used in C2DM
*
* @return mixed
*/
public function getFCMIdentifiers()
{
return array_values($this->allIdentifiers);
}

/**
* Adds a device identifier to the FCM list
* @param string $identifier
*/
public function addFCMIdentifier($identifier)
{
$this->allIdentifiers[$identifier] = $identifier;
}

/**
* Sets the GCM list
* @param array $allIdentifiers
Expand All @@ -238,4 +301,23 @@ public function getGCMOptions()
{
return $this->gcmOptions;
}

/**
* Sets FCM options
* @param array $options
*/
public function setFCMOptions($options)
{
$this->fcmOptions = $options;
}

/**
* Returns FCM options
*
* @return array
*/
public function getFCMOptions()
{
return $this->fcmOptions;
}
}
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# RMSPushNotificationsBundle ![](https://secure.travis-ci.org/richsage/RMSPushNotificationsBundle.png)

A bundle to allow sending of push notifications to mobile devices. Currently supports Android (C2DM, GCM), Blackberry and iOS devices.
A bundle to allow sending of push notifications to mobile devices. Currently supports Android (C2DM, GCM, FCM), Blackberry and iOS devices.

## Installation

Expand Down Expand Up @@ -44,6 +44,9 @@ only be available if you provide configuration respectively for them.
api_key: <string_android_gcm_api_key> # This is titled "Server Key" when creating it
use_multi_curl: <boolean_android_gcm_use_multi_curl> # default is true
dry_run: <bool_use_gcm_dry_run>
fcm:
api_key: <string_android_fcm_api_key> # This is titled "Server Key" when creating it
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not that familiar with FCM - is the comment still relevant here about "Server Key" being how the Firebase control panels name the API key?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still called "Server Key"

use_multi_curl: <boolean_android_fcm_use_multi_curl> # default is true
ios:
timeout: 60 # Seconds to wait for connection timeout, default is 60
sandbox: <bool_use_apns_sandbox>
Expand All @@ -62,7 +65,7 @@ only be available if you provide configuration respectively for them.
windowsphone:
timeout: 5 # Seconds to wait for connection timeout, default is 5

NOTE: If you are using Windows, you may need to set the Android GCM `use_multi_curl` flag to false for GCM messages to be sent correctly.
NOTE: If you are using Windows, you may need to set the Android GCM/FCM `use_multi_curl` flag to false for GCM/FCM messages to be sent correctly.

Timeout defaults are the defaults from prior to the introduction of this configuration value.

Expand All @@ -89,15 +92,15 @@ A little example of how to push your first message to an iOS device, we'll assum
The send method will detect the type of message so if you'll pass it an `AndroidMessage` it will automatically send it through the C2DM/GCM servers, and likewise for Mac and Blackberry.

## Android messages

Since both C2DM and GCM are still available, the `AndroidMessage` class has a small flag on it to toggle which service to send it to. Use as follows:
You have a choice of three services, C2DM, GCM and FCM. C2DM is defined as main option, but the `AndroidMessage` class has flags to toggle which service to send it to.

use RMS\PushNotificationsBundle\Message\AndroidMessage;

$message = new AndroidMessage();
$message->setGCM(true);

to send as a GCM message rather than C2DM.
You may choose `setGCM` or `setFCM` - this is optional.

$message->setFCM(true);

## iOS Feedback service

Expand Down
11 changes: 11 additions & 0 deletions Resources/config/android.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<parameters>
<parameter key="rms_push_notifications.android.c2dm.class">RMS\PushNotificationsBundle\Service\OS\AndroidNotification</parameter>
<parameter key="rms_push_notifications.android.gcm.class">RMS\PushNotificationsBundle\Service\OS\AndroidGCMNotification</parameter>
<parameter key="rms_push_notifications.android.fcm.class">RMS\PushNotificationsBundle\Service\OS\AndroidFCMNotification</parameter>
</parameters>

<services>
Expand All @@ -30,6 +31,16 @@
<tag name="rms_push_notifications.handler" osType="rms_push_notifications.os.android.gcm" />
</service>

<!-- Android (FCM) -->
<service id="rms_push_notifications.android.fcm" class="%rms_push_notifications.android.fcm.class%" public="false">
<argument>%rms_push_notifications.android.fcm.api_key%</argument>
<argument>%rms_push_notifications.android.fcm.use_multi_curl%</argument>
<argument>%rms_push_notifications.android.timeout%</argument>
<argument type="service" id="logger" />
<argument>null</argument>
<tag name="rms_push_notifications.handler" osType="rms_push_notifications.os.android.fcm" />
</service>

</services>

</container>
Loading