JpushHelper.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Helper;
  3. use JPush\Client as JPush;
  4. //use App\Models\Push;
  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 = User::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('jpush send!');
  34. } catch (\JPush\Exceptions\APIConnectionException $e) {
  35. // try something here
  36. \Log::error($e);
  37. } catch (\JPush\Exceptions\APIRequestException $e) {
  38. // try something here
  39. \Log::error($e);
  40. }
  41. }
  42. }
  43. }