ThinkPHP框架整合极光推送DEMO

时间:2023-03-09 20:16:41
ThinkPHP框架整合极光推送DEMO

极光推送(JPush)是独立的第三方云推送平台,致力于为全球移动应用开发者提供专业、高效的移动消息推送服务。

本篇博文讲述如何在将极光推送DEMO整合到ThinkPHP框架中,我使用的是极光推送PHP_DEMO_V3.4.3版本:

1、将极光推送DEMO文件(文件夹名称为Jpush)放入到你的公共文件夹(Common)中,按照极光开发文档在极光后台建立好自己的应用,获取相应的app_key、master_secret,在文件中将会用到这两个值;

ThinkPHP框架整合极光推送DEMO

2、如上,在公共文件夹(Common)下建立function.php文件;

  1. /**
  2. * 将数据先转换成json,然后转成array
  3. */
  4. function json_array($result){
  5. $result_json = json_encode($result);
  6. return json_decode($result_json,true);
  7. }
  8. /**
  9. * 向所有设备推送消息
  10. * @param string $message 需要推送的消息
  11. */
  12. function sendNotifyAll($message){
  13. require_once "JPush\JPush.php";
  14. $app_key = 'your app_key';                //填入你的app_key
  15. $master_secret = 'your master_secret';    //填入你的master_secret
  16. $client = new \JPush($app_key,$master_secret);
  17. $result = $client->push()->setPlatform('all')->addAllAudience()->setNotificationAlert($message)->send();
  18. return json_array($result);
  19. }
  20. /**
  21. * 向特定设备推送消息
  22. * @param array $regid 特定设备的设备标识
  23. * @param string $message 需要推送的消息
  24. */
  25. function sendNotifySpecial($regid,$message){
  26. require_once "JPush\JPush.php";
  27. $app_key = 'your app_key';                //填入你的app_key
  28. $master_secret = 'your master_secret';    //填入你的master_secret
  29. $client = new \JPush($app_key,$master_secret);
  30. $result = $client->push()->setPlatform('all')->addRegistrationId($regid)->setNotificationAlert($message)->send();
  31. return json_array($result);
  32. }
  33. /**
  34. * 向指定设备推送自定义消息
  35. * @param string $message 发送消息内容
  36. * @param array $regid 特定设备的id
  37. * @param int $did 状态值1
  38. * @param int $mid 状态值2
  39. */
  40. function sendSpecialMsg($regid,$message,$did,$mid){
  41. require_once "JPush\JPush.php";
  42. $app_key = 'your app_key';                //填入你的app_key
  43. $master_secret = 'your master_secret';    //填入你的master_secret
  44. $client = new \JPush($app_key,$master_secret);
  45. $result = $client->push()->setPlatform('all')->addRegistrationId($regid)
  46. ->addAndroidNotification($message,'',1,array('did'=>$did,'mid'=>$mid))
  47. ->addIosNotification($message,'','+1',true,'',array('did'=>$did,'mid'=>$mid))->send();
  48. return json_array($result);
  49. }
  50. /**
  51. * 得到各类统计数据
  52. * @param array $msgIds 推送消息返回的msg_id列表
  53. */
  54. function reportNotify($msgIds){
  55. require_once "JPush\JPush.php";
  56. $app_key = 'your app_key';                //填入你的app_key
  57. $master_secret = 'your master_secret';    //填入你的master_secret
  58. $client = new \JPush($app_key,$master_secret);
  59. $response = $client->report()->getReceived($msgIds);
  60. return json_array($response);
  61. }

在文件中写入各种集成函数,以方便在系统应用控制器中进行调用。

3、最后便是在控制器中进行调用即可;

  1. //向特定用户进行推送—单播
  2. //$regid可以是一个单个regid组成的字符串,也可以是多个regid组成的数组
  3. //$data['content']是你所需要推送的内容
  4. $result_s = sendNotifySpecial($regid, $data['content']);
  5. //想所有用户进行推送—广播
  6. $result_a = sendNotifyAll($data['content']);
  7. //获取统计用户是否获取推送消息的信息(或者有多少用户收到了推送消息)
  8. //$msgids是你推送消息的消息id
  9. $result_r = reportNotify($msgIds);
版权声明:转载时请标注http://blog.****.net/zhihua_w