1. 程式人生 > >php 接極光推送 普通訊息和標題內容訊息推送實現方法

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');
            }
        }