前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP实现极光推送jpush/jpush 手机APP消息推送

PHP实现极光推送jpush/jpush 手机APP消息推送

作者头像
OwenZhang
发布2021-12-08 14:40:25
3K0
发布2021-12-08 14:40:25
举报
文章被收录于专栏:Owen's World

application/common/JPush.php

代码语言:javascript
复制
<?php
/**
 * 极光推送
 */
namespace app\common;

use JPush\Client;

class JPush
{
    private $key = '';
    private $secret = '';

    use InstanceTrait;

    public function _init()
    {
        $this->key = '3412f';
        $this->secret = '2c23';
    }

    /**
     * 指定注册ID发送
     */
    public function push($registrationId, $title, $text, $url, $itemId)
    {
        $registrationId = array_filter($registrationId);
        \Log::info(var_export([$registrationId, $title, $text, $url, $itemId], true));
        $this->_init();

        $client = new Client($this->key, $this->secret);
        $push = $client->push();

        $push->setPlatform('all');
        $push->addRegistrationId($registrationId);
        $push->addAndroidNotification($title, $text);
        $push->androidNotification($text, ['title' => $title, 'alert_type' => 2, 'extras' => ['url' => $url, 'id' => $itemId]]);
        $push->iosNotification(['title' => $title, 'body' => $text], ['extras' => ['url' => $url, 'id' => $itemId]]);
        $push->options(['apns_production' => false]);
        $push->send();
    }
}
复制代码

application/lucky/push/service/PushService.php

代码语言:javascript
复制
<?php
/**
 * 推送服务
 */

namespace app\lucky\push\service;

use app\common\JPush;
use app\lucky\follow\service\FollowService;
use app\lucky\push\model\UserPushConfigModel;
use app\lucky\subscribe\service\SubscribeService;
use app\sports\match\service\FollowMatchService;
use app\sports\match\service\SportsApiService;

class PushService
{
    public function push()
    {
        try {
            $push = JPush::getInstance()->push(['1517badf006e81e'], '我是标题', '我是内容', 'GameDetails:1909991');
            var_dump($push);
        } catch (\Exception $e) {
            var_dump($e->getMessage());
        }

    }

    /**
     * 推送设置
     */
    public function pushConfig($userId, $sys, $ext)
    {
        $userPushConfigModel = new UserPushConfigModel();
        $result = $userPushConfigModel->updateConfig($userId, $sys, ['ext' => json_encode($ext), 'update_time' => time()]);

        if ($result) {
            return ['code' => 0, 'msg' => '添加成功'];
        }

        return ['code' => -1, 'msg' => '添加失败'];
    }

    /**
     * 获取配置
     */
    public function getPushConfig($userId, $sys)
    {
        $userPushConfigModel = new UserPushConfigModel();
        $result = $userPushConfigModel->getConfigByUserId($userId, $sys);
        $data = isset($result['ext']) ? $result['ext'] : '';
        $data = (array)json_decode($data, true);

        return ['code' => 0, 'msg' => '获取成功', 'data' => $data];
    }


    /**
     * 发布资讯推送
     */
    public function blogPush($authorId, $title, $text, $blogId)
    {
        //获取作者的粉丝列表ID
        $followService = new FollowService();
        $followListId = $followService->getAuthorFollowList($authorId, 'sports');

        //获取用户ID的配置
        $pushModel = new UserPushConfigModel();
        $pushConfig = $pushModel->getConfigByUserIdArr($followListId, 'sports');

        $identifyArr = [];
        foreach ($pushConfig as $value) {
            $ext = (array)json_decode($value['ext'], true);
            if (in_array('information', $ext)) {
                $identifyArr[] = $value['identify'];
            }
        }

        if (!empty($identifyArr)) {
            try {
                JPush::getInstance()->push($identifyArr, $title, $text, 'InfoDetails', $blogId);
            } catch (\Exception $exception) {
                \Log::error($exception->getMessage());
            }
        }
    }

    

    /**
     * 登录关联极光ID
     */
    public function loginLinkPush($userId, $identify, $sys = '343')
    {
        $userPushConfigModel = new UserPushConfigModel();
        $config = $userPushConfigModel->getConfigByUserId($userId, 'sports');

        if (empty($config)) {
            $data = [
                'user_id' => $userId,
                'identify' => $identify,
                'update_time' => time(),
                'sys' => $sys,
                'ext' => json_encode(['start' => true, 'end' => true, 'score' => true, 'news' => true, 'information' => true])
            ];

            $result = $userPushConfigModel->addConfig($data);
            if (empty($result)) {
                return ['code' => -1, 'msg' => '添加极光推送失败'];
            }

            return ['code' => 0, 'msg' => '添加极光推送成功'];
        }

        $data = [
            'identify' => $identify,
            'update_time' => time(),
        ];
        $result = $userPushConfigModel->updateConfig($userId, $sys, $data);
        if (empty($result)) {
            return ['code' => -1, 'msg' => '更新极光推送失败'];
        }

        return ['code' => 0, 'msg' => '更新极光推送成功'];
    }

    /**
     * 退出登录关联极光ID
     */
    public function logoutLinkPush($userId, $sys = '343')
    {
        $userPushConfigModel = new UserPushConfigModel();
        $data = [
            'identify' => '',
            'update_time' => time(),
        ];
        $result = $userPushConfigModel->updateConfig($userId, $sys, $data);
        if (empty($result)) {
            return ['code' => -1, 'msg' => '退出登录,更新极光推送失败'];
        }

        return ['code' => 0, 'msg' => '退出登录,更新极光推送成功'];
    }
}
复制代码

application/lucky/admin/controller/Blog.php

代码语言:javascript
复制
//调用推送APP PUSH
$data['author_id']=123;
$data['title']='文章标题今天三美好的一天';
$title = '张三发布资讯';
$pushService = new PushService();
$pushService->blogPush($data['author_id'], $title, mb_substr($data['title'], 0, 10) . '...', $result);
复制代码
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年10月25日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档