Skip to content

Add FCM HTTP V1 service #171

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open

Add FCM HTTP V1 service #171

wants to merge 13 commits into from

Conversation

dragermrb
Copy link

Add service to use FCM HTTP v1 API.

According to Google (https://firebase.google.com/docs/cloud-messaging/migrate-v1)

Apps using the deprecated FCM legacy APIs for HTTP and XMPP should migrate to the HTTP v1 API at the earliest opportunity. Sending messages (including upstream messages) with those APIs was deprecated on June 20, 2023, and will be removed in June 2024.

@shaffe-fr
Copy link

shaffe-fr commented Jun 12, 2024

Hi,
It looks like new message format is not yet implemented. Is it planned?

@matiss-zalans
Copy link

@dragermrb Thanks for the solution. We are using your fork. Just wanted to leave a comment here. I know that main project does not allow to use multiple fcm api keys with config structure, but that can be dealt with by using ->setApiKey() . Here I had a bit of a headache. When initializing FcmV1 with default configs or setting projectId the url property was updated. I was using ->setConfig() to set everything at once, that property was not updated. So just a feedback. You could add a check to update url property after setting the whole config. Was trying to find ->setUrl(), but class didn't have that, but PushService::class had that so I had to contruct the full url the same way as in your class. So another feedback. You could override ->setUrl() with project-id injection or something like that where users pass can project-id and code itself could use when reading from config or calling ->setProjectId().

@dragermrb
Copy link
Author

Hi @matiss-zalans

I have overwritten setConfig() method to updates the URL

@pushpak1300
Copy link

Any plans to merge this ?

@hossamadelelsayed
Copy link

is this PR will be merged soon ?

@pultho
Copy link
Collaborator

pultho commented Aug 13, 2024

Hi thanks for your contribution here. I will have a look at this next weeks and will prepare a release for that.

@AbdulRahmanIqbal
Copy link

how much time it will take to fix?

@adrianpaiva1
Copy link

@dragermrb I think you need to update your FCMV1Channel class to include the buildData method, your code currently doesn't work when using the FCM Channel in Laravel Notifications. The google API returns errors for the request keys that are no longer valid (sound, color, etc)

this is my custom channel class that works for me

image

@dragermrb
Copy link
Author

Hi @adrianpaiva1

Class Edujugon\PushNotification\Channels\FcmV1Channel inherits from Edujugon\PushNotification\Channels\GcmChannel, and it already implements buildData()

@adrianpaiva1
Copy link

hi @dragermrb yes but because the buildData method there is including the 'sound' and 'color' fields in the request the Google API is returning an error as they aren't supported anymore I believe.

I'll post the error response from the API later today.

image

@adrianpaiva1
Copy link

image

@dragermrb
Copy link
Author

@adrianpaiva1
Copy link

@dragermrb it looks like those keys are only valid in the 'android' key of the request though, currently it's including them in the base notification object so it's giving that error

https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?hl=en#Notification

@dragermrb
Copy link
Author

Hi @adrianpaiva1

The problem only occurs when using Laravel notifications. Since I don't use them I couldn't reproduce the problem.

I need to do some work to correctly generate the payload from an instance of Edujugon\PushNotification\Messages\PushMessage

I'll post the changes when I have a chance.

@adrianpaiva1
Copy link

Thanks @dragermrb one other thing I noticed was the status codes here. I think this should only be a 404 status code.

I was using the unregistedDeviceTokens to remove any invalid tokens for my users and including the 400 status code here means that if I send any invalid request parameters my tokens will be deactivated even if they are valid. From the docs it looks like 404 is correct to me for detecting deactivated device tokens.

image

@pultho pultho requested review from Edujugon and pultho May 18, 2025 17:16
@pultho pultho self-assigned this May 18, 2025
Copy link
Collaborator

@pultho pultho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance we can add a test using mocks for basic coverage?

'priority' => 'normal',
'dry_run' => false,
'projectId' => 'my-project-id',
'jsonFile' => __DIR__ . '/fcmCertificates/file.json',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use storage_path() instead of __DIR__ to follow Laravel standards.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe apn config needs that change too...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, yes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's up to you to modify it in the main branch for both drivers.

src/FcmV1.php Outdated

protected function getOauthToken()
{
return Cache::remember(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets add Cache::driver(config(...)) so people can configure a cache driver of their choice (depending on their setups). Variable for cache duration should be named more specific about what's actually be cached. Also duration should be moved into the config so people can adjust.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean Cache::store(config(...))?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed 01da9cc

src/FcmV1.php Outdated
{
$this->config = $this->initializeConfig('fcmv1');

$this->url = 'https://fcm.googleapis.com/v1/projects/' . $this->config['projectId'] . '/messages:send';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Base URL should be changed to constant.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed 0e9d756

@@ -25,8 +25,8 @@
"require": {
"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 || ^11.0",
"illuminate/notifications": "~5.8 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0",
"illuminate/support": "~5.8 || ^6.0 || ^7.0 || ^8.0 || ^9.0|^10.0|^11.0|^12.0",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single pipe are deprecated. Only for backwards compatibility

@dragermrb
Copy link
Author

Any chance we can add a test using mocks for basic coverage?

In my opinion, the entire test suite needs a major rewrite. I believe such changes are beyond the scope of this PR.

@pultho
Copy link
Collaborator

pultho commented May 19, 2025

In my opinion, the entire test suite needs a major rewrite. I believe such changes are beyond the scope of this PR.

It definitely needs that. But yes, would be out of scope. But would be good to have at least basic coverage on that one here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants