快捷地集成极光推送(JPush)到 Laravel 项目中

file

需求概述

我们在开发 Laravel 应用项目地时候,经常会用到“推送”,Laravel 自带了 Pusher 进行推送,但是国内环境大家都懂!所以这里以集成极光推送为例。

依赖极光 SDK

我们进入 Laravel 项目执行 composer require jpush/jpush 进行依赖。

创建配置文件

现在进入 Laravel 项目地 config/ 目录,创建一个 jpush.php 配置文件:

<?php

return [
    'production' => env('JPUSH_PRODUCTION', false), // 是否是正式环境
    'key' => env('JPUSH_APP_KEY', ''),                          // key
    'secret' => env('JPUSH_MASTER_SECRET', ''),       // master secret
    'log' => env('JPUSH_LOG_PATH', storage_path('logs/jpush.log')), // 日志文件路径
];

创建 JPush 单例到容器

为了方便之后在驱动中获取 JPush 实例,我们打开 app/Providers/AppServiceProvider.php 文件,在 register 方法中进行注册单例:

use JPush\Client as JPushClient;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        // 注册 JPush 客户端单例
        $this->app->singleton(JPushClient::class, function ($app) {
            $options = [
                $app->config->get('jpush.key'),
                $app->config->get('jpush.secret'),
                $app->config->get('jpush.log'),
            ];

            return new JPushClient(...$options);
        });
    }
}

创建驱动

我们在 Laravel 应用的 app/Notifications/ 目录下创建一个 Channels/ 目录,然后在其目录下创建一个 JPushChannel.php 文件 app/Notifications/Channels/JPushChannel.php :

<?php

namespace App\Notifications\Channels;

use JPush\Client as JPushClient;
use Illuminate\Notifications\Notification;

class JPushChannel
{
    protected $client;

    /**
     * Create the notification channel instance.
     *
     * @param \JPush\Client $client
     */
    public function __construct(JPushClient $client)
    {
        $this->client = $client;
    }

    /**
     * Send the given notification.
     *
     * @param  mixed  $notifiable
     * @param  \Illuminate\Notifications\Notification  $notification
     */
    public function send($notifiable, Notification $notification)
    {
        $push = $notification->toJPush($notifiable, $this->client->push());

        // 这里是为了屏蔽极光服务器没有找到设备等情况报错,
        try {
            $push->send();
        } catch (\Throwable $th) {
            //throw $th;
        }
    }
}

注册驱动

创建完成驱动后,我们需要注册驱动。所以我们打开 app/Providers/AppServiceProvider.php 文件在 register 添加注册代码:

use App\Notifications\Channels\JPushChannel;
use Illuminate\Notifications\ChannelManager;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        // 添加 JPush 驱动
        $this->app->extend(ChannelManager::class, function ($manager) {
            $manager->extend('jpush', function ($app) {
                return $app->make(JPushChannel::class);
            });
        });
    }
}

场景使用

我们打开 app/Notifications/ 目录下需要走极光推送的文件,录入我这里以「评论为例」:

use JPush\PushPayload;

class CommentByMeow extends Notification
{
    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database', 'jpush']; // 表示除了保存到数据库外,进行极光推送,`jpush` 就是注册驱动的时候的 key。
    }

    // 只需要按需设置好 JPush 的 payload 并返回回去就好了了
    public function toJPush($notifiable, PushPayload $payload): PushPayload
    {
        return $payload
            ->setPlatform('all')
            // ... 参考 https://github.com/jpush/jpush-api-php-client/blob/master/doc/api.md#push-api
        ;
    }
}

以我自己使用的为例:

file

其他

声明:我不是在做 JPush 广告,只是因为自己产品在使用,特意分享给大家!本人和极光推送没有任何关系。

本作品采用《CC 协议》,转载必须注明作者和本文链接
Seven 的代码太渣,欢迎关注我的新拓展包 medz/cors 解决 PHP 项目程序设置跨域需求。
本帖由系统于 5年前 自动加精
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 13
Complicated

我们用的也是极光推送

5年前 评论

怎么用呢?一直提示A facade root has not been set.

4年前 评论
medz

@jwwb681232 这是基于 Laravel 通知的一个通知驱动,集成和使用方法文章已经写了,你可以在看下 Laravel 通知章节的文档

4年前 评论

我使用了https://github.com/medz/laravel-jpush-notification-channel 和这编的教程 出现下面两个

file

file

4年前 评论
yu1ec 4年前

// $marketCode 是 验证码模型类型
//极光推送
$marketCode->notify(new MarketingCode());

file

4年前 评论
沈小明 (作者) 4年前
黑将军

支持单播吗?比如一些审核通知什么的,只针对个人的

4年前 评论
medz (楼主) 4年前

楼主你好,如果jpush的推送内容需要用到database里的数据内容,通知放在队列中的话又是无序的,会不会导致jpush先推送但是获取不到数据库信息的情况呢?

4年前 评论
medz (楼主) 4年前

请教下,极光日志按天切割该如何配置

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
创始人 @ Odore Inc.
文章
33
粉丝
202
喜欢
532
收藏
198
排名:23
访问:24.7 万
私信
所有博文
社区赞助商