Skip to content
This repository was archived by the owner on Jan 10, 2020. It is now read-only.

Commit fd342e5

Browse files
committed
Adding in google GCM to support custom data and multiple gcm apps
1 parent df5cb7f commit fd342e5

File tree

8 files changed

+39
-17
lines changed

8 files changed

+39
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22
vendor
33
composer.phar
4+
atlassian-ide-plugin.xml

DependencyInjection/Configuration.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,18 @@ protected function addAndroid()
105105
scalarNode("source")->defaultValue("")->end()->
106106
end()->
107107
end()->
108-
arrayNode("gcm")->
109-
canBeUnset()->
110-
children()->
111-
scalarNode("api_key")->isRequired()->cannotBeEmpty()->end()->
112-
end()->
108+
arrayNode("gcm")
109+
->children()
110+
->arrayNode('api_keys')
111+
->isRequired()
112+
->requiresAtLeastOneElement()
113+
->useAttributeAsKey('name')
114+
->prototype('array')
115+
->children()
116+
->scalarNode('api_key')->isRequired()->end()
117+
->scalarNode('internal_app_id')->isRequired()->end()
118+
->end()->
119+
end()->
113120
end()->
114121
end()->
115122
end()->

Model/Message.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ abstract class Message implements MessageInterface
8686
/**
8787
* Collapse key for data
8888
*
89-
* @var int
89+
* @var string
9090
*/
9191
protected $collapseKey = self::DEFAULT_COLLAPSE_KEY;
9292

@@ -234,6 +234,7 @@ public function getMessageBody()
234234
"registration_id" => $this->device->getDeviceidentifier(),
235235
"collapse_key" => $this->collapseKey,
236236
"data.message" => $this->message,
237+
"data.content_available" => $this->contentAvailable ? 1 : 0,
237238
);
238239
if (!empty($this->customData)) {
239240
$data = array_merge($data, $this->customData);
@@ -388,7 +389,7 @@ public function getTargetOS()
388389
* Android-specific
389390
* Returns the collapse key
390391
*
391-
* @return int
392+
* @return string
392393
*/
393394
public function getCollapseKey()
394395
{

Model/MessageInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
interface MessageInterface
66
{
7-
const DEFAULT_COLLAPSE_KEY = 1;
7+
const DEFAULT_COLLAPSE_KEY = "1";
88

99
/**
1010
* @return mixed database ID for this device

Resources/config/android.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<!-- Android (GCM) -->
2222
<service id="dab_push_notifications.android.gcm" class="%dab_push_notifications.android.gcm.class%" public="false">
23-
<argument>%dab_push_notifications.android.gcm.api_key%</argument>
23+
<argument>%dab_push_notifications.android.gcm.api_keys%</argument>
2424
<tag name="dab_push_notifications.handler" osType="dab_push_notifications.os.android.gcm" />
2525
<argument type="service" id="dab_push_notifications.manager.device" />
2626
</service>

Resources/config/doctrine/Message.orm.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131

3232
<field name="contentAvailable" type="boolean" />
3333

34+
<field name="collapseKey" type="string" nullable="true" />
35+
36+
<field name="customData" type="array" nullable="true" />
37+
3438
<field name="createdAt" type="datetime" />
3539

3640
<field name="updatedAt" type="datetime" />

Service/OS/AndroidGCMNotification.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ class AndroidGCMNotification implements OSNotificationServiceInterface
1717
protected $apiURL = "https://android.googleapis.com/gcm/send";
1818

1919
/**
20-
* Google GCM API key
20+
* Array of used api keys
2121
*
2222
* @var string
2323
*/
24-
protected $apiKey;
24+
protected $apiKeys;
2525

2626
/**
2727
* @var \DABSquared\PushNotificationsBundle\Model\DeviceManager
@@ -47,9 +47,9 @@ class AndroidGCMNotification implements OSNotificationServiceInterface
4747
*
4848
* @param $apiKey
4949
*/
50-
public function __construct($apiKey, \DABSquared\PushNotificationsBundle\Model\DeviceManager $deviceManager)
50+
public function __construct($apiKeys, \DABSquared\PushNotificationsBundle\Model\DeviceManager $deviceManager)
5151
{
52-
$this->apiKey = $apiKey;
52+
$this->apiKeys = $apiKeys;
5353
$this->deviceManager = $deviceManager;
5454
}
5555

@@ -66,8 +66,18 @@ public function send(MessageInterface $message)
6666
throw new InvalidMessageTypeException(sprintf("Message type '%s' not supported by GCM", get_class($message)));
6767
}
6868

69+
$apiKey = null;
70+
71+
foreach($this->apiKeys as $anAPIKey) {
72+
if($message->getDevice()->getAppId() == $anAPIKey['internal_app_id']) {
73+
$apiKey = $anAPIKey;
74+
break;
75+
}
76+
}
77+
78+
6979
$headers = array(
70-
"Authorization: key=" . $this->apiKey,
80+
"Authorization: key=" . $apiKey,
7181
"Content-Type: application/json",
7282
);
7383
$data = array_merge(

Service/OS/iOSNotification.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
DABSquared\PushNotificationsBundle\Device\Types,
99
DABSquared\PushNotificationsBundle\Model\Device;
1010

11-
use Buzz\Browser;
1211
use DABSquared\PushNotificationsBundle\Message\MessageStatus;
1312
use DABSquared\PushNotificationsBundle\Model\DeviceManagerInterface;
1413
use DABSquared\PushNotificationsBundle\Model\MessageManagerInterface;
@@ -108,9 +107,9 @@ public function send(MessageInterface $message)
108107
* Send a bunch of notification messages
109108
*
110109
* @param array $messages
111-
* @throws \RuntimeException
110+
* @return bool|void
111+
* @throws \Symfony\Component\DependencyInjection\Exception\RuntimeException
112112
* @throws \DABSquared\PushNotificationsBundle\Exception\InvalidMessageTypeException
113-
* @return int
114113
*/
115114
public function sendMessages(array $messages)
116115
{

0 commit comments

Comments
 (0)