Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FCM HTTP V1 service #171

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
}
],
"require": {
"php": "^7.1.3|^8.0",
"php": "^7.1.3 || ^8.0",
"guzzlehttp/guzzle": "^6.3 || ^7.0.1",
"illuminate/support": "~5.8 || ^6.0 || ^7.0 || ^8.0 || ^9.0|^10.0",
"illuminate/notifications": "~5.8 || ^6.0 || ^7.0 || ^8.0 || ^9.0|^10.0"
"illuminate/support": "~5.8 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0",
"illuminate/notifications": "~5.8 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0",
"google/apiclient": "^2.15"
},
"require-dev": {
"phpunit/phpunit": "^7.5 || ^8.0 || ^9.0"
Expand All @@ -46,4 +47,4 @@
}
}
}
}
}
57 changes: 57 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This is an easy to use package to send push notification.

* GCM
* FCM
* FCMV1
* APN

## Installation
Expand Down Expand Up @@ -59,6 +60,28 @@ $push->setConfig([
]);
```

The default configuration parameters for **FCMV1** are :

* ```priority => 'normal'```
* ```dry_run => false```
* ```projectId => 'my-project-id'```
* ```jsonFile => __DIR__ . '/fcmCertificates/file.json'```

You can dynamically update those values or adding new ones calling the method setConfig like so:
```php
$push->setConfig([
'priority' => 'high',
'projectId' => 'my-real-project-id',
'jsonFile' => 'path/to/credentials.json'
]);
```

To generate a credentials json file for your service account:

* In the Firebase console, open **Settings** > [Service Accounts](https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk).
* Click **Generate New Private Key**, then confirm by clicking **Generate Key**.
* Securely store the JSON file containing the key.


The default configuration parameters for **APN** are:

Expand Down Expand Up @@ -117,6 +140,11 @@ For FCM Service:
$push = new PushNotification('fcm');
```

For FCMV1 Service:
```php
$push = new PushNotification('fcmv1');
```

Now you may use any method that you need. Please see the API List.


Expand All @@ -139,6 +167,11 @@ Now you may use any method that you need. Please see the API List.

- [sendByTopic](https://github.com/edujugon/PushNotification#sendbytopic)

### Only for Fcmv1

- [setProjectId](https://github.com/edujugon/PushNotification#setprojectid)
- [setJsonFile](https://github.com/edujugon/PushNotification#setjsonfile)

> Go to [Usage samples](https://github.com/edujugon/PushNotification#usage-samples) directly.

#### setService
Expand Down Expand Up @@ -173,6 +206,30 @@ object setMessage(array $data)
object setApiKey($api_key)
```

#### setProjectId

> Only for fcmv1

`setProjectId` method sets the Project ID of your App as a string.

**Syntax**

```php
object setProjectId($project_id)
```

#### setJsonFile

> Only for fcmv1

`setJsonFile` method sets the path of credentials json file of your App.

**Syntax**

```php
object setJsonFile($api_key)
```

#### setDevicesToken

`setDevicesToken` method sets the devices' tokens, which you pass the token through parameter as array or string if it was only one.
Expand Down
71 changes: 71 additions & 0 deletions src/Channels/FcmV1Channel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Edujugon\PushNotification\Channels;

use Edujugon\PushNotification\Messages\PushMessage;

class FcmV1Channel extends GcmChannel
{
/**
* {@inheritdoc}
*/
protected function pushServiceName()
{
return 'fcmv1';
}

/**
* {@inheritdoc}
*/
protected function buildData(PushMessage $message)
{
$data = [];

if ($message->title != null || $message->body != null) {
$data = [
'notification' => [
'title' => $message->title,
'body' => $message->body,
],
];
}

if (
$message->icon ||
$message->color ||
$message->sound ||
$message->click_action ||
$message->badge
) {
$data['android'] = [
'notification' => []
];

if (! empty($message->icon)) {
$data['android']['notification']['icon'] = $message->icon;
}

if (! empty($message->color)) {
$data['android']['notification']['color'] = $message->color;
}

if (! empty($message->sound)) {
$data['android']['notification']['sound'] = $message->sound;
}

if (! empty($message->click_action)) {
$data['android']['notification']['click_action'] = $message->click_action;
}

if (! empty($message->badge)) {
$data['android']['notification']['notification_count'] = $message->badge;
}
}

if (! empty($message->extra)) {
$data['data'] = $message->extra;
}

return $data;
}
}
11 changes: 11 additions & 0 deletions src/Config/config.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @see https://github.com/Edujugon/PushNotification
*/
Expand All @@ -20,6 +21,16 @@
// See https://docs.guzzlephp.org/en/stable/request-options.html
'guzzle' => [],
],
'fcmv1' => [
'priority' => 'normal',
'dry_run' => false,
'projectId' => 'my-project-id',
'jsonFile' => __DIR__ . '/fcmCertificates/file.json',
// 'concurrentRequests' => 5, // Optional, default 10
// Optional: Default Guzzle request options for each FCM request
// See https://docs.guzzlephp.org/en/stable/request-options.html
'guzzle' => [],
],
'apn' => [
'certificate' => __DIR__ . '/iosCertificates/apns-dev-cert.pem',
'passPhrase' => 'secret', //Optional
Expand Down
Empty file.
Loading