From a2c2d5e61ef1a2cc993439e2a89dddddb9e9bd45 Mon Sep 17 00:00:00 2001 From: Rajendra Date: Sun, 19 Mar 2023 23:02:05 +0530 Subject: [PATCH 1/2] Added android_channel_id Added setChannelId method to update android_channel_id --- src/Notification.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Notification.php b/src/Notification.php index ad18565..ad0d9ee 100644 --- a/src/Notification.php +++ b/src/Notification.php @@ -14,6 +14,7 @@ class Notification extends Message private $clickAction; private $tag; private $content_available; + private $android_channel_id; public function __construct($title = '', $body = '') { @@ -71,7 +72,17 @@ public function setSound($sound) $this->sound = $sound; return $this; } - + /** + * android only, set android_channel_id as string, make sure to have channel id at client side with sound=/res/raw/noti.mp3 + * example : let channle = {id: 'panic', sound: 'noti'} + * @param string $channel + * @return $this + */ + public function setChannelId($channel) + { + $this->android_channel_id = $channel; + return $this; + } public function setTag($tag) { $this->tag = $tag; @@ -110,7 +121,10 @@ public function jsonSerialize() } if ($this->content_available) { $jsonData['content_available'] = $this->content_available; - } + } + if ($this->android_channel_id) { + $jsonData['android_channel_id'] = $this->android_channel_id; + } return $jsonData; } } From 965b9e3ffc780394224875b4312e9a15cac83f07 Mon Sep 17 00:00:00 2001 From: Rajendra Date: Sun, 19 Mar 2023 23:14:52 +0530 Subject: [PATCH 2/2] readme update for setChannelId Example added for setChannelId method --- README.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/README.md b/README.md index 93c6160..e6a9bea 100644 --- a/README.md +++ b/README.md @@ -132,3 +132,53 @@ $response = $client->removeTopicSubscription('_SOME_TOPIC_ID_', ['_FIRST_TOKEN_' var_dump($response->getStatusCode()); var_dump($response->getBody()->getContents()); ``` + +#Use custom notification sound +This need to have channel id at notification client side. +For example, a sample channel interface will be like: +``` +// Example from https://github.com/dpa99c/cordova-plugin-firebasex/issues/557 plugin + let channel: IChannelOptions = { + name: 'panic', + id: 'panic', + sound: 'panic', + vibration: true, + light: true, + lightColor: parseInt('FF0000FF', 16).toString(), + badge: true, + importance: 4 + }; + this.firebase.createChannel(channel); + ``` +For this to work, a sound file must be stored at /res/raw/panic.mp3 + +and server side should use same channel id. + +``` +use sngrl\PhpFirebaseCloudMessaging\Client; +use sngrl\PhpFirebaseCloudMessaging\Message; +use sngrl\PhpFirebaseCloudMessaging\Recipient\Topic; +use sngrl\PhpFirebaseCloudMessaging\Notification; + +$server_key = '_YOUR_SERVER_KEY_'; +$noti = new Notification(); +$noti->setTitle('Some title'); +$noti->setBody('Some Body'); +$noti->setChannelId('panic'); + +$client = new Client(); +$client->setApiKey($server_key); +$client->injectGuzzleHttpClient(new \GuzzleHttp\Client()); + +$message = new Message(); +$message->setPriority('high'); +$message->addRecipient(new Topic('_YOUR_TOPIC_')); +$message + ->setNotification($noti) + ->setData(['key' => 'value']) +; + +$response = $client->send($message); +var_dump($response->getStatusCode()); +var_dump($response->getBody()->getContents()); +```