在 Laravel 中使用 極光推送

一個人的江湖發表於2019-07-13

最近在專案中需要用到推送,抽出時間看了看極光推送,記錄一下
Github地址:https://github.com/jpush/jpush-api-php-cli...
極光推送官方文件地址:https://docs.jiguang.cn/jpush/server/push/...

  • PHP 7.1.29
  • Laravel 5.6

    composer.json 檔案中的 require 下新增 "jpush/jpush": "^3.6"

    "require": {
        "jpush/jpush": "^3.6",
    },

    執行命令:composer install

    config 目錄下新建檔案 Jpush.php 並進行如下配置:

    return [
        'android_app_key' => env('JPUSH_APP_KEY'),
        'android_master_secret' => env('JPUSH_APP_MASTER_SECRET'),
    
        'ios_app_key' => env('JPUSH_IOS_KEY'),
        'ios_master_secret' => env('JPUSH_IOS_MASTER_SECRET'),
    
        // 環境 true-生產 false-開發
        'environment' => env('JPUSH_APNS_PRODUCTION', false),
    ];

    .env 檔案進行如下配置:

    JPUSH_APP_KEY=極光推送的app_key
    JPUSH_APP_MASTER_SECRET=極光推送的master_secret
    JPUSH_IOS_KEY=
    JPUSH_IOS_MASTER_SECRET=
    JPUSH_APNS_PRODUCTION=true

    Support 目錄下新建目錄 Jpush,然後在 Jpush 目錄下新建檔案 Jpush.php 內容如下:

<?php

namespace App\Support\Jpush;

use JPush\Client;

class Jpush
{
    private $androidAppKey;
    private $androidMasterSecret;

    public function __construct()
    {
        $this->androidAppKey = config('Jpush.android_app_key');
        $this->androidMasterSecret = config('Jpush.android_master_secret');
    }

    /**
     * 初始化 JPushClient
     * @return Client
     */
    public function newJpushClient()
    {
        $client = new Client($this->androidAppKey, $this->androidMasterSecret);
        return $client;
    }

    /**
     * android 通過 註冊ID 給單個裝置或多個裝置推送訊息(已測可用)
     * @param $platform
     * @param $registrationId
     * @param $title
     * @param $content
     * @param $builder
     * @param $extras
     * @return array
     */
    public function androidPushByRegistrationId($platform, $registrationId, $title, $content, $builder, $extras)
    {
        $response = $this->newJpushClient()
            ->push()
            ->setPlatform($platform)
            ->addRegistrationId($registrationId)
            ->androidNotification($content, [
                "title" => $title,
                "builder_id" => $builder,
                "extras" => $extras
            ])->send();
        return $response;
    }

    /**
     * ios 通過 註冊ID 給單個裝置或多個裝置推送訊息(未測)
     * @param $platform
     * @param $registrationId
     * @param $title
     * @param $content
     * @param $builder
     * @param $extras
     * @return array
     */
    public function iosPushByRegistrationId($platform, $registrationId, $title, $content, $builder, $extras)
    {
        $response = $this->newJpushClient()
            ->push()
            ->setPlatform($platform)
            ->addRegistrationId($registrationId)
            ->androidNotification($content, [
                "title" => $title,
                "builder_id" => $builder,
                "extras" => $extras
            ])->send();
        return $response;
    }

    /**
     * android 通過 別名 給單個裝置或多個裝置推送訊息(已測可用)
     * @param $platform
     * @param $alias
     * @param $title
     * @param $content
     * @param $builder
     * @param $extras
     * @return array
     */
    public function androidPushByAlias($platform, $alias, $title, $content, $builder, $extras)
    {
        $response = $this->newJpushClient()
            ->push()
            ->setPlatform($platform)
            ->addAlias($alias)
            ->androidNotification($content, [
                "title" => $title,
                "builder_id" => $builder,
                "extras" => $extras
            ])->send();
        return $response;
    }

    /**
     * ios 通過 別名 給單個裝置或多個裝置推送訊息(未測)
     * @param $platform
     * @param $alias
     * @param $title
     * @param $content
     * @param $builder
     * @param $extras
     * @return array
     */
    public function iosPushByAlias($platform, $alias, $title, $content, $builder, $extras)
    {
        $response = $this->newJpushClient()
            ->push()
            ->setPlatform($platform)
            ->addAlias($alias)
            ->androidNotification($content, [
                "title" => $title,
                "builder_id" => $builder,
                "extras" => $extras
            ])->send();
        return $response;
    }

    /**
     * 推送所有 android裝置 或者 ios裝置(android 已測可用)
     * @param $platform
     * @param $title
     * @param $content
     * @param $builder
     * @param $extras
     * @return array
     */
    public function AndroidOrIosPushAll($platform, $title, $content, $builder, $extras)
    {
        $response = $this->newJpushClient()
            ->push()
            ->setPlatform($platform)
            ->addAllAudience()
            ->androidNotification($content, [
                "title" => $title,
                "builder_id" => $builder,
                "extras" => $extras
            ])->send();
        return $response;
    }

    /**
     * 推送所有裝置(android 已測可用)
     * @param $platform
     * @param $title
     * @param $content
     * @param $builder
     * @param $extras
     * @return array
     */
    public function pushAll($title, $content, $builder, $extras)
    {
        $response = $this->newJpushClient()
            ->push()
            ->setPlatform('all')
            ->addAllAudience()
            ->androidNotification($content, [
                "title" => $title,
                "builder_id" => $builder,
                "extras" => $extras
            ])->send();
        return $response;
    }
}

Modules 目錄下新建目錄 Jpush,然後在 Jpush 目錄下新建檔案 JpushService.php 內容如下:

<?php
namespace App\Modules\Jpush;

use App\BaseService;
use App\Exceptions\BaseResponseException;
use App\Support\Jpush\Jpush;
use Log;

class JpushService extends BaseService
{

    /**
     * 通過 註冊id 推送
     * @param $platform
     * @param $registrationId
     * @param $title
     * @param $content
     * @param $builder
     * @param $extras
     * @return array
     */
    public static function pushByRegistrationId($platform, $registrationId, $title, $content, $builder, $extras)
    {
        $jpush = new Jpush();
        if ($platform == 'android') {
            $response = $jpush->androidPushByRegistrationId($platform, $registrationId, $title, $content, $builder, $extras);
        } elseif ($platform == 'ios') {
            $response = $jpush->iosPushByRegistrationId($platform, $registrationId, $title, $content, $builder, $extras);
        } else {
            throw new BaseResponseException('推送平臺型別錯誤');
        }

        if ($response['http_code'] != 200) {
            Log::info('推送失敗 by registrationId', compact('response', 'platform', 'registrationId', 'title', 'content'));
        } else {
            Log::info('推送成功 by registrationId', compact('response', 'platform', 'registrationId', 'title', 'content'));
        }

        return $response;
    }

    /**
     * 通過 別名 推送
     * @param $platform
     * @param $alias
     * @param $title
     * @param $content
     * @param $builder
     * @param $extras
     * @return array
     */
    public static function pushByAlias($platform, $alias, $title, $content, $builder, $extras)
    {
        $jpush = new Jpush();
        if ($platform == 'android') {
            $response = $jpush->androidPushByAlias($platform, $alias, $title, $content, $builder, $extras);
        } elseif ($platform == 'ios') {
            $response = $jpush->iosPushByAlias($platform, $alias, $title, $content, $builder, $extras);
        } else {
            throw new BaseResponseException('推送平臺型別錯誤');
        }

        if ($response['http_code'] != 200) {
            Log::info('推送失敗 by alias', compact('response', 'platform', 'alias', 'title', 'content'));
        } else {
            Log::info('推送成功 by alias', compact('response', 'platform', 'alias', 'title', 'content'));
        }

        return $response;
    }

    /**
     * 推送全部 android裝置 或 ios裝置
     * @param $platform
     * @param $title
     * @param $content
     * @param $builder
     * @param $extras
     * @return array
     */
    public static function pushAllAndroidOrIos($platform, $title, $content, $builder, $extras)
    {
        $jpush = new Jpush();
        $response = $jpush->AndroidOrIosPushAll($platform, $title, $content, $builder, $extras);
        if ($response['http_code'] != 200) {
            Log::info('推送失敗 by all', compact('response', 'platform', 'title', 'content'));
        } else {
            Log::info('推送成功 by all', compact('response', 'platform', 'title', 'content'));
        }
        return $response;
    }

    /**
     * 推送全部
     * @param $title
     * @param $content
     * @param $builder
     * @param $extras
     * @return array
     */
    public static function pushAll($title, $content, $builder, $extras)
    {
        $jpush = new Jpush();
        $response = $jpush->pushAll($title, $content, $builder, $extras);
        if ($response['http_code'] != 200) {
            Log::info('推送失敗 by all', compact('response', 'title', 'content'));
        } else {
            Log::info('推送成功 by all', compact('response', 'title', 'content'));
        }
        return $response;
    }
}

新建路由:

// 手動推送測試(極光推送)
Route::post('message/jpush', 'MessageController@testJpush');

新建控制器並新增方法如下:

   /**
     * 手動測試推送(極光推送)
     */
    public function testJpush()
    {
        // 推送平臺 android 或 ios
        $platform = 'android';

        // 通過id推送,支援單個(字串)或多個(陣列)
        // $registrationId = '170976fa8afac898f28443';
        // $registrationId = [
        //     '170976fa8afac898f2822',
        //     '100d8559093c40254b03'
        // ];

        // 通過別名推送,支援單個(字串)或多個(陣列),下面的 * 號只是為了隱藏手機號
        // $alias = '188****2730';
        // $alias = [
        //     '188****2730',
        //     '176****4994',
        // ];

        $title = '恭喜您中獎了!';
        $content = '恭喜您中獎了!500萬元大獎哦!';
        // 通知欄樣式 ID
        $builder = 1;
        // 返回給客戶端的引數,用於其他需求,比如點選通知跳轉到詳情
        $extras = [
            'orderid' => 13545,
        ];

        // 通過id推送
        // $data = JpushService::pushByRegistrationId($platform, $registrationId, $title, $content, $builder, $extras);

        // 通過別名推送
        // $data = JpushService::pushByAlias($platform, $alias, $title, $content, $builder, $extras);

        // 推送所有android裝置或者ios裝置
        // $data = JpushService::pushAllAndroidOrIos($platform, $title, $content, $builder, $extras);

        // 推送所有裝置
        $data = JpushService::pushAll($title, $content, $builder, $extras);
        dd($data);
    }

最後,請求該測試方法,得到返回結果如下:

在 Laravel 中使用 極光推送

在 Laravel 中使用 極光推送

本文只用到了最常用的通知欄推送,如需其他方式請參考 官方文件

為了能夠看的清楚些,寫的有點囉嗦,很多地方都可以優化,大家多多擔待,以上方法都在 android demo 下測試過,可用,等 IOS 小哥哥整合 IOS 包後,我測過 iOS 後重新補充整理一下,Thanks♪(・ω・)ノ。

乾坤未定,你我皆是黑馬

相關文章