温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Yii1.1框架怎么实现PHP极光推送消息通知功能

发布时间:2021-05-22 16:30:37 来源:亿速云 阅读:140 作者:Leah 栏目:开发技术

本篇文章给大家分享的是有关使用Yii1.1框架怎么实现PHP极光推送消息通知功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

一、下载极光推送PHP SDK,解压后放在/protected/components/目录下,如下图所示:

使用Yii1.1框架怎么实现PHP极光推送消息通知功能

二、完善修改下官方的demo例子,我这里复制一份demo,改为NotifyPush.php,如下代码:

<?php
require dirname(__FILE__) . '/jpush-api-php-client/autoload.php';
use JPush\Client as JPush;
class NotifyPush {
 static function pushAlias($alias,$ticket='消息提醒',$alert){
  $appKey=Yii::app()->params['push']['AppKey'];
  $appMasterSecret=Yii::app()->params['push']['AppMasterSecret'];
  $production_mode=Yii::app()->params['push']['production_mode'];
  $client = new JPush($appKey, $appMasterSecret);
// 完整的推送示例
// 这只是使用样例,不应该直接用于实际生产环境中 !!
  try {
   $response = $client->push()
    ->setPlatform(array('ios', 'android'))
    // 一般情况下,关于 audience 的设置只需要调用 addAlias、addTag、addTagAnd 或 addRegistrationId
    // 这四个方法中的某一个即可,这里仅作为示例,当然全部调用也可以,多项 audience 调用表示其结果的交集
    // 即是说一般情况下,下面三个方法和没有列出的 addTagAnd 一共四个,只适用一个便可满足大多数的场景需求
    ->addAlias($alias)
    ->setNotificationAlert($alert)
    ->iosNotification($ticket, array(
     'sound' => 'sound.caf',
     // 'badge' => '+1',
     // 'content-available' => true,
     // 'mutable-content' => true,
     'category' => 'jiguang',
     'extras' => array(
      'key' => 'value',
      'jiguang'
     ),
    ))
    ->androidNotification($ticket, array(
     'title' => $alert,
     // 'build_id' => 2,
     'extras' => array(
      'key' => 'value',
      'jiguang'
     ),
    ))
    ->message($alert, array(
     'title' => $alert,
     // 'content_type' => 'text',
     'extras' => array(
      'key' => 'value',
      'jiguang'
     ),
    ))
    ->options(array(
     // sendno: 表示推送序号,纯粹用来作为 API 调用标识,
     // API 返回时被原样返回,以方便 API 调用方匹配请求与返回
     // 这里设置为 100 仅作为示例
     // 'sendno' => 100,
     // time_to_live: 表示离线消息保留时长(秒),
     // 推送当前用户不在线时,为该用户保留多长时间的离线消息,以便其上线时再次推送。
     // 默认 86400 (1 天),最长 10 天。设置为 0 表示不保留离线消息,只有推送当前在线的用户可以收到
     // 这里设置为 1 仅作为示例
     // 'time_to_live' => 1,
     // apns_production: 表示APNs是否生产环境,
     // True 表示推送生产环境,False 表示要推送开发环境;如果不指定则默认为推送生产环境
     'apns_production' => $production_mode,
     // big_push_duration: 表示定速推送时长(分钟),又名缓慢推送,把原本尽可能快的推送速度,降低下来,
     // 给定的 n 分钟内,均匀地向这次推送的目标用户推送。最大值为1400.未设置则不是定速推送
     // 这里设置为 1 仅作为示例
     // 'big_push_duration' => 1
    ))
    ->send();
   print_r($response);
  } catch (\JPush\Exceptions\APIConnectionException $e) {
   // try something here
   print $e;
  } catch (\JPush\Exceptions\APIRequestException $e) {
   // try something here
   print $e;
  }
 }
 static function pushAll($ticket='消息提醒',$alert){
  $appKey=Yii::app()->params['push']['AppKey'];
  $appMasterSecret=Yii::app()->params['push']['AppMasterSecret'];
  $production_mode=Yii::app()->params['push']['production_mode'];
  $client = new JPush($appKey, $appMasterSecret);
// 完整的推送示例
// 这只是使用样例,不应该直接用于实际生产环境中 !!
  try {
   $response = $client->push()
    ->setPlatform(array('ios', 'android'))
    ->setAudience('all')
    // 一般情况下,关于 audience 的设置只需要调用 addAlias、addTag、addTagAnd 或 addRegistrationId
    // 这四个方法中的某一个即可,这里仅作为示例,当然全部调用也可以,多项 audience 调用表示其结果的交集
    // 即是说一般情况下,下面三个方法和没有列出的 addTagAnd 一共四个,只适用一个便可满足大多数的场景需求
    ->setNotificationAlert($alert)
    ->iosNotification($ticket, array(
     'sound' => 'sound.caf',
     // 'badge' => '+1',
     // 'content-available' => true,
     // 'mutable-content' => true,
     'category' => 'jiguang',
     'extras' => array(
      'key' => 'value',
      'jiguang'
     ),
    ))
    ->androidNotification($ticket, array(
     'title' => $alert,
     // 'build_id' => 2,
     'extras' => array(
      'key' => 'value',
      'jiguang'
     ),
    ))
    ->message($alert, array(
     'title' => $alert,
     // 'content_type' => 'text',
     'extras' => array(
      'key' => 'value',
      'jiguang'
     ),
    ))
    ->options(array(
     // sendno: 表示推送序号,纯粹用来作为 API 调用标识,
     // API 返回时被原样返回,以方便 API 调用方匹配请求与返回
     // 这里设置为 100 仅作为示例
     // 'sendno' => 100,
     // time_to_live: 表示离线消息保留时长(秒),
     // 推送当前用户不在线时,为该用户保留多长时间的离线消息,以便其上线时再次推送。
     // 默认 86400 (1 天),最长 10 天。设置为 0 表示不保留离线消息,只有推送当前在线的用户可以收到
     // 这里设置为 1 仅作为示例
     // 'time_to_live' => 1,
     // apns_production: 表示APNs是否生产环境,
     // True 表示推送生产环境,False 表示要推送开发环境;如果不指定则默认为推送生产环境
     'apns_production' => $production_mode,
     // big_push_duration: 表示定速推送时长(分钟),又名缓慢推送,把原本尽可能快的推送速度,降低下来,
     // 给定的 n 分钟内,均匀地向这次推送的目标用户推送。最大值为1400.未设置则不是定速推送
     // 这里设置为 1 仅作为示例
     // 'big_push_duration' => 1
    ))
    ->send();
   print_r($response);
  } catch (\JPush\Exceptions\APIConnectionException $e) {
   // try something here
   print $e;
  } catch (\JPush\Exceptions\APIRequestException $e) {
   // try something here
   print $e;
  }
 }
}

三、调用NotifyPush.php里的方法,实现推送,如下代码:

class CronNotifyPushCommand extends CConsoleCommand{
 public $keys=array();
 public function init(){
  parent::init();
 }
 public function actionIndex(){
  echo 'start CronNotifyPushCommand '.chr(10);
  if(!Yii::app()->params['push']['push_status']){
   echo 'push status disabled';die();
  }
  $rkey='message_notify_list';
  $waitTotals=Fredis::model()->redis->lsize($rkey);
  echo 'wait totals:'.$waitTotals.chr(10);
  $waitResult=true;
  $i=0;
  while($waitResult) {$i++;
   echo $i.'/'.$waitTotals.' wait to do'.chr(10);
   $waitResult=Fredis::model()->redis->rpop($rkey);
   if(!$waitResult) {
    continue;
   }
   $db_data=unserialize($waitResult);var_dump($db_data);
   $message_content=$db_data['message_content'];
   $uid=$db_data['uid'];
   $alias=$uid;
   if($uid==0){
    NotifyPush::pushAll($message_content, $message_content);
   }else {
    NotifyPush::pushAlias($alias, $message_content, $message_content);
   }
  }
  echo 'end'.chr(10);
 }
}

php有什么特点

1、执行速度快。2、具有很好的开放性和可扩展性。3、PHP支持多种主流与非主流的数据库。4、面向对象编程:PHP提供了类和对象。5、版本更新速度快。6、具有丰富的功能。7、可伸缩性。8、功能全面,包括图形处理、编码与解码、压缩文件处理、xml解析等。

以上就是使用Yii1.1框架怎么实现PHP极光推送消息通知功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI