php 接极光推送 普通消息和标题内容消息推送实现方法

一、如下两种消息样式推送方法,这里介绍第一种标题+内容样式的消息推送。

1.首先,下载极光PHP的SDK,引入到项目,基础参考 https://docs.jiguang.cn/jpush/server/sdk/php_sdk/   这里不详细介绍了

2.在项目的config文件的配置文件中加入极光配置

3.下面是封装的方法

          自定义type=1推送出来的消息是如上图“发现精彩”的消息样式,else里面推送出来的是简单的消息,如上图招商银行的消息样式。

/**
     * $content 推送内容,
     * $title 推送的标题
     * $type 推送的类型:给所有人推送还是指定id去推送
     * $ids 推送给哪些人,我存的别名是用户id,所以这里只有组合出要推送的人的用户id数组即可
     * $news_info  推送的新闻详情。移动端根据里面新闻id 去打开app中自动打开这个新闻id对应的新闻详情
     * 
     */
function appJpush($content,$type,$ids=[],$title='',$news_info){
        $config = config('jpush');
        $client = new JPush($config['AppKey'], $config['MasterSecret']);
        // 给所有人
        if($type==1){
            $response = $client->push()
                ->setPlatform(array('ios','android'))// 推送的接收平台
                ->addAllAudience()// 给所有人
//                ->setNotificationAlert('Hello, JPush')
                ->iosNotification(
                    array(
                        "title" => $title,
//                        "subtitle" => "JPush Subtitle" ,
                        "body" => $content
                    ),
                    array(
                        'sound' => 'sound',
                        'badge' => 1,
                        'extras' => [
                            'news_info'=>$news_info
                        ]
                    )
                )
                ->androidNotification($title, [
                    'title' => $content,
                    'extras' => [
                        'news_info'=>$news_info
                    ]
                ])
                ->options(array(
                    'apns_production' => false, // 测试环境
                ))
                ->send();
        }else{
            try{
                $response = $client->push()
                    ->setPlatform(array('ios','android'))// 推送的接收平台
                    ->addAlias($ids)// 别名推送
                    ->setNotificationAlert($content)
                    ->options(array(
                        'apns_production' => false, // 测试环境
                    ))
                    ->send();
            } catch(\Exception $e){
                trace($e,'error');
            }
        }

猜你喜欢

转载自blog.csdn.net/lw545034502/article/details/84328020