通过 Notification 进行发送 以及 极光推送的集成

本文介绍了一种基于PHP的推送通知实现方案,包括极光推送(JPush)的使用方法及具体实现代码,适用于移动应用的消息推送场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

流程

触发推送

/** @var PluginHelp $help */
$help = PluginHelp::findOrFail($id);
$type = 'announce';
if ($help->cat_id == site('activity_category')) {
    $type = 'activity';
}
\Notification::send(new AccountFront(), new HelpNotification($type, $help));

Notification


class HelpNotification extends Notification implements ShouldQueue, Jpush, AliPush
{
    use SerializesModels, Queueable;

    /**
     * @var
     */
    protected $type;

    /**
     * @var
     */
    protected $help;

    /**
     * Create a new notification instance.
     * ArticleAndActivity constructor.
     * @param            $type
     * @param PluginHelp $help
     */
    public function __construct($type, $help)
    {
        $this->type = $type;
        $this->help = $help;
    }

    /**
     * Get the notification's delivery channels.
     * @param  mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [JpushChannel::class, AliPushChannel::class];
    }

    public function toJpush()
    {

        // 获取 jpush 注册码
        /** @var Collection $jpushIds */
        $jpushIds = AppInit::where('jpush_id', '!=', '')
            ->lists('jpush_id');
        $ids      = $jpushIds->toArray();
        if (!$ids) {
            return false;
        }

        $extras = [
            'head_pic' => '',
            'nickname' => '',
            'order_no' => '',
            'content'  => '',
            'type'     => $this->type,
            'title'    => $this->help->help_title,
            'url'      => route_url('app.help', '', ['id' => $this->help->id]),
            'id'       => $this->help->id,
            'cat_id'   => $this->help->cat_id,
        ];

        return [
            'broadcast_type'   => 'registration_id',
            'registration_ids' => $ids,
            'title'            => $this->help->help_title,
            'extras'           => $extras,
        ];
    }

    public function toAliPush()
    {
        $extras = [
            'type'   => $this->type,
            'title'  => $this->help->help_title,
            'url'    => route_url('app.help', '', ['id' => $this->help->id]),
            'id'     => $this->help->id,
            'cat_id' => $this->help->cat_id,
        ];

        return [
            'broadcast_type' => 'ALL',
            'title'          => $this->help->help_title,
            'content'        => mb_substr(strip_tags($this->help->content), 0, 30, 'utf-8'),
            'extras'         => $extras,
        ];
    }
}

通过极光进行推送

生成极光推送实例

/**
 * 极光推送实例
 * @package App\Lemon\Dailian\Application
 */
class JPush
{

    /**
     * @var JPushClient
     */
    protected static $instance;


    /**
     * Create a new instance of this singleton.
     * @return JPushClient
     */
    final public static function instance()
    {
        return isset(static::$instance)
            ? static::$instance
            : static::$instance = self::init();
    }


    /**
     * @return JPushClient
     */
    protected static function init()
    {
        $app_key       = config('services.jpush.key');
        $master_secret = config('services.jpush.secret');

        return (new JPushClient($app_key, $master_secret, null));
    }
}

极光推送频道

/**
 * 极光推送频道
 * @package App\Channels
 */
class JpushChannel
{
    /**
     * The Jpush client manager.
     * @var \JPush\Client
     */
    protected $clientManager;

    /**
     * Create a new JPush instance.
     */
    public function __construct()
    {
        $this->clientManager = JPush::instance();
    }

    /**
     * Send the given notification.
     * @param  mixed                     $notifiable
     * @param  Notification| JpushNotify $notification
     */
    public function send($notifiable, Notification $notification)
    {
        $notify = $notification->toJpush();

        if (!$notify) {
            return;
        }
        $title            = $notify['title'] ?? '';
        $extras           = $notify['extras'] ?? '';
        $broadcast_type   = $notify['broadcast_type'] ?? '';
        $registration_ids = $notify['registration_ids'] ?? '';

        if (!$registration_ids) {
            return;
        }
        try {

            $client = $this->clientManager->push()
                ->setPlatform('all')
                ->addTag([env('APP_ENV')]);
            switch ($broadcast_type) {
                case 'registration_id':
                    $client->addRegistrationId($registration_ids);
                    break;
            }
            $client->iosNotification($title, [
                'sound'    => 'sound.caf',
                'category' => 'jiguang',
                'extras'   => $extras,
            ])
                ->androidNotification($extras, [
                    'title'  => '易代练',
                    'extras' => $extras,
                ])
                ->options(array(
                    'apns_production' => (env('APP_ENV') == 'production'),
                ))
                ->send();
        } catch (\Exception $e) {
            \Log::debug($e->getMessage());
        }
    }

}

转载于:https://my.oschina.net/duoli/blog/1791700

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值