Skip to content

Commit 52e8298

Browse files
committed
FCM Adapter
1 parent 3542d8b commit 52e8298

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
namespace Sly\NotificationPusher\Adapter;
3+
4+
use Sly\NotificationPusher\Adapter\Fcm\Client;
5+
use Sly\NotificationPusher\Adapter\Fcm\Message;
6+
use Sly\NotificationPusher\Model\BaseOptionedModel;
7+
use Sly\NotificationPusher\Model\MessageInterface;
8+
use ZendService\Google\Gcm\Client as BaseClient;
9+
10+
class Fcm extends Gcm
11+
{
12+
/**
13+
* Get opened client.
14+
*
15+
* @param \ZendService\Google\Gcm\Client $client
16+
* @return Sly\NotificationPusher\Adapter\Fcm\Client
17+
*/
18+
public function getOpenedClient()
19+
{
20+
return parent::getOpenedClient(new Client);
21+
}
22+
/**
23+
* Get service message from origin.
24+
*
25+
* @param array $tokens
26+
* @param \Sly\NotificationPusher\Model\BaseOptionedModel $message
27+
* @return Sly\NotificationPusher\Adapter\Fcm\Message
28+
*/
29+
public function getServiceMessageFromOrigin(array $tokens, BaseOptionedModel $message)
30+
{
31+
$data = $message->getOptions();
32+
$data['message'] = $message->getText();
33+
$serviceMessage = new Message();
34+
$serviceMessage->setRegistrationIds($tokens)
35+
->setData($data)
36+
->setCollapseKey($this->getParameter('collapseKey'))
37+
->setRestrictedPackageName($this->getParameter('restrictedPackageName'))
38+
->setDelayWhileIdle($this->getParameter('delayWhileIdle', false))
39+
->setTimeToLive($this->getParameter('ttl', 600))
40+
->setDryRun($this->getParameter('dryRun', false))
41+
->setPriority($this->getParameter('priority'));
42+
return $serviceMessage;
43+
}
44+
/**
45+
* Get the feedback.
46+
*
47+
* @return array
48+
*/
49+
public function getFeedback()
50+
{
51+
return $this->response->getResults();
52+
}
53+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Sly\NotificationPusher\Adapter\Fcm;
4+
5+
use ZendService\Google\Gcm\Client as BaseClient;
6+
class Client extends BaseClient
7+
{
8+
/**
9+
* @const string Server URI
10+
*/
11+
const SERVER_URI = 'https://fcm.googleapis.com/fcm/send';
12+
}
13+
?>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Sly\NotificationPusher\Adapter\Fcm;
4+
5+
use Zend\Json\Json;
6+
use ZendService\Google\Gcm\Message as BaseMessage;
7+
8+
class Message extends BaseMessage
9+
{
10+
/**
11+
* @var string
12+
*/
13+
const PRIORITY_HIGH = 'high';
14+
/**
15+
* @var string
16+
*/
17+
const PRIORITY_NORMAL = 'normal';
18+
/**
19+
* @var string
20+
*/
21+
protected $priority;
22+
/**
23+
* Set the priority.
24+
*
25+
* @param string $priority
26+
* @return $this
27+
*/
28+
public function setPriority($priority)
29+
{
30+
$priority = in_array($priority, [static::PRIORITY_HIGH, static::PRIORITY_NORMAL]) ? $priority : static::PRIORITY_NORMAL;
31+
$this->priority = $priority;
32+
return $this;
33+
}
34+
/**
35+
* Get priority.
36+
*
37+
* @return string
38+
*/
39+
public function getPriority()
40+
{
41+
return $this->priority;
42+
}
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function toJson()
47+
{
48+
$json = parent::toJson();
49+
$data = Json::decode($json, Json::TYPE_ARRAY);
50+
if ($this->priority) {
51+
$data['priority'] = $this->priority;
52+
}
53+
return Json::encode($data);
54+
}
55+
}

0 commit comments

Comments
 (0)