I can send notification using Firebase Messaging using the CURL request below. I am using currently using the OAuth 2.0 Playground to get an access token. I need to implement a PHP script to do this. How can generate the access token programmatically in PHP?
curl -X POST -k -H 'Authorization: Bearer access_token_goes_here' -H 'Content-Type: application/json' -i 'https://fcm.googleapis.com/v1/projects/projectId/messages:send' --data '{
"message":{
"topic" : "newTopic",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message"
}
}
}
2
Answers
You can check these similar questions that are already answered:
I found a lot of solutions, but all of them needed a lot of libraries and dependencies.
I build my own solution, without extra dependencies. This is the api for getting a OAuth2 token: https://developers.google.com/identity/protocols/oauth2/service-account#httprest
The first step is to create a JWT (Json Web Token). With that JWT, a bearer token can be requested.
The
$response
contains the bearer token. You should store this token for other requests, and request a new bearer token when it almost expires. The maximum lifetime of this bearer token is 1 hour.