diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 552b207d..5518e21d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -12,7 +12,7 @@ jobs: with: fetch-depth: 2 - run: git checkout HEAD^2 - - name: Run Tests + - name: Run Tests env: MAILGUN_API_KEY: ${{ secrets.MAILGUN_API_KEY }} MAILGUN_DOMAIN: ${{ secrets.MAILGUN_DOMAIN }} @@ -40,7 +40,10 @@ jobs: VONAGE_API_SECRET: ${{ secrets.VONAGE_API_SECRET }} VONAGE_TO: ${{ secrets.VONAGE_TO }} VONAGE_FROM: ${{ secrets.VONAGE_FROM }} + PUSHER_INSTANCE_ID: ${{ secrets.PUSHER_INSTANCE_ID }} + PUSHER_SECRET_KEY: ${{ secrets.PUSHER_SECRET_KEY }} + PUSHER_TO: ${{ secrets.PUSHER_TO }} run: | docker compose up -d --build sleep 5 - docker compose exec tests vendor/bin/phpunit \ No newline at end of file + docker compose exec tests vendor/bin/phpunit diff --git a/README.md b/README.md index d745b630..b0c54b6f 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ $messaging->send($message); - [x] [FCM](https://firebase.google.com/docs/cloud-messaging) - [ ] [APNS](https://developer.apple.com/documentation/usernotifications) - [ ] [OneSignal](https://onesignal.com/) -- [ ] [Pusher](https://pusher.com/) +- [x] [Pusher](https://pusher.com/) - [ ] [WebPush](https://developer.mozilla.org/en-US/docs/Web/API/Push_API) - [ ] [UrbanAirship](https://www.urbanairship.com/) - [ ] [Pushwoosh](https://www.pushwoosh.com/) diff --git a/docker-compose.yml b/docker-compose.yml index 80d626d9..ab2cdd01 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,13 +29,16 @@ services: - VONAGE_API_SECRET - VONAGE_TO - VONAGE_FROM + - PUSHER_INSTANCE_ID + - PUSHER_SECRET_KEY + - PUSHER_TO build: context: . volumes: - ./src:/usr/local/src/src - ./tests:/usr/local/src/tests - ./phpunit.xml:/usr/local/src/phpunit.xml - + maildev: image: appwrite/mailcatcher:1.0.0 ports: @@ -44,4 +47,4 @@ services: request-catcher: image: appwrite/requestcatcher:1.0.0 ports: - - '10001:5000' \ No newline at end of file + - '10001:5000' diff --git a/src/Utopia/Messaging/Adapters/Push/Pusher.php b/src/Utopia/Messaging/Adapters/Push/Pusher.php new file mode 100644 index 00000000..3888aa08 --- /dev/null +++ b/src/Utopia/Messaging/Adapters/Push/Pusher.php @@ -0,0 +1,104 @@ + $val) { + if (is_array($val) && array_keys($val) !== range(0, count($val) - 1)) { + $output[$key] = $this->removeNullFields($val); + } elseif (! is_null($val)) { + $output[$key] = $val; + } + } + + return $output; + } + + /** + * {@inheritdoc} + * + * @throws \Exception + */ + protected function process(Push $message): string + { + return $this->request( + method: 'POST', + url: "https://{$this->instanceId}.pushnotifications.pusher.com/publish_api/v1/instances/{$this->instanceId}/publishes/users", + headers: [ + 'Content-Type: application/json', + "Authorization: Bearer {$this->secretKey}", + ], + body: \json_encode($this->removeNullFields([ + 'users' => $message->getTo(), + 'apns' => [ + 'aps' => [ + 'alert' => [ + 'title' => $message->getTitle(), + 'body' => $message->getBody(), + ], + 'badge' => $message->getBadge(), + 'sound' => $message->getSound(), + 'data' => $message->getData(), + ], + ], + 'fcm' => [ + 'notification' => [ + 'title' => $message->getTitle(), + 'body' => $message->getBody(), + 'click_action' => $message->getAction(), + 'icon' => $message->getIcon(), + 'color' => $message->getColor(), + 'sound' => $message->getSound(), + 'tag' => $message->getTag(), + ], + 'data' => $message->getData(), + ], + 'web' => [ + 'notification' => [ + 'title' => $message->getTitle(), + 'body' => $message->getBody(), + 'icon' => $message->getIcon(), + ], + 'data' => $message->getData(), + ], + ])) + ); + } +} diff --git a/tests/e2e/Push/PusherTest.php b/tests/e2e/Push/PusherTest.php new file mode 100644 index 00000000..cf6e2472 --- /dev/null +++ b/tests/e2e/Push/PusherTest.php @@ -0,0 +1,39 @@ + 'metadata', + ], + action: null, + sound: 'default', + icon: null, + color: null, + tag: null, + badge: '1' + ); + + $response = \json_decode($adapter->send($message)); + + $this->assertNotEmpty($response); + $this->assertIsString($response->publishId); + } +}