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编译器
151 1
|
10月前
|
JSON 自然语言处理 前端开发
【01】对APP进行语言包功能开发-APP自动识别地区ip后分配对应的语言功能复杂吗?-成熟app项目语言包功能定制开发-前端以uniapp-基于vue.js后端以laravel基于php为例项目实战-优雅草卓伊凡
【01】对APP进行语言包功能开发-APP自动识别地区ip后分配对应的语言功能复杂吗?-成熟app项目语言包功能定制开发-前端以uniapp-基于vue.js后端以laravel基于php为例项目实战-优雅草卓伊凡
535 72
【01】对APP进行语言包功能开发-APP自动识别地区ip后分配对应的语言功能复杂吗?-成熟app项目语言包功能定制开发-前端以uniapp-基于vue.js后端以laravel基于php为例项目实战-优雅草卓伊凡
|
5月前
|
编解码 数据安全/隐私保护
手机录制脚本自动执行, 免root屏幕录制脚本,自动脚本精灵app【autojs】
自动创建保存目录确保路径存在 动态生成带时间戳的文件名避免重复
|
7月前
|
安全 测试技术 开发者
银行转账模拟器手机版app, 银行转账凭证生成器app,用autojs实现效果【逼真效果】
本内容展示了一套基于Auto.js的银行APP自动化测试脚本和框架,用于学习和研究移动应用测试技术。脚本涵盖登录、转账等功能测试
|
12月前
|
缓存 前端开发 IDE
【06】flutter完成注册页面-密码登录-手机短信验证-找回密码相关页面-并且实现静态跳转打包demo做演示-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
【06】flutter完成注册页面-密码登录-手机短信验证-找回密码相关页面-并且实现静态跳转打包demo做演示-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
331 0
【06】flutter完成注册页面-密码登录-手机短信验证-找回密码相关页面-并且实现静态跳转打包demo做演示-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
|
移动开发 Android开发 数据安全/隐私保护
移动应用与系统的技术演进:从开发到操作系统的全景解析随着智能手机和平板电脑的普及,移动应用(App)已成为人们日常生活中不可或缺的一部分。无论是社交、娱乐、购物还是办公,移动应用都扮演着重要的角色。而支撑这些应用运行的,正是功能强大且复杂的移动操作系统。本文将深入探讨移动应用的开发过程及其背后的操作系统机制,揭示这一领域的技术演进。
本文旨在提供关于移动应用与系统技术的全面概述,涵盖移动应用的开发生命周期、主要移动操作系统的特点以及它们之间的竞争关系。我们将探讨如何高效地开发移动应用,并分析iOS和Android两大主流操作系统的技术优势与局限。同时,本文还将讨论跨平台解决方案的兴起及其对移动开发领域的影响。通过这篇技术性文章,读者将获得对移动应用开发及操作系统深层理解的钥匙。
511 12
|
小程序 JavaScript API
微信小程序开发之:保存图片到手机,使用uni-app 开发小程序;还有微信原生保存图片到手机
这篇文章介绍了如何在uni-app和微信小程序中实现将图片保存到用户手机相册的功能。
3463 0
微信小程序开发之:保存图片到手机,使用uni-app 开发小程序;还有微信原生保存图片到手机
|
Go PHP 数据安全/隐私保护
【应用服务 App Service】Azure App Service 中如何安装mcrypt - PHP
【应用服务 App Service】Azure App Service 中如何安装mcrypt - PHP
129 2
|
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. 错误
276 1
|
PHP 开发工具 git
【Azure 应用服务】在 App Service for Windows 中自定义 PHP 版本的方法
【Azure 应用服务】在 App Service for Windows 中自定义 PHP 版本的方法
210 1

热门文章

最新文章