Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. <?php
  2. namespace common\components;
  3.  
  4. use ApnsPHP_Message;
  5. use bryglen\apnsgcm\Apns as ApnsOrigin;
  6. use common\models\Ad;
  7. use common\models\Comment;
  8. use common\models\Device;
  9. use common\models\Offer;
  10. use common\models\User;
  11.  
  12.  
  13.  
  14. class Apns extends ApnsOrigin
  15. {
  16. const TYPE_AUCTION = 0;
  17. const TYPE_LIKE = 1;
  18. const TYPE_COMMENTS = 2;
  19. const TYPE_OTHER = 3;
  20.  
  21. /**
  22. * @param User $user
  23. * @param string $message
  24. * @param integer $notification_type
  25. * @param null||integer $auction_id
  26. * @param null||integer $ad_id
  27. * @return bool
  28. */
  29. public function sendPush(User $user, $message, $notification_type, $auction_id = null, $ad_id = null)
  30. {
  31.  
  32. $params = ['notification_type' => $notification_type];
  33. if (!empty($auction_id)) {
  34. $params['auction_id'] = $auction_id;
  35. }
  36. if (!empty($ad_id)) {
  37. $params['ad_id'] = $ad_id;
  38. }
  39.  
  40. \Yii::info('send notification to ' . $user->username . ' id=' . $user->getId(), 'push');
  41. /** @var Device[] $devices */
  42. $devices = Device::find()->where(['user_id' => $user->getId()])->all();
  43. if (!empty($devices)) {
  44. foreach ($devices as $device) {
  45. $token = str_replace(" ", "", $device->token);
  46. \Yii::info('send to device ' . $token, 'push');
  47. \Yii::info('message ' . $message, 'push');
  48.  
  49. if (YII_ENV != 'test') {
  50. /** @var ApnsPHP_Message $response */
  51. $response = $this->send($token, $message,
  52. $params,
  53. [
  54. 'sound' => 'default',
  55. 'badge' => 1
  56. ]
  57. );
  58. \Yii::info('result ' . $response, 'push');
  59. }
  60. }
  61. return true;
  62. } else {
  63. \Yii::info('not find device token for ' . $user->username . ' id =' . $user->getId(), 'push');
  64. return false;
  65. }
  66. }
  67.  
  68.  
  69. /**
  70. * @param Ad $ad
  71. * @return bool
  72. */
  73. public function sendAnswerOwnerInCommentsPush(Ad $ad)
  74. {
  75. $tokens = [];
  76. $params = [
  77. 'notification_type' => Apns::TYPE_COMMENTS,
  78. 'ad_id' => $ad->id
  79. ];
  80.  
  81. /** @var Comment[] $comments */
  82. $comments = Comment::find()
  83. ->where(['ad_id' => $ad->id])
  84. ->andWhere(['!=', 'user_id', $ad->user_id])
  85. ->all();
  86. foreach ($comments as $comment) {
  87. /** @var Device[] $devices */
  88. $devices = Device::find()->where(['user_id' => $comment->user_id])->all();
  89. foreach ($devices as $device) {
  90. $tokens[] = str_replace(" ", "", $device->token);
  91. }
  92. }
  93. if (empty($tokens)) {
  94. return true;
  95. }
  96.  
  97. $message = 'Владелец "' . $ad->title . '" ответил в комментариях';
  98. if (YII_ENV != 'test') {
  99. /** @var ApnsPHP_Message $response */
  100. $response = $this->sendMulti($tokens, $message,
  101. $params,
  102. [
  103. 'sound' => 'default',
  104. 'badge' => 1
  105. ]
  106. );
  107. \Yii::info('result multi ' . $response, 'push');
  108. }
  109. return true;
  110. }
  111.  
  112. /**
  113. * @param User $owner
  114. * @param User $buyer
  115. * @param Offer $offer
  116. * @return bool
  117. */
  118. public function acceptPricePush(User $owner, User $buyer, Offer $offer)
  119. {
  120. if ($offer->from_user_id != $owner->id) {
  121. return $this->sendPush($owner, $buyer->username . ' согласился с ценой.', Apns::TYPE_AUCTION, $offer->auction_id);
  122. }
  123. return $this->sendPush($buyer, $owner->username . ' согласился с ценой.', Apns::TYPE_AUCTION, $offer->auction_id);
  124. }
  125.  
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement