Skip to content

Commit 32d93ad

Browse files
committed
Add ability to send to both platforms in a single shot
1 parent 2fabc59 commit 32d93ad

File tree

3 files changed

+110
-8
lines changed

3 files changed

+110
-8
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,30 @@ class AccountApproved extends Notification
9494
- `badge(1)`: Accepts an integer value for the badge. (iOS Only)
9595
- `setOption($key, $value)`: Allows you to set any value in the message payload. For more information [check here for iOS](https://pusher.com/docs/push_notifications/ios/server), [or here for Android](https://pusher.com/docs/push_notifications/android/server).
9696

97+
### Sending to multiple platforms
98+
99+
You can send a single message to an iOS device and an Android device at the same time using the `withiOS()` and `withAndroid()` method:
100+
101+
```php
102+
public function toPushNotification($notifiable)
103+
{
104+
$message = "Your {$notifiable->service} account was approved!";
105+
106+
return PusherMessage::create()
107+
->iOS()
108+
->badge(1)
109+
->body($message)
110+
->withAndroid(
111+
PusherMessage::create()
112+
->title($message)
113+
->icon('icon')
114+
);
115+
}
116+
```
117+
118+
> - Notice that iOS is the default platform, which means you don't have to call `->iOS()`.
119+
> - When using `withAndroid()` or `withiOS()` you don't have to define the platform, it's done behind the scenes for you.
120+
97121
### Routing a message
98122

99123
By default the pusher "interest" messages will be sent to will be defined using the {notifiable}.{id} convention, for example `App.User.1`, however you can change this behaviour by including a `routeNotificationForPusherPushNotifications()` in the notifiable class method that returns the interest name.

src/Exceptions/CouldNotCreateMessage.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ class CouldNotCreateMessage extends Exception
88
{
99
/**
1010
* @param string $platform
11-
*
1211
* @return static
1312
*/
1413
public static function invalidPlatformGiven($platform)
1514
{
1615
return new static("Platform `{$platform}` is invalid. It should be either `iOS` or `Android`.");
1716
}
17+
18+
/**
19+
* @param $platform
20+
* @return static
21+
*/
22+
public static function platformConflict($platform)
23+
{
24+
return new static("You are trying to send an extra message to `{$platform}` while the original message is to `{$platform}`.");
25+
}
1826
}

src/PusherMessage.php

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ class PusherMessage
5656
*/
5757
protected $options = [];
5858

59+
/**
60+
* An extra message to the other platform.
61+
*
62+
* @var
63+
*/
64+
protected $extraMessage;
65+
5966
/**
6067
* @param string $body
6168
*
@@ -118,6 +125,47 @@ public function android()
118125
return $this;
119126
}
120127

128+
/**
129+
* Set an extra message to be sent to Android.
130+
*
131+
* @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
132+
* @return $this
133+
*/
134+
public function withAndroid(PusherMessage $message)
135+
{
136+
$this->withExtra($message->android());
137+
138+
return $this;
139+
}
140+
141+
/**
142+
* Set an extra message to be sent to iOS.
143+
*
144+
* @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
145+
* @return $this
146+
*/
147+
public function withiOS(PusherMessage $message)
148+
{
149+
$this->withExtra($message->iOS());
150+
151+
return $this;
152+
}
153+
154+
/**
155+
* Set an extra message to be sent to another platform.
156+
*
157+
* @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
158+
* @return void
159+
*/
160+
private function withExtra(PusherMessage $message)
161+
{
162+
if ($message->getPlatform() == $this->platform) {
163+
throw CouldNotCreateMessage::platformConflict($this->platform);
164+
}
165+
166+
$this->extraMessage = $message;
167+
}
168+
121169
/**
122170
* Set the message title.
123171
*
@@ -229,13 +277,11 @@ public function toiOS()
229277
],
230278
'sound' => $this->sound,
231279
'badge' => $this->badge,
232-
],
233-
],
280+
]
281+
]
234282
];
235283

236-
foreach ($this->options as $option => $value) {
237-
Arr::set($message, $option, $value);
238-
}
284+
$this->formatMessage($message);
239285

240286
return $message;
241287
}
@@ -258,10 +304,34 @@ public function toAndroid()
258304
],
259305
];
260306

307+
$this->formatMessage($message);
308+
309+
return $message;
310+
}
311+
312+
/**
313+
* Return the current platform.
314+
*
315+
* @return string
316+
*/
317+
public function getPlatform()
318+
{
319+
return $this->platform;
320+
}
321+
322+
/**
323+
* Format the final Payload.
324+
*
325+
* @param $message
326+
*/
327+
private function formatMessage(&$message)
328+
{
329+
if ($this->extraMessage) {
330+
$message = array_merge($message, $this->extraMessage->toArray());
331+
}
332+
261333
foreach ($this->options as $option => $value) {
262334
Arr::set($message, $option, $value);
263335
}
264-
265-
return $message;
266336
}
267337
}

0 commit comments

Comments
 (0)