Skip to content

Instantly share code, notes, and snippets.

@kenchoong
Created March 9, 2018 19:36
Show Gist options
  • Save kenchoong/ec10b7554486298e1a3778e2611022cb to your computer and use it in GitHub Desktop.
Save kenchoong/ec10b7554486298e1a3778e2611022cb to your computer and use it in GitHub Desktop.
Fcm.php
<?php
/*
* Add FCM Class
*/
class Google_Service_Fcm extends Google_Service
{
/** Set URL for Google Cloud Platform services. */
const CLOUD_PLATFORM = "https://www.googleapis.com/auth/cloud-platform";
const FCM = "https://www.googleapis.com/auth/firebase.messaging";
public $operations;
/**
* Firebase Messaging Service.
*
* @param Google_Client $client
*/
public function __construct(Google_Client $client)
{
parent::__construct($client);
$this->rootUrl = 'https://fcm.googleapis.com/';
$this->servicePath = '';
$this->version = 'v1';
$this->serviceName = 'fcm';
$this->operations = new Google_Service_Fcm_Resource_Operations(
$this,
$this->serviceName,
'operations',
array(
'methods' => array(
'send' => array(
'path' => 'v1/projects/{project}/messages:send',
'httpMethod' => 'POST',
'parameters' => array(
'project' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
'message' => array(
'location' => 'query',
'type' => 'array',
'required' => false,
),
),
)
)
)
);
}
}
<?php
/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
class Google_Service_Fcm_Empty extends Google_Model
{
}
<?php
/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
class Google_Service_Fcm_Resource_Operations extends Google_Service_Resource
{
/**
*
* @param string $project The name of the project to be used.
* @param array $message Object that contains message details.
* @param array $optParams Optional parameters.
* @return Google_Service_Fcm_Empty
*/
public function send($project, $message, $optParams = array())
{
$params = array('project' => $project, 'postBody' => $message);
$params = array_merge($params, $optParams);
return $this->call('send', array($params), "Google_Service_Fcm_Empty");
}
}
<?php
require_once 'vendor/autoload.php';
// json
$returnValue = json_decode('{
"message": {
"android": {
"priority": "high",
"data": {
"title": "Hello",
"body": "Please confirm your availability android"
}
},
"name": "Planning validation",
"apns": {
"payload": {
"aps": {
"alert": {
"title": "Hello",
"body": "Please confirm your availability ios"
},
"sound": "default"
}
}
},
"topic": "your-topic-name"
}
}', true);
putenv('GOOGLE_APPLICATION_CREDENTIALS=client_cred.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/firebase.messaging");
$service = new Google_Service_Fcm($client);
$data = $service->operations->send("your-project-name", $returnValue);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment