ThinkPHP集成极光推送简单实例

本文介绍如何在ThinkPHP框架中集成极光推送服务。通过简单的步骤,包括配置极光推送参数、创建Push控制器及编写推送逻辑等,实现向所有用户或特定用户群体发送消息的功能。

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

ThinkPHP集成极光推送简单实例

准备工作

开始集成

这里写图片描述

配置极光推送

这里写图片描述

  'JPUSH' => array(
        'APP_KEY' => '',
        'MASTER_SECRET' => ''
    ),
写一个Push控制器
<?php
namespace Api\Controller;
require 'vendor/autoload.php';

use JPush\Client as JPushClient;
use Think\Controller;

class PushController extends Controller{
    public function sendAll() {
       $client = new JPushClient(C('JPUSH.APP_KEY'),            C('JPUSH.MASTER_SECRET'));
        $pusher = $client->push();
        //设置发送平台
        $pusher->setPlatform('all');
        //设置发送对象,发送给所有人
        $pusher->addAllAudience();
        //设置发送内容
        $pusher->setNotificationAlert('Hello, JPush');     
        try {
            $pusher->send();           
        } catch (\JPush\Exceptions\JPushException $e) {
            print $e;
        }
    }
  }

部分函数讲解

函数名作用用法
setPlatform()设置发送平台可填写参数有: ios、android、all
addAllAudience()设置发送对象发送给所有人消息
setNotificationAlert()设置推送的消息setNotificationAlert(‘Hello, JPush’);
addTag()发送给某个特定标签的人群addTag(‘tag1’), addTag(‘tag1’,’tag2’)
addRegistrationId()发送给指定用户id的人,这个id是移动端生成的的addRegistrationId(‘user1’,’user2’)
发送扩展数据
//ios平台
$push->iosNotification('hello', [
  'sound' => 'sound',
  'badge' => '+1',
  'extras' => [
    'key' => 'value'
  ]
]);

//安卓平台
$push->androidNotification('hello', [
  'sound' => 'sound',
  'badge' => '+1',
  'extras' => [
    'key' => 'value'
  ]
]);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值