Skip to content

Commit d2b9e98

Browse files
committed
Add more properties to the Notification.
Use sngrl\PhpFirebaseCloudMessaging\Notification as Notification Entity.
1 parent 4ab83d7 commit d2b9e98

File tree

5 files changed

+106
-132
lines changed

5 files changed

+106
-132
lines changed

Entity/DeviceNotification.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
namespace RedjanYm\FCMBundle\Entity;
44

5+
use sngrl\PhpFirebaseCloudMessaging\Notification;
6+
57
class DeviceNotification extends Notification implements \RedjanYm\FCMBundle\Model\DeviceNotification
68
{
9+
10+
use NotificationData;
11+
12+
/**
13+
* @var string $deviceToken
14+
*/
715
private $deviceToken;
816

917
/**
@@ -31,4 +39,4 @@ public function getDeviceToken()
3139
throw new \InvalidArgumentException('The Mobile Notification must have a Device Token!');
3240
}
3341
}
34-
}
42+
}

Entity/Notification.php

Lines changed: 0 additions & 118 deletions
This file was deleted.

Entity/NotificationData.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace RedjanYm\FCMBundle\Entity;
4+
5+
6+
trait NotificationData {
7+
8+
/**
9+
* @var array
10+
*/
11+
private $data = array();
12+
13+
/**
14+
* @var string
15+
*/
16+
private $priority;
17+
18+
/**
19+
* @return array
20+
*/
21+
public function getData()
22+
{
23+
return $this->data;
24+
}
25+
26+
/**
27+
* @param array $data
28+
* @return $this
29+
*/
30+
public function setData(array $data)
31+
{
32+
$this->data = $data;
33+
34+
return $this;
35+
}
36+
37+
/**
38+
* @param $key
39+
* @param $value
40+
* @return $this
41+
*/
42+
public function addData($key, $value)
43+
{
44+
if((is_string($key) || is_numeric($key)) && strlen($key) > 0) {
45+
$this->data[$key] = $value;
46+
} else {
47+
throw new \InvalidArgumentException('The value of key must not be empty!');
48+
}
49+
50+
return $this;
51+
}
52+
53+
/**
54+
* @param $key
55+
* @return $this
56+
*/
57+
public function removeData($key)
58+
{
59+
if(isset($this->data[$key])){
60+
unset($this->data[$key]);
61+
}
62+
63+
return $this;
64+
}
65+
66+
/**
67+
* @return string
68+
*/
69+
public function getPriority()
70+
{
71+
return $this->priority;
72+
}
73+
74+
/**
75+
* @param string $priority
76+
* @return $this;
77+
*/
78+
public function setPriority($priority)
79+
{
80+
$this->priority = $priority;
81+
82+
return $this;
83+
}
84+
}

Entity/TopicNotification.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22

33
namespace RedjanYm\FCMBundle\Entity;
44

5+
use sngrl\PhpFirebaseCloudMessaging\Notification;
56

67
class TopicNotification extends Notification implements \RedjanYm\FCMBundle\Model\TopicNotification
78
{
9+
10+
use NotificationData;
11+
12+
/**
13+
* @var string $topic
14+
*/
815
private $topic;
916

1017
/**

FCMClient.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function subscribeDevicesToTopic($topicId = null, $deviceTokens = array()
9292
if(!$topicId || empty($deviceTokens)){
9393
throw new \InvalidArgumentException("Please check arguments!");
9494
}
95-
95+
9696
return $this->client->addTopicSubscription($topicId, $deviceTokens);
9797
}
9898

@@ -123,8 +123,10 @@ public function sendNotification($notification)
123123
throw new \InvalidArgumentException('Notification must be of type DeviceNotification or TopicNotification');
124124
}
125125

126-
$message = new Message();
127-
$message->setPriority($notification->getPriority());
126+
$message = (new Message())
127+
->setNotification($notification)
128+
->setData($notification->getData())
129+
->setPriority($notification->getPriority());
128130

129131
// Check for the type of Notification
130132
if($notification instanceof DeviceNotification){
@@ -133,15 +135,6 @@ public function sendNotification($notification)
133135
$message->addRecipient(new Topic($notification->getTopic()));
134136
}
135137

136-
$message
137-
->setNotification(
138-
new Notification(
139-
$notification->getTitle(),
140-
$notification->getBody()
141-
)
142-
)
143-
->setData($notification->getData());
144-
145138
return $this->client->send($message);
146139
}
147-
}
140+
}

0 commit comments

Comments
 (0)