Ejemplo n.º 1
0
 public static function pushMessage($messageId, $time)
 {
     $time = (int) $time;
     $logFileName = 'pushmessage';
     $logMsg = 'Message (' . $messageId . ') started to push at ' . date('Y-m-d H:i:s', time());
     LogUtil::error('uhkklp-push-message:  ' . $logMsg);
     $queryMessage = new Query();
     $logMsg = '---Find message by id...';
     LogUtil::error('uhkklp-push-message:  ' . $logMsg);
     $message = $queryMessage->from('uhkklpMessage')->select(['content', 'linkType', 'newsId', 'pushTime'])->where(['_id' => new \MongoId($messageId)])->all();
     if (empty($message)) {
         $logMsg = 'Push canseled, message was deleted in database';
         LogUtil::error('uhkklp-push-message:  ' . $logMsg);
         return;
     }
     $message = $message[0];
     if ($message['pushTime']->sec != $time) {
         $logMsg = 'Push canseled, pushTime was changed in database';
         LogUtil::error('uhkklp-push-message:  ' . $logMsg);
         return;
     }
     $logMsg = '---done';
     LogUtil::error('uhkklp-push-message:  ' . $logMsg);
     $logMsg = '---Find tokens...';
     LogUtil::error('uhkklp-push-message:  ' . $logMsg);
     $queryUser = new Query();
     $users = $queryUser->from('uhkklpPushMessage')->select(['mobile', 'token', 'deviceType', 'accountId'])->where(['messageId' => new \MongoId($messageId)])->all();
     $logMsg = '---done';
     LogUtil::error('uhkklp-push-message:  ' . $logMsg);
     $gcmUtil = new GCMUtil();
     $apmUtil = new APMUtil();
     $logMsg = '---Pushing...';
     LogUtil::error('uhkklp-push-message:  ' . $logMsg);
     $msg['content'] = $message['content'];
     $msg['linkType'] = $message['linkType'];
     $msg['newsId'] = $message['newsId'];
     $log = new PushMessageLog();
     $log->messageId = $messageId;
     $log->startTime = date('Y-m-d H:i:s', time());
     $log->save();
     $results = [];
     foreach ($users as $user) {
         if ($user['deviceType'] == PushUser::DEVICE_ANDROID) {
             $response = $gcmUtil->pushMessageByToken($user['token'], $msg, $user['accountId']);
             LogUtil::error('GCMUtil:  ' . $user['token'] . '  ' . $response);
         } else {
             $response = $apmUtil->pushMsg($user['token'], $msg, $user['accountId']);
             LogUtil::error('APMUtil:  ' . $user['token'] . '  ' . $response);
         }
         $result['type'] = $user['deviceType'];
         $result['token'] = $user['token'];
         if (!empty($user['mobile'])) {
             $result['mobile'] = $user['mobile'];
         }
         $result['res'] = $response;
         $results[] = $result;
         $log->results = $results;
         $log->save();
         unset($result);
     }
     $apmUtil->closeFp();
     $logMsg = 'Push finished at ' . date('Y-m-d H:i:s', time());
     LogUtil::error('uhkklp-push-message:  ' . $logMsg);
     $log->endTime = date('Y-m-d H:i:s', time());
     $log->accountId = $user['accountId'];
     $log->save();
     $uhkklpMessage = Message::findOne([$messageId]);
     $uhkklpMessage->isPushed = true;
     $uhkklpMessage->update();
     return $results;
 }
Ejemplo n.º 2
0
 public function actionDelete()
 {
     $request = Yii::$app->request;
     $id = $request->post('$id');
     $message = Message::findOne([$id]);
     $message->isDeleted = true;
     $message->update();
     PushMessage::deleteAll(['messageId' => $message->_id]);
     Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
     return ['code' => '-1'];
 }