Skip to content

Commit

Permalink
添加实例和说明
Browse files Browse the repository at this point in the history
  • Loading branch information
blueseashore committed Jul 17, 2019
1 parent 8f537ff commit 0c5caf3
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
5 changes: 5 additions & 0 deletions GetAccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public static function getInstance()
return self::$_instance;
}

/**
* 获取access_token
*
* @return bool|string
*/
public function get()
{
$ch = curl_init(self::BASE_LOGIN_URL . '/oauth2/v2/token');
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# Huawei Mobile Services
# 支持的功能
-- 获取access_token
-- 推送消息
13 changes: 13 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
define('HWPUSH_ROOT', dirname(__DIR__) . '/');

function hwpushAutoload(string $class)
{
$parts = explode('\\', $class);
$path = HWPUSH_ROOT . implode('/', $parts) . '.php';
if (file_exists($path)) {
include_once($path);
}
}

spl_autoload_register('hwpushAutoload');
57 changes: 57 additions & 0 deletions example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

use hwpush\GetAccessToken;
use hwpush\Sender;

include_once(dirname(__FILE__) . '/autoload.php');

//APP ID
$clientId = 'xxx';
//APP SECRET
$clientSecret = 'xxx';
//获取【AccessToken】实例
$access = GetAccessToken::getInstance();
//设置通信参数
$access->setClient($clientId, $clientSecret);
//获取access_token
$response = $access->get();
$response = json_decode($response, true);
/**
* Array(
* [access_token] => xxxx
* [expires_in] => 3600
* [token_type] => Bearer
* )
*/
print_r($response);


//获取 【Sender】实例
$sender = Sender::getInstance();
//设置通信参数
$sender->setClient($clientId, $clientSecret);
//设置通信令牌
$sender->setAccessToken($response['access_token']);
//定义设备Token列表
$deviceTokenList = [
'0867778034447519300001659700CN01'
];
//定义payload,下方使用的是:系统通知栏消息
$payload = ['hps' => [
'msg' => [
'type' => 3,
'body' => [
'content' => 'Hello,is me !',
'title' => 'Who are you?',
],
'action' => [
'type' => 1,
'param' => [
"intent" => "#Intent;compo=com.rvr/.Activity;S.W=U;end"
]
],
]
]];
$response = $sender->send($deviceTokenList, $payload);
//{"code":"80000000","msg":"Success","requestId":"154335160415079796001601"}
print_r($response);

0 comments on commit 0c5caf3

Please sign in to comment.