JpushHelper.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Helper;
  3. use JPush\Client as JPush;
  4. use App\Models\UserInfoModel;
  5. trait JpushHelper
  6. {
  7. public function jPush($title,$content='',$userid=0,$type=0,$id=0) {
  8. $app_key = '69838317211448192366f9d8';
  9. $master_secret = 'f202d10301151e1816b49e55';
  10. $client = new JPush($app_key, $master_secret, storage_path('logs/jpush.log'));
  11. \Log::info('jpush start!');
  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. if($type!=0&&$id!=0){
  31. $pusher->androidNotification($title,[
  32. 'title' => $content,
  33. 'content_type' => 'text',
  34. 'extras' => [
  35. 'type' => $type,
  36. 'id' => $id,
  37. ]
  38. ]);
  39. $pusher->iosNotification($title, [
  40. 'sound' => 'sound',
  41. 'badge' => '+1',
  42. 'extras' => [
  43. 'type' => $type,
  44. 'id' => $id,
  45. ]
  46. ]);
  47. }
  48. $pusher->setNotificationAlert($title);
  49. $pusher->options($options);
  50. $pusher->send();
  51. \Log::info('jpush send!');
  52. } catch (\JPush\Exceptions\APIConnectionException $e) {
  53. // try something here
  54. \Log::error($e);
  55. } catch (\JPush\Exceptions\APIRequestException $e) {
  56. // try something here
  57. \Log::error($e);
  58. }
  59. }
  60. }
  61. }