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

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

application/common/JPush.php

<?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

<?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

//调用推送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);


目录
相关文章
|
编译器 Linux PHP
【Azure App Service】为部署在App Service上的PHP应用开启JIT编译器
【Azure App Service】为部署在App Service上的PHP应用开启JIT编译器
101 1
|
16天前
|
存储 小程序 Java
热门小程序源码合集:微信抖音小程序源码支持PHP/Java/uni-app完整项目实践指南
小程序已成为企业获客与开发者创业的重要载体。本文详解PHP、Java、uni-app三大技术栈在电商、工具、服务类小程序中的源码应用,提供从开发到部署的全流程指南,并分享选型避坑与商业化落地策略,助力开发者高效构建稳定可扩展项目。
|
5月前
|
存储 消息中间件 前端开发
PHP后端与uni-app前端协同的校园圈子系统:校园社交场景的跨端开发实践
校园圈子系统校园论坛小程序采用uni-app前端框架,支持多端运行,结合PHP后端(如ThinkPHP/Laravel),实现用户认证、社交关系管理、动态发布与实时聊天功能。前端通过组件化开发和uni.request与后端交互,后端提供RESTful API处理业务逻辑并存储数据于MySQL。同时引入Redis缓存热点数据,RabbitMQ处理异步任务,优化系统性能。核心功能包括JWT身份验证、好友系统、WebSocket实时聊天及活动管理,确保高效稳定的用户体验。
295 4
PHP后端与uni-app前端协同的校园圈子系统:校园社交场景的跨端开发实践
|
7月前
|
JSON 自然语言处理 前端开发
【01】对APP进行语言包功能开发-APP自动识别地区ip后分配对应的语言功能复杂吗?-成熟app项目语言包功能定制开发-前端以uniapp-基于vue.js后端以laravel基于php为例项目实战-优雅草卓伊凡
【01】对APP进行语言包功能开发-APP自动识别地区ip后分配对应的语言功能复杂吗?-成熟app项目语言包功能定制开发-前端以uniapp-基于vue.js后端以laravel基于php为例项目实战-优雅草卓伊凡
307 72
【01】对APP进行语言包功能开发-APP自动识别地区ip后分配对应的语言功能复杂吗?-成熟app项目语言包功能定制开发-前端以uniapp-基于vue.js后端以laravel基于php为例项目实战-优雅草卓伊凡
|
10月前
|
移动开发 小程序 前端开发
使用php开发圈子系统特点,如何获取圈子系统源码,社交圈子运营以及圈子系统的功能特点,圈子系统,允许二开,免费源码,APP 小程序 H5
开发一个圈子系统(也称为社交网络或社群系统)可以是一个复杂但非常有趣的项目。以下是一些关键特点和步骤,帮助你理解如何开发、获取源码以及运营一个圈子系统。
378 4
|
PHP Windows
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
218 1
|
PHP 开发工具 git
【Azure 应用服务】在 App Service for Windows 中自定义 PHP 版本的方法
【Azure 应用服务】在 App Service for Windows 中自定义 PHP 版本的方法
124 1
|
Go PHP 数据安全/隐私保护
【应用服务 App Service】Azure App Service 中如何安装mcrypt - PHP
【应用服务 App Service】Azure App Service 中如何安装mcrypt - PHP
|
Java PHP
【应用服务 App Service】在Azure App Service中使用WebSocket - PHP的问题 - 如何使用和调用
【应用服务 App Service】在Azure App Service中使用WebSocket - PHP的问题 - 如何使用和调用
|
Linux 应用服务中间件 网络安全
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
112 0

热门文章

最新文章