Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace common\components;
- use ApnsPHP_Message;
- use bryglen\apnsgcm\Apns as ApnsOrigin;
- use common\models\Ad;
- use common\models\Comment;
- use common\models\Device;
- use common\models\Offer;
- use common\models\User;
- class Apns extends ApnsOrigin
- {
- const TYPE_AUCTION = 0;
- const TYPE_LIKE = 1;
- const TYPE_COMMENTS = 2;
- const TYPE_OTHER = 3;
- /**
- * @param User $user
- * @param string $message
- * @param integer $notification_type
- * @param null||integer $auction_id
- * @param null||integer $ad_id
- * @return bool
- */
- public function sendPush(User $user, $message, $notification_type, $auction_id = null, $ad_id = null)
- {
- $params = ['notification_type' => $notification_type];
- if (!empty($auction_id)) {
- $params['auction_id'] = $auction_id;
- }
- if (!empty($ad_id)) {
- $params['ad_id'] = $ad_id;
- }
- \Yii::info('send notification to ' . $user->username . ' id=' . $user->getId(), 'push');
- /** @var Device[] $devices */
- $devices = Device::find()->where(['user_id' => $user->getId()])->all();
- if (!empty($devices)) {
- foreach ($devices as $device) {
- $token = str_replace(" ", "", $device->token);
- \Yii::info('send to device ' . $token, 'push');
- \Yii::info('message ' . $message, 'push');
- if (YII_ENV != 'test') {
- /** @var ApnsPHP_Message $response */
- $response = $this->send($token, $message,
- $params,
- [
- 'sound' => 'default',
- 'badge' => 1
- ]
- );
- \Yii::info('result ' . $response, 'push');
- }
- }
- return true;
- } else {
- \Yii::info('not find device token for ' . $user->username . ' id =' . $user->getId(), 'push');
- return false;
- }
- }
- /**
- * @param Ad $ad
- * @return bool
- */
- public function sendAnswerOwnerInCommentsPush(Ad $ad)
- {
- $tokens = [];
- $params = [
- 'notification_type' => Apns::TYPE_COMMENTS,
- 'ad_id' => $ad->id
- ];
- /** @var Comment[] $comments */
- $comments = Comment::find()
- ->where(['ad_id' => $ad->id])
- ->andWhere(['!=', 'user_id', $ad->user_id])
- ->all();
- foreach ($comments as $comment) {
- /** @var Device[] $devices */
- $devices = Device::find()->where(['user_id' => $comment->user_id])->all();
- foreach ($devices as $device) {
- $tokens[] = str_replace(" ", "", $device->token);
- }
- }
- if (empty($tokens)) {
- return true;
- }
- $message = 'Владелец "' . $ad->title . '" ответил в комментариях';
- if (YII_ENV != 'test') {
- /** @var ApnsPHP_Message $response */
- $response = $this->sendMulti($tokens, $message,
- $params,
- [
- 'sound' => 'default',
- 'badge' => 1
- ]
- );
- \Yii::info('result multi ' . $response, 'push');
- }
- return true;
- }
- /**
- * @param User $owner
- * @param User $buyer
- * @param Offer $offer
- * @return bool
- */
- public function acceptPricePush(User $owner, User $buyer, Offer $offer)
- {
- if ($offer->from_user_id != $owner->id) {
- return $this->sendPush($owner, $buyer->username . ' согласился с ценой.', Apns::TYPE_AUCTION, $offer->auction_id);
- }
- return $this->sendPush($buyer, $owner->username . ' согласился с ценой.', Apns::TYPE_AUCTION, $offer->auction_id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement