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

Add FCM Support #161

Open
wants to merge 8 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
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ language: php

before_install:
# If PHP >= 5.6, download & install PHPunit 5.7 to avoid builds failures
- if php -r "exit( (int)! version_compare( '$TRAVIS_PHP_VERSION', '5.6', '>=' ) );"; then wget -O phpunit https://phar.phpunit.de/phpunit-5.7.phar && chmod +x phpunit && mkdir ~/bin && mv -v phpunit ~/bin; fi
- if php -r "exit( (int)! version_compare( '$TRAVIS_PHP_VERSION', '5.6', '>=' ) );"; then wget -O phpunit https://phar.phpunit.de/phpunit-5.7.phar && chmod +x phpunit && mkdir -p ~/bin && mv -v phpunit ~/bin; fi

php:
- 5.3
- 5.4
- 5.5
- 5.6
Expand Down
8 changes: 8 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ 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()->
booleanNode("dry_run")->defaultFalse()->end()->
end()->
end()->
end()->
end()->
end()
Expand Down
79 changes: 61 additions & 18 deletions DependencyInjection/RMSPushNotificationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ class RMSPushNotificationsExtension extends Extension
/**
* Loads any resources/services we need
*
* @param array $configs
* @param array $configs
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
*
* @return void
*/
public function load(array $configs, ContainerBuilder $container)
Expand All @@ -35,7 +36,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('services.xml');

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$config = $this->processConfiguration($configuration, $configs);

$this->setInitialParams();
if (isset($config["android"])) {
Expand Down Expand Up @@ -83,12 +84,12 @@ protected function setAndroidConfig(array $config)
// C2DM
$username = $config["android"]["username"];
$password = $config["android"]["password"];
$source = $config["android"]["source"];
$timeout = $config["android"]["timeout"];
$source = $config["android"]["source"];
$timeout = $config["android"]["timeout"];
if (isset($config["android"]["c2dm"])) {
$username = $config["android"]["c2dm"]["username"];
$password = $config["android"]["c2dm"]["password"];
$source = $config["android"]["c2dm"]["source"];
$source = $config["android"]["c2dm"]["source"];
}
$this->container->setParameter("rms_push_notifications.android.timeout", $timeout);
$this->container->setParameter("rms_push_notifications.android.c2dm.username", $username);
Expand All @@ -98,9 +99,35 @@ 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"]);
$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"]
);
}

// 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",
$config["android"]["fcm"]["api_key"]
);
$this->container->setParameter(
"rms_push_notifications.android.fcm.use_multi_curl",
$config["android"]["fcm"]["use_multi_curl"]
);
$this->container->setParameter(
'rms_push_notifications.android.fcm.dry_run',
$config["android"]["fcm"]["dry_run"]
);
}
}

Expand All @@ -127,8 +154,9 @@ protected function setMacConfig(array $config)
/**
* Sets Apple config into container
*
* @param array $config
* @param array $config
* @param $os
*
* @throws \RuntimeException
* @throws \LogicException
*/
Expand All @@ -146,7 +174,7 @@ protected function setAppleConfig(array $config, $os)
if (realpath($config[$os]["pem"])) {
// Absolute path
$pemFile = $config[$os]["pem"];
} elseif (realpath($this->kernelRootDir.DIRECTORY_SEPARATOR.$config[$os]["pem"]) ) {
} elseif (realpath($this->kernelRootDir.DIRECTORY_SEPARATOR.$config[$os]["pem"])) {
// Relative path
$pemFile = $this->kernelRootDir.DIRECTORY_SEPARATOR.$config[$os]["pem"];
} else {
Expand All @@ -158,19 +186,28 @@ protected function setAppleConfig(array $config, $os)
if ($config[$os]['json_unescaped_unicode']) {
// Not support JSON_UNESCAPED_UNICODE option
if (!version_compare(PHP_VERSION, '5.4.0', '>=')) {
throw new \LogicException(sprintf(
'Can\'t use JSON_UNESCAPED_UNICODE option. This option can use only PHP Version >= 5.4.0. Your version: %s',
PHP_VERSION
));
throw new \LogicException(
sprintf(
'Can\'t use JSON_UNESCAPED_UNICODE option. ' .
'This option can use only PHP Version >= 5.4.0. Your version: %s',
PHP_VERSION
)
);
}
}

$this->container->setParameter(sprintf('rms_push_notifications.%s.enabled', $os), true);
$this->container->setParameter(sprintf('rms_push_notifications.%s.timeout', $os), $config[$os]["timeout"]);
$this->container->setParameter(sprintf('rms_push_notifications.%s.sandbox', $os), $config[$os]["sandbox"]);
$this->container->setParameter(sprintf('rms_push_notifications.%s.pem', $os), $pemFile);
$this->container->setParameter(sprintf('rms_push_notifications.%s.passphrase', $os), $config[$os]["passphrase"]);
$this->container->setParameter(sprintf('rms_push_notifications.%s.json_unescaped_unicode', $os), (bool) $config[$os]['json_unescaped_unicode']);
$this->container->setParameter(
sprintf('rms_push_notifications.%s.passphrase', $os),
$config[$os]["passphrase"]
);
$this->container->setParameter(
sprintf('rms_push_notifications.%s.json_unescaped_unicode', $os),
(bool)$config[$os]['json_unescaped_unicode']
);
}

/**
Expand All @@ -182,14 +219,20 @@ protected function setBlackberryConfig(array $config)
{
$this->container->setParameter("rms_push_notifications.blackberry.enabled", true);
$this->container->setParameter("rms_push_notifications.blackberry.timeout", $config["blackberry"]["timeout"]);
$this->container->setParameter("rms_push_notifications.blackberry.evaluation", $config["blackberry"]["evaluation"]);
$this->container->setParameter(
"rms_push_notifications.blackberry.evaluation",
$config["blackberry"]["evaluation"]
);
$this->container->setParameter("rms_push_notifications.blackberry.app_id", $config["blackberry"]["app_id"]);
$this->container->setParameter("rms_push_notifications.blackberry.password", $config["blackberry"]["password"]);
}

protected function setWindowsphoneConfig(array $config)
{
$this->container->setParameter("rms_push_notifications.windowsphone.enabled", true);
$this->container->setParameter("rms_push_notifications.windowsphone.timeout", $config["windowsphone"]["timeout"]);
$this->container->setParameter(
"rms_push_notifications.windowsphone.timeout",
$config["windowsphone"]["timeout"]
);
}
}
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
82 changes: 76 additions & 6 deletions 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 @@ -52,11 +59,11 @@ class AndroidMessage implements MessageInterface
protected $allIdentifiers = array();

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

/**
* Sets the string message

Choose a reason for hiding this comment

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

Hello,
In the FCM case setMessage method will not work :) it should be replaced by body attribute. We can add also "title" attribute.

public function setMessage($message) { if ($this->isFCM){ $this->body = $message; } else { $this->message = $message; } }

Copy link
Author

Choose a reason for hiding this comment

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

Ah! Good catch. Thanks. I'll get that fixed shortly.

Expand Down Expand Up @@ -136,7 +143,7 @@ public function setDeviceIdentifier($identifier)
*/
public function getTargetOS()
{
return ($this->isGCM ? Types::OS_ANDROID_GCM : Types::OS_ANDROID_C2DM);
return ($this->isFCM ? Types::OS_ANDROID_FCM : ($this->isGCM ? Types::OS_ANDROID_GCM : Types::OS_ANDROID_C2DM));
}

/**
Expand Down Expand Up @@ -216,26 +223,89 @@ public function addGCMIdentifier($identifier)
* Sets the GCM list
* @param array $allIdentifiers
*/
public function setAllIdentifiers($allIdentifiers) {
public function setAllIdentifiers($allIdentifiers)
{
$this->allIdentifiers = array_combine($allIdentifiers, $allIdentifiers);
}

/**
* Sets GCM options
* @param array $options
* @deprecated GCM is being deprecated for FCM, both services share the same options
*/
public function setGCMOptions($options)
{
$this->gcmOptions = $options;
$this->setFCMOptions($options);
}

/**
* Returns GCM options
* @deprecated GCM is being deprecated for FCM, both services share the same options
*
* @return array
*/
public function getGCMOptions()
{
return $this->gcmOptions;
return $this->getFCMOptions();
}

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

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

/**
* 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 GCM list
* @param string $identifier
*/
public function addFCMIdentifier($identifier)
{
$this->allIdentifiers[$identifier] = $identifier;
}

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

/**
* Returns FCM options
*
* @return array
*/
public function getFCMOptions()
{
return $this->fcmOptions;
}
}
14 changes: 13 additions & 1 deletion 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,10 @@ 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
use_multi_curl: <boolean_android_fcm_use_multi_curl> # default is true
dry_run: <bool_use_fcm_dry_run>
ios:
timeout: 60 # Seconds to wait for connection timeout, default is 60
sandbox: <bool_use_apns_sandbox>
Expand Down Expand Up @@ -99,6 +103,14 @@ Since both C2DM and GCM are still available, the `AndroidMessage` class has a sm

to send as a GCM message rather than C2DM.

Since the deprecation of GCM for FCM, the follow is now recommended:

use RMS\PushNotificationsBundle\Message\AndroidMessage;

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


## iOS Feedback service

The Apple Push Notification service also exposes a Feedback service where you can get information about failed push notifications - see [here](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW3) for further details.
Expand Down
12 changes: 12 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,17 @@
<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>
<argument>%rms_push_notifications.android.fcm.dry_run%</argument>
<tag name="rms_push_notifications.handler" osType="rms_push_notifications.os.android.fcm" />
</service>

</services>

</container>
Loading