JpushHelper.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Helper;
  3. use JPush\Client as JPush;
  4. use App\Models\UserInfoModel;
  5. use App\User;
  6. trait JpushHelper
  7. {
  8. public function jPush($title,$content='',$userid=0) {
  9. $app_key = '69838317211448192366f9d8';
  10. $master_secret = 'f202d10301151e1816b49e55';
  11. $client = new JPush($app_key, $master_secret, storage_path('logs/jpush.log'));
  12. // $push = new Push;
  13. // $push->title = $title;
  14. // $push->content = $content;
  15. // $push->user_id = $userid;
  16. // $res=$push->save();
  17. // if($userid){
  18. $user = UserInfoModel::find($userid);
  19. // }
  20. if($user&&$user->jpush){
  21. $regId = array($user->jpush);
  22. $options = array(
  23. 'apns_production' => true,
  24. );
  25. try {
  26. $pusher = $client->push();
  27. $pusher->setPlatform(['ios', 'android']);
  28. // $pusher->addAllAudience();
  29. $pusher->addRegistrationId($regId);
  30. $pusher->setNotificationAlert($title);
  31. $pusher->options($options);
  32. $pusher->send();
  33. \Log::info("$user->jpush");
  34. \Log::info('jpush send!');
  35. } catch (\JPush\Exceptions\APIConnectionException $e) {
  36. // try something here
  37. \Log::error($e);
  38. } catch (\JPush\Exceptions\APIRequestException $e) {
  39. // try something here
  40. \Log::error($e);
  41. }
  42. }
  43. }
  44. }