极光推送PHP服务器端+ThinkPHP3.5

下载安装配置

  1. 下载源码
    极光推送服务器端php源码下载

  2. 解压安装
    解压jpush-api-php-client文件改名为jpush放入ThinkPHP->Library->Vendor中

  3. 上极光官网获取AppKey及密钥
    极光官网

  4. 配置

    在项目Application->Common->Conf->config.php中配置密钥

 //'配置项'=>'配置值'
    'JPUSH' => array(
        'APP_KEY' =>'这里输入AppKey',
        'MASTER_SECRET' =>'这里输入Master Secret'
    ),

在需要调用jpush的Controller中引入autoload

Vendor('jpush.autoload');
use JPush\Client as JPushClient;

方法

/**
 * @description   推送
 * @params 
   alias 别名(数组) 
   alert 推送内容 
   page 点击推送跳转到的页面参数 
 * @return
*/
 public function pushByAlias($alias,$alert,$page,$type){
        $jpush = new JPushClient(C('JPUSH.APP_KEY'), C('JPUSH.MASTER_SECRET'));
        try {
            $response = $jpush->push()
                ->setPlatform(array('ios', 'android'));
                foreach ($alias as $item) {
                    $response->addAlias($item);
                }
            $response
                ->androidNotification($alert, array(
                    'extras' => array(
                        'page' => $page
                    ),
                ))
                ->send();
        } catch (\JPush\Exceptions\APIConnectionException $e) {
            print $e;
        } catch (\JPush\Exceptions\APIRequestException $e) {
            print $e;
        }
    }

另外

有多台设备登录同一账号解决方案
1. 数据库添加设备码和极光推送注册码字段
2. 登录时将本机设备码对应的其他用户设备码清空(数据库)
3. 为本用户保存本机设备码和注册码(数据库)
4. 如果用户换手机登录则推送给用户对应之前的手机(推送)
5. 删除同别名的其他设备(极光)

//删除同别名的其他设备
public function deleteAliasByRID($alias,$rID){
        $jpush = new JPushClient(C('JPUSH.APP_KEY'), C('JPUSH.MASTER_SECRET'));
        $device = $jpush->device();
        //删除别名下全部设备
        $UUID = $device->getAliasDevices($alias)['body']['registration_ids'];
        for($i = 0;$i<count($UUID);$i++){
            $device->updateAlias($UUID[$i], '');
        }
        //为本机增加别名
        $device->updateAlias($rID, $alias);
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于ThinkPHP框架整合极光推送的示例代码: 1. 安装JPush SDK 使用composer安装JPush SDK: ``` composer require jpush/jpush ``` 2. 配置JPush 在ThinkPHP框架中,可以将JPush的配置信息写入到config目录下的jpush.php文件中,示例代码如下: ```php return [ 'app_key' => 'YOUR_APP_KEY', 'master_secret' => 'YOUR_MASTER_SECRET', ]; ``` 3. 创建JPushService类 在app/service目录下创建JPushService类,该类用于封装极光推送的相关操作。 ```php <?php namespace app\service; use JPush\Client as JPush; class JPushService { protected $jpush; public function __construct() { $config = config('jpush'); $this->jpush = new JPush($config['app_key'], $config['master_secret']); } // 发送通知 public function sendNotification($title, $content, $extras = [], $audience = 'all') { $notification = [ 'title' => $title, 'alert' => $content, ]; $message = [ 'title' => $title, 'msg_content' => $content, 'extras' => $extras, ]; $options = [ 'apns_production' => false, ]; $response = $this->jpush->push() ->setPlatform(['ios', 'android']) ->setAudience($audience) ->setNotification($notification) ->setMessage($message) ->setOptions($options) ->send(); return $response; } } ``` 4. 使用JPushService类发送推送 在控制器中使用JPushService类发送推送,示例代码如下: ```php <?php namespace app\controller; use app\service\JPushService; class PushController { public function send() { $title = '测试推送'; $content = '这是一条测试推送'; $extras = ['key1' => 'value1', 'key2' => 'value2']; $jpushService = new JPushService(); $response = $jpushService->sendNotification($title, $content, $extras); if ($response['http_code'] === 200) { return json(['code' => 0, 'message' => '推送成功']); } else { return json(['code' => -1, 'message' => '推送失败']); } } } ``` 以上就是基于ThinkPHP框架整合极光推送的示例代码。需要注意的是,示例代码中使用了JPush的免费版服务,如果需要使用更高级别的服务,需要进行相应的付费。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值