push_example.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. // 这只是使用样例,不应该直接用于实际生产环境中 !!
  3. require __DIR__ . '/../autoload.php';
  4. use JPush\Client as JPush;
  5. // 这里填写appKey,masterSecret以及registration_id
  6. $app_key = 'e5c0d34f58732cf09b2d4d74';
  7. $master_secret = '4cdda6d3c8b029941dbc5cb3';
  8. $registration_id = ('registration_id');
  9. $client = new JPush($app_key, $master_secret);
  10. // 简单推送示例
  11. // 这只是使用样例,不应该直接用于实际生产环境中 !!
  12. // $push_payload = $client->push()
  13. // ->setPlatform('all')
  14. // ->addAllAudience()
  15. // ->setNotificationAlert('Hi, JPush');
  16. // try {
  17. // $response = $push_payload->send();
  18. // print_r($response);
  19. // } catch (\JPush\Exceptions\APIConnectionException $e) {
  20. // // try something here
  21. // print $e;
  22. // } catch (\JPush\Exceptions\APIRequestException $e) {
  23. // // try something here
  24. // print $e;
  25. // }
  26. // 完整的推送示例
  27. // 这只是使用样例,不应该直接用于实际生产环境中 !!
  28. try {
  29. $response = $client->push()
  30. ->setPlatform(array('ios', 'android'))
  31. // 一般情况下,关于 audience 的设置只需要调用 addAlias、addTag、addTagAnd 或 addRegistrationId
  32. // 这四个方法中的某一个即可,这里仅作为示例,当然全部调用也可以,多项 audience 调用表示其结果的交集
  33. // 即是说一般情况下,下面三个方法和没有列出的 addTagAnd 一共四个,只适用一个便可满足大多数的场景需求
  34. // ->addAlias('alias')
  35. // ->addTag(array('tag1', 'tag2'))
  36. // ->addRegistrationId($registration_id)
  37. ->addAllAudience()
  38. ->setNotificationAlert('Test custom')
  39. ->iosNotification('Hello IOS', array(
  40. 'sound' => 'sound.caf',
  41. // 'badge' => '+1',
  42. // 'content-available' => true,
  43. // 'mutable-content' => true,
  44. 'category' => 'jiguang',
  45. 'extras' => array(
  46. 'key' => 'value',
  47. 'jiguang'
  48. ),
  49. ))
  50. ->androidNotification('Hello Android', array(
  51. 'title' => 'hello jpush',
  52. // 'builder_id' => 2,
  53. 'extras' => array(
  54. 'key' => 'value',
  55. 'jiguang'
  56. ),
  57. ))
  58. // voip可以传输任意键值对,可用作自定义
  59. ->voip(array(
  60. 'test123' => 'val1',
  61. 'jsontest' => 2,
  62. 'booleantest' => true
  63. ))
  64. ->message('message content', array(
  65. 'title' => 'hello jpush',
  66. // 'content_type' => 'text',
  67. 'extras' => array(
  68. 'key' => 'value',
  69. 'jiguang'
  70. ),
  71. ))
  72. ->options(array(
  73. // sendno: 表示推送序号,纯粹用来作为 API 调用标识,
  74. // API 返回时被原样返回,以方便 API 调用方匹配请求与返回
  75. // 这里设置为 100 仅作为示例
  76. // 'sendno' => 100,
  77. // time_to_live: 表示离线消息保留时长(秒),
  78. // 推送当前用户不在线时,为该用户保留多长时间的离线消息,以便其上线时再次推送。
  79. // 默认 86400 (1 天),最长 10 天。设置为 0 表示不保留离线消息,只有推送当前在线的用户可以收到
  80. // 这里设置为 1 仅作为示例
  81. 'time_to_live' => 1,
  82. // apns_production: 表示APNs是否生产环境,
  83. // True 表示推送生产环境,False 表示要推送开发环境;如果不指定则默认为推送开发环境
  84. // 'apns_production' => false,
  85. // big_push_duration: 表示定速推送时长(分钟),又名缓慢推送,把原本尽可能快的推送速度,降低下来,
  86. // 给定的 n 分钟内,均匀地向这次推送的目标用户推送。最大值为1400.未设置则不是定速推送
  87. // 这里设置为 1 仅作为示例
  88. // 'big_push_duration' => 1
  89. ))
  90. ->setSmsMessage(array(
  91. 'delay_time' => 60,
  92. 'signid' => 154,
  93. 'temp_id' => 1,
  94. 'temp_para' => array(
  95. 'code' => 357
  96. ),
  97. 'active_filter' => false
  98. ))
  99. // custom可自定义最外层参数,如skd未支持部分文档功能,用户可自行写入
  100. // 这里仅作为例子展示
  101. // ->custom(array(
  102. // 'sms_message' => array(
  103. // 'active_filter' => false,
  104. // 'delay_time' => 60,
  105. // 'signid' => 154,
  106. // 'temp_id' => 1,
  107. // 'temp_para' => array(
  108. // 'code' => 357
  109. // )),
  110. // 'options' => array(
  111. // 'apns_production' => false,
  112. // 'time_to_live' => 62000,
  113. // )
  114. // ))
  115. ->send();
  116. print_r($response);
  117. } catch (\JPush\Exceptions\APIConnectionException $e) {
  118. // try something here
  119. print $e;
  120. } catch (\JPush\Exceptions\APIRequestException $e) {
  121. // try something here
  122. print $e;
  123. }